Kubernetes-Pod进阶
目录:
Pod 进阶

资源限制
CPU 资源单位

内存 资源单位
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"
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):
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)
探针的三种规则:

Probe支持三种检查方法:
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
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
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
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
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
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
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 事件。
总结
Kubernetes-Pod进阶的更多相关文章
- 第25 章 : Kubernetes 网络模型进阶
Kubernetes 网络模型进阶 本文将主要分享以下五个方面的内容: Kubernetes 网络模型来龙去脉 Pod 究竟如何上网? Service 究竟怎么工作? 啥?负载均衡还分内部外部? 思考 ...
- Kubernetes Pod 驱逐详解
原文链接:Kubernetes Pod 驱逐详解 在 Kubernetes 中,Pod 使用的资源最重要的是 CPU.内存和磁盘 IO,这些资源可以被分为可压缩资源(CPU)和不可压缩资源(内存,磁盘 ...
- Kubernetes Pod 镜像拉取策略
Kubernetes Pod 镜像拉取策略 官方文档:https://kubernetes.io/docs/concepts/containers/images/ • IfNotPresent:默认值 ...
- Kubernetes Pod 资源限制
Kubernetes Pod 资源限制 官方文档:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources- ...
- Kubernetes Pod 调度约束
Kubernetes Pod 调度约束 可以将pod调度到指定的节点Node内 默认:根据节点资源利用率等分配Node节点. nodeName用于将Pod调度到指定的Node名称上 nodeSelec ...
- Kubernetes Pod故障归类与排查方法
Pod概念 Pod是kubernetes集群中最小的部署和管理的基本单元,协同寻址,协同调度. Pod是一个或多个容器的集合,是一个或一组服务(进程)的抽象集合. Pod中可以共享网络和存储(可以简单 ...
- Python Django撸个WebSSH操作Kubernetes Pod(下)- 终端窗口自适应Resize
追求完美不服输的我,一直在与各种问题斗争的路上痛并快乐着 上一篇文章Django实现WebSSH操作Kubernetes Pod最后留了个问题没有解决,那就是terminal内容窗口的大小没有办法调整 ...
- Kubernetes Pod 全面知识
Pod 是在 Kubernetes 中创建和管理的.最小的可部署的计算单元,是最重要的对象之一.一个 Pod 中包含一个或多个容器,这些容器在 Pod 中能够共享网络.存储等环境. 学习 Kubern ...
- 6、Kubernetes Pod控制器应用进阶
定义pod时,在spec字段中常用的定义字段有哪些? master ~]# kubectl explain pods.spec.containers KIND: Pod VERSION: v1 RES ...
- Kubernetes的Pod进阶(十一)
一.Lifecycle 官网:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/ 通过前面的分享,关于pod是什么相信看 ...
随机推荐
- linux下删除文件夹的软链接时注意千万不能在后面加反斜杠,千万不要用强制删除,否则下面2种场景,你会把源文件删除,要闯祸的
今天遇到一个坑,自己在子目录下创建了父目录的软链接,导致可以无限循环进入父目录 [clouder@ana53 dir1]$ ll total 8 -rw-rw-r-- 1 clouder cloude ...
- python Gui编程工具详解:beeware
各个gui开发工具对比 Flexx: 可以使用Flexx创建桌面应用程序和web应用程序,同时可以将程序导出到独立的HTML文档中,GitHub推荐 Kivy&BeeWare: 只需编写一套代 ...
- 生物标记钙卫蛋白在RA诊疗中的应用研究
标签: 类风湿关节炎; 预测因子; 钙卫蛋白; S100A9 [文献摘记]生物标记钙卫蛋白在RA诊疗中的应用研究 电邮发布日期:2016年4月20日 不断提升精确度和应用范围的组学技术正在帮助包括类风 ...
- filter 和 map 的异同
filter是满足条件的留下,是对原数组的过滤:map则是对原数组的加工,映射成一一映射的新数组 filter() Filter() 创建一个新数组,新数组中返回的值为经过检查指定数组中满足条件的所有 ...
- Postgresql索引浅析
一.摘要 1.索引是提高数据库性能的常用途径.比起没有索引,使用索引可以让数据库服务器更快找到并获取特定行.但是索引同时也会增加数据库系统的日常管理负担,因此我们应该聪明地使用索引. 2.索引其实就是 ...
- 添加weui-miniprogram
1.打开根目录 npm init npm install weui-miniprogram --save 2.打开project.config.json 设置 "packNpmManuall ...
- Django中多数据库的配置,实现分库分表,主从复制,读写分离
在django项目中, 一个工程中存在多个APP应用很常见. 有时候希望不同的APP连接不同的数据库,这个时候需要建立多个数据库连接. 1. 修改项目的 settings 配置 在 settings. ...
- JavaWeb中的Servlet
Servlet 目录 Servlet 一.互联网中的资源 二.Servlet 2.1.Servlet的作用 2.2.Servlet执行流程 2.3.Servlet生命周期 2.4.Servlet的继承 ...
- Linux学习之文件目录指令(部分)
包括了 ls pwd cd mkdir rmdir touch cp rm mv cat echo more less head tail > >>指 ...
- .NET 中创建录音机和播放器应用
前言 在本博客中,你将了解如何在 .NET MAUI 中开发录音机和播放器.音频播放器将录制和播放音频文件.此应用程序可以在Android和iOS上部署和使用. 预览 以下是该录音机和播放录音的应用程 ...