目录:
Pod 进阶
![0](/i/l/?n=23&i=blog/2964696/202302/2964696-20230223185949327-1971250678.png)
资源限制
当定义 Pod 时可以选择性地为每个容器设定所需要的资源数量。 最常见的可设定资源是 CPU 和内存大小,以及其他类型的资源。 当为 Pod 中的容器指定了 request 资源时,调度器就使用该信息来决定将 Pod 调度到哪个节点上。当还为容器指定了 limit 资源时,kubelet 就会确保运行的容器不会使用超出所设的 limit 资源量。kubelet 还会为容器预留所设的 request 资源量, 供该容器使用。 如果 Pod 运行所在的节点具有足够的可用资源,容器可以使用超出所设置的 request 资源量。不过,容器不可以使用超出所设置的 limit 资源量。 如果给容器设置了内存的 limit 值,但未设置内存的 request 值,Kubernetes 会自动为其设置与内存 limit 相匹配的 request 值。 类似的,如果给容器设置了 CPU 的 limit 值但未设置 CPU 的 request 值,则 Kubernetes 自动为其设置 CPU 的 request 值 并使之与 CPU 的 limit 值匹配。 官网示例: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/ //Pod 和 容器 的资源请求和限制: spec.containers[].resources.requests.cpu //定义创建容器时预分配的CPU资源 spec.containers[].resources.requests.memory //定义创建容器时预分配的内存资源 spec.containers[].resources.limits.cpu //定义 cpu 的资源上限 spec.containers[].resources.limits.memory //定义内存的资源上限CPU 资源单位
![0](/i/l/?n=23&i=blog/2964696/202302/2964696-20230223185949330-1720274879.png)
内存 资源单位
内存的 request 和 limit 以字节为单位。可以以整数表示,或者以10为底数的指数的单位(E、P、T、G、M、K)来表示, 或者以2为底数的指数的单位(Ei、Pi、Ti、Gi、Mi、Ki)来表示。 如:1KB=10^3=1000,1MB=10^6=1000000=1000KB,1GB=10^9=1000000000=1000MB 1KiB=2^10=1024,1MiB=2^20=1048576=1024KiB PS:在买硬盘的时候,操作系统报的数量要比产品标出或商家号称的小一些,主要原因是标出的是以 MB、GB为单位的,1GB 就是1,000,000,000Byte,而操作系统是以2进制为处理单位的,因此检查硬盘容量时是以MiB、GiB为单位,1GiB=2^30=1,073,741,824,相比较而言,1GiB要比1GB多出1,073,741,824-1,000,000,000=73,741,824Byte,所以检测实际结果要比标出的少一些。 示例1:1 apiVersion: v1 2 kind: Pod 3 metadata: 4 name: frontend 5 spec: 6 containers: 7 - name: app 8 image: images.my-company.example/app:v4 9 env: 10 - name: MYSQL_ROOT_PASSWORD 11 value: "password" 12 resources: 13 requests: 14 memory: "64Mi" 15 cpu: "250m" 16 limits: 17 memory: "128Mi" 18 cpu: "500m" 19 - name: log-aggregator 20 image: images.my-company.example/log-aggregator:v6 21 resources: 22 requests: 23 memory: "64Mi" 24 cpu: "250m" 25 limits: 26 memory: "128Mi" 27 cpu: "500m"此例子中的 Pod 有两个容器。每个容器的 request 值为 0.25 cpu 和 64MiB 内存,每个容器的 limit 值为 0.5 cpu 和 128MiB 内存。那么可以认为该 Pod 的总的资源 request 为 0.5 cpu 和 128 MiB 内存,总的资源 limit 为 1 cpu 和 256MiB 内存。 示例2:
1 vim pod2.yaml 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 name: frontend 6 spec: 7 containers: 8 - name: web 9 image: nginx 10 env: 11 - name: WEB_ROOT_PASSWORD 12 value: "password" 13 resources: 14 requests: 15 memory: "64Mi" 16 cpu: "250m" 17 limits: 18 memory: "128Mi" 19 cpu: "500m" 20 - name: db 21 image: mysql 22 env: 23 - name: MYSQL_ROOT_PASSWORD 24 value: "abc123" 25 resources: 26 requests: 27 memory: "512Mi" 128 28 cpu: "0.5" 29 limits: 30 memory: "1Gi" 256 31 cpu: "1" 32 33 34 35 36 kubectl apply -f pod2.yaml 37 kubectl describe pod frontend 38 39 kubectl get pods -o wide 40 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 41 frontend 2/2 Running 5 15m 10.244.2.4 node02 <none> <none> 42 43 kubectl describe nodes node02 #由于当前虚拟机有2个CPU,所以Pod的CPU Limits一共占用了50% 44 Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits AGE 45 --------- ---- ------------ ---------- --------------- ------------- --- 46 default frontend 500m (25%) 1 (50%) 128Mi (3%) 256Mi (6%) 16m 47 kube-system kube-flannel-ds-amd64-f4pbp 100m (5%) 100m (5%) 50Mi (1%) 50Mi (1%) 19h 48 kube-system kube-proxy-pj4wp 0 (0%) 0 (0%) 0 (0%) 0 (0%) 19h 49 Allocated resources: 50 (Total limits may be over 100 percent, i.e., overcommitted.) 51 Resource Requests Limits 52 -------- -------- ------ 53 cpu 600m (30%) 1100m (55%) 54 memory 178Mi (4%) 306Mi (7%) 55 ephemeral-storage 0 (0%) 0 (0%)
重启策略(restartPolicy):
当 Pod 中的容器退出时通过节点上的 kubelet 重启容器。适用于 Pod 中的所有容器。 1、Always:当容器终止退出后,总是重启容器,默认策略 2、OnFailure:当容器异常退出(退出状态码非0)时,重启容器;正常退出则不重启容器 3、Never:当容器终止退出,从不重启容器。 #注意:K8S 中不支持重启 Pod 资源,只有删除重建1 kubectl edit deployment nginx-deployment 2 ...... 3 restartPolicy: Always 4 5 6 //示例 7 vim pod3.yaml 8 apiVersion: v1 9 kind: Pod 10 metadata: 11 name: foo 12 spec: 13 containers: 14 - name: busybox 15 image: busybox 16 args: 17 - /bin/sh 18 - -c 19 - sleep 30; exit 3 20 21 22 kubectl apply -f pod3.yaml 23 24 //查看Pod状态,等容器启动后30秒后执行exit退出进程进入error状态,就会重启次数加1 25 kubectl get pods 26 NAME READY STATUS RESTARTS AGE 27 foo 1/1 Running 1 50s 28 29 30 kubectl delete -f pod3.yaml 31 32 vim pod3.yaml 33 apiVersion: v1 34 kind: Pod 35 metadata: 36 name: foo 37 spec: 38 containers: 39 - name: busybox 40 image: busybox 41 args: 42 - /bin/sh 43 - -c 44 - sleep 30; exit 3 45 restartPolicy: Never 46 #注意:跟container同一个级别 47 48 kubectl apply -f pod3.yaml 49 50 //容器进入error状态不会进行重启 51 kubectl get pods -w
健康检查/探针(Probe)
探针是由kubelet对容器执行的定期诊断。探针的三种规则:
![0](/i/l/?n=23&i=blog/2964696/202302/2964696-20230223185949327-265205733.png)
Probe支持三种检查方法:
●exec :在容器内执行指定命令。如果命令退出时返回码为0则认为诊断成功。 ●tcpSocket :对指定端口上的容器的IP地址进行TCP检查(三次握手)。如果端口打开,则诊断被认为是成功的。 ●httpGet :对指定的端口和路径上的容器的IP地址执行HTTPGet请求。如果响应的状态码大于等于200且小于400,则诊断被认为是成功的 每次探测都将获得以下三种结果之一: ●成功:容器通过了诊断。 ●失败:容器未通过诊断。 ●未知:诊断失败,因此不会采取任何行动 官网示例: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/1 //示例1:exec方式 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 labels: 6 test: liveness 7 name: liveness-exec 8 spec: 9 containers: 10 - name: liveness 11 image: k8s.gcr.io/busybox 12 args: 13 - /bin/sh 14 - -c 15 - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 60 16 livenessProbe: 17 exec: 18 command: 19 - cat 20 - /tmp/healthy 21 failureThreshold: 1 22 initialDelaySeconds: 5 23 periodSeconds: 5#initialDelaySeconds:指定 kubelet 在执行第一次探测前应该等待5秒,即第一次探测是在容器启动后的第6秒才开始执行。默认是 0 秒,最小值是 0。 #periodSeconds:指定了 kubelet 应该每 5 秒执行一次存活探测。默认是 10 秒。最小值是 1。 #failureThreshold: 当探测失败时,Kubernetes 将在放弃之前重试的次数。 存活探测情况下的放弃就意味着重新启动容器。就绪探测情况下的放弃 Pod 会被打上未就绪的标签。默认值是 3。最小值是 1。 #timeoutSeconds:探测的超时后等待多少秒。默认值是 1 秒。最小值是 1。(在 Kubernetes 1.20 版本之前,exec 探针会忽略 timeoutSeconds 探针会无限期地 持续运行,甚至可能超过所配置的限期,直到返回结果为止。) 可以看到 Pod 中只有一个容器。kubelet 在执行第一次探测前需要等待 5 秒,kubelet 会每 5 秒执行一次存活探测。kubelet 在容器内执行命令 cat /tmp/healthy 来进行探测。如果命令执行成功并且返回值为 0,kubelet 就会认为这个容器是健康存活的。 当到达第 31 秒时,这个命令返回非 0 值,kubelet 会杀死这个容器并重新启动它
1 vim exec.yaml 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 name: liveness-exec 6 namespace: default 7 spec: 8 containers: 9 - name: liveness-exec-container 10 image: busybox 11 imagePullPolicy: IfNotPresent 12 command: ["/bin/sh","-c","touch /tmp/live ; sleep 30; rm -rf /tmp/live; sleep 3600"] 13 livenessProbe: 14 exec: 15 command: ["test","-e","/tmp/live"] 16 initialDelaySeconds: 1 17 periodSeconds: 3 18 19 kubectl create -f exec.yaml 20 21 kubectl describe pods liveness-exec 22 Events: 23 Type Reason Age From Message 24 ---- ------ ---- ---- ------- 25 Normal Scheduled 51s default-scheduler Successfully assigned default/liveness-exec-pod to node02 26 Normal Pulled 46s kubelet, node02 Container image "busybox" already present on machine 27 Normal Created 46s kubelet, node02 Created container liveness-exec-container 28 Normal Started 45s kubelet, node02 Started container liveness-exec-container 29 Warning Unhealthy 8s (x3 over 14s) kubelet, node02 Liveness probe failed: 30 Normal Killing 8s kubelet, node02 Container liveness-exec-container failed liveness probe,will be restarted 31 32 kubectl get pods -w 33 NAME READY STATUS RESTARTS AGE 34 liveness-exec 1/1 Running 1 85s 35 36 37 //示例2:httpGet方式 38 apiVersion: v1 39 kind: Pod 40 metadata: 41 labels: 42 test: liveness 43 name: liveness-http 44 spec: 45 containers: 46 - name: liveness 47 image: k8s.gcr.io/liveness 48 args: 49 - /server 50 livenessProbe: 51 httpGet: 52 path: /healthz 53 port: 8080 54 httpHeaders: 55 - name: Custom-Header 56 value: Awesome 57 initialDelaySeconds: 3 58 periodSeconds: 3在这个配置文件中,可以看到 Pod 也只有一个容器。initialDelaySeconds 字段告诉 kubelet 在执行第一次探测前应该等待 3 秒。periodSeconds 字段指定了 kubelet 每隔 3 秒执行一次存活探测。kubelet 会向容器内运行的服务(服务会监听 8080 端口)发送一个 HTTP GET 请求来执行探测。如果服务器上 /healthz 路径下的处理程序返回成功代码,则 kubelet 认为容器是健康存活的。如果处理程序返回失败代码,则 kubelet 会杀死这个容器并且重新启动它。 任何大于或等于 200 并且小于 400 的返回代码标示成功,其它返回代码都标示失败。
1 vim httpget.yaml 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 name: liveness-httpget 6 namespace: default 7 spec: 8 containers: 9 - name: liveness-httpget-container 10 image: soscscs/myapp:v1 11 imagePullPolicy: IfNotPresent 12 ports: 13 - name: http 14 containerPort: 80 15 livenessProbe: 16 httpGet: 17 port: http 18 path: /index.html 19 initialDelaySeconds: 1 20 periodSeconds: 3 21 timeoutSeconds: 10 22 23 kubectl create -f httpget.yaml 24 25 kubectl exec -it liveness-httpget -- rm -rf /usr/share/nginx/html/index.html 26 27 kubectl get pods 28 NAME READY STATUS RESTARTS AGE 29 liveness-httpget 1/1 Running 1 2m44s 30 31 32 //示例3:tcpSocket方式 33 apiVersion: v1 34 kind: Pod 35 metadata: 36 name: goproxy 37 labels: 38 app: goproxy 39 spec: 40 containers: 41 - name: goproxy 42 image: k8s.gcr.io/goproxy:0.1 43 ports: 44 - containerPort: 8080 45 readinessProbe: 46 tcpSocket: 47 port: 8080 48 initialDelaySeconds: 5 49 periodSeconds: 10 50 livenessProbe: 51 tcpSocket: 52 port: 8080 53 initialDelaySeconds: 15 54 periodSeconds: 20这个例子同时使用 readinessProbe 和 livenessProbe 探测。kubelet 会在容器启动 5 秒后发送第一个 readinessProbe 探测。这会尝试连接 goproxy 容器的 8080 端口。如果探测成功,kubelet 将继续每隔 10 秒运行一次检测。除了 readinessProbe 探测,这个配置包括了一个 livenessProbe 探测。kubelet 会在容器启动 15 秒后进行第一次 livenessProbe 探测。就像 readinessProbe 探测一样,会尝试连接 goproxy 容器的 8080 端口。如果 livenessProbe 探测失败,这个容器会被重新启动。
1 vim tcpsocket.yaml 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 name: probe-tcp 6 spec: 7 containers: 8 - name: nginx 9 image: soscscs/myapp:v1 10 livenessProbe: 11 initialDelaySeconds: 5 12 timeoutSeconds: 1 13 tcpSocket: 14 port: 8080 15 periodSeconds: 10 16 failureThreshold: 2 17 18 kubectl create -f tcpsocket.yaml 19 20 kubectl exec -it probe-tcp -- netstat -natp 21 Active Internet connections (servers and established) 22 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 23 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro 24
![](/i/l/?n=23&i=blog/2964696/202302/2964696-20230223184250534-775214546.png)
25 kubectl get pods -w 26 NAME READY STATUS RESTARTS AGE 27 probe-tcp 1/1 Running 0 1s 28 probe-tcp 1/1 Running 1 25s #第一次是 init(5秒) + period(10秒) * 2 29 probe-tcp 1/1 Running 2 45s #第二次是 period(10秒) + period(10秒) 重试了两次 30 probe-tcp 1/1 Running 3 65s//示例4:就绪检测
1 vim readiness-httpget.yaml 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 name: readiness-httpget 6 namespace: default 7 spec: 8 containers: 9 - name: readiness-httpget-container 10 image: soscscs/myapp:v1 11 imagePullPolicy: IfNotPresent 12 ports: 13 - name: http 14 containerPort: 80 15 readinessProbe: 16 httpGet: 17 port: 80 18 path: /index1.html 19 initialDelaySeconds: 1 20 periodSeconds: 3 21 livenessProbe: 22 httpGet: 23 port: http 24 path: /index.html 25 initialDelaySeconds: 1 26 periodSeconds: 3 27 timeoutSeconds: 10 28 29 kubectl create -f readiness-httpget.yaml 30 31 //readiness探测失败,无法进入READY状态 32 kubectl get pods 33 NAME READY STATUS RESTARTS AGE 34 readiness-httpget 0/1 Running 0 18s 35 36 kubectl exec -it readiness-httpget sh 37 # cd /usr/share/nginx/html/ 38 # ls 39 50x.html index.html 40 # echo 123 > index1.html 41 # exit 42 43 kubectl get pods 44 NAME READY STATUS RESTARTS AGE 45 readiness-httpget 1/1 Running 0 2m31s 46 47 kubectl exec -it readiness-httpget -- rm -rf /usr/share/nginx/html/index.html 48 49 kubectl get pods -w 50 NAME READY STATUS RESTARTS AGE 51 readiness-httpget 1/1 Running 0 4m10s 52 readiness-httpget 0/1 Running 1 4m15s//示例5:就绪检测2
1 vim readiness-myapp.yaml 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 name: myapp1 6 labels: 7 app: myapp 8 spec: 9 containers: 10 - name: myapp 11 image: soscscs/myapp:v1 12 ports: 13 - name: http 14 containerPort: 80 15 readinessProbe: 16 httpGet: 17 port: 80 18 path: /index.html 19 initialDelaySeconds: 5 20 periodSeconds: 5 21 timeoutSeconds: 10 22 --- 23 apiVersion: v1 24 kind: Pod 25 metadata: 26 name: myapp2 27 labels: 28 app: myapp 29 spec: 30 containers: 31 - name: myapp 32 image: soscscs/myapp:v1 33 ports: 34 - name: http 35 containerPort: 80 36 readinessProbe: 37 httpGet: 38 port: 80 39 path: /index.html 40 initialDelaySeconds: 5 41 periodSeconds: 5 42 timeoutSeconds: 10 43 --- 44 apiVersion: v1 45 kind: Pod 46 metadata: 47 name: myapp3 48 labels: 49 app: myapp 50 spec: 51 containers: 52 - name: myapp 53 image: soscscs/myapp:v1 54 ports: 55 - name: http 56 containerPort: 80 57 readinessProbe: 58 httpGet: 59 port: 80 60 path: /index.html 61 initialDelaySeconds: 5 62 periodSeconds: 5 63 timeoutSeconds: 10 64 --- 65 apiVersion: v1 66 kind: Service 67 metadata: 68 name: myapp 69 spec: 70 selector: 71 app: myapp 72 type: ClusterIP 73 ports: 74 - name: http 75 port: 80 76 targetPort: 80 77 78 kubectl create -f readiness-myapp.yaml 79 80 kubectl get pods,svc,endpoints -o wide 81 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 82 pod/myapp1 1/1 Running 0 3m42s 10.244.2.13 node02 <none> <none> 83 pod/myapp2 1/1 Running 0 3m42s 10.244.1.15 node01 <none> <none> 84 pod/myapp3 1/1 Running 0 3m42s 10.244.2.14 node02 <none> <none> 85 86 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR 87 ...... 88 service/myapp ClusterIP 10.96.138.13 <none> 80/TCP 3m42s app=myapp 89 90 NAME ENDPOINTS AGE 91 ...... 92 endpoints/myapp 10.244.1.15:80,10.244.2.13:80,10.244.2.14:80 3m42s 93 94 95 kubectl exec -it pod/myapp1 -- rm -rf /usr/share/nginx/html/index.html 96 97 //readiness探测失败,Pod 无法进入READY状态,且端点控制器将从 endpoints 中剔除删除该 Pod 的 IP 地址 98 kubectl get pods,svc,endpoints -o wide 99 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 100 pod/myapp1 0/1 Running 0 5m17s 10.244.2.13 node02 <none> <none> 101 pod/myapp2 1/1 Running 0 5m17s 10.244.1.15 node01 <none> <none> 102 pod/myapp3 1/1 Running 0 5m17s 10.244.2.14 node02 <none> <none> 103 104 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR 105 ...... 106 service/myapp ClusterIP 10.96.138.13 <none> 80/TCP 5m17s app=myapp 107 108 NAME ENDPOINTS AGE 109 ...... 110 endpoints/myapp 10.244.1.15:80,10.244.2.14:80 5m17s//启动、退出动作
1 vim post.yaml 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 name: lifecycle-demo 6 spec: 7 containers: 8 - name: lifecycle-demo-container 9 image: soscscs/myapp:v1 10 lifecycle: #此为关键字段 11 postStart: 12 exec: 13 command: ["/bin/sh", "-c", "echo Hello from the postStart handler >> /var/log/nginx/message"] 14 preStop: 15 exec: 16 command: ["/bin/sh", "-c", "echo Hello from the poststop handler >> /var/log/nginx/message"] 17 volumeMounts: 18 - name: message-log 19 mountPath: /var/log/nginx/ 20 readOnly: false 21 initContainers: 22 - name: init-myservice 23 image: soscscs/myapp:v1 24 command: ["/bin/sh", "-c", "echo 'Hello initContainers' >> /var/log/nginx/message"] 25 volumeMounts: 26 - name: message-log 27 mountPath: /var/log/nginx/ 28 readOnly: false 29 volumes: 30 - name: message-log 31 hostPath: 32 path: /data/volumes/nginx/log/ 33 type: DirectoryOrCreate 34 35 kubectl create -f post.yaml 36 37 kubectl get pods -o wide 38 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES 39 lifecycle-demo 1/1 Running 0 2m8s 10.244.2.28 node02 <none> <none> 40 41 kubectl exec -it lifecycle-demo -- cat /var/log/nginx/message 42 Hello initContainers 43 Hello from the postStart handler 44 45 //在 node02 节点上查看 46 [root@node02 ~]# cd /data/volumes/nginx/log/ 47 [root@node02 log]# ls 48 access.log error.log message 49 [root@node02 log]# cat message 50 Hello initContainers 51 Hello from the postStart handler 52 #由上可知,init Container先执行,然后当一个主容器启动后,Kubernetes 将立即发送 postStart 事件。 53 54 //删除 pod 后,再在 node02 节点上查看 55 kubectl delete pod lifecycle-demo 56 57 [root@node02 log]# cat message 58 Hello initContainers 59 Hello from the postStart handler 60 Hello from the poststop handler 61 #由上可知,当在容器被终结之前, Kubernetes 将发送一个 preStop 事件。