postStart 、preStop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
[root@k8s-master1 test]# vim vito-demo-po-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: vito-demo-po-nginx
labels:
type: app
version: 1.0.0
namespace: 'default'
spec:
terminationGracePeriodSeconds: 30 # 默认值30秒,配置Pod从触发终止操作到强制终止Pod之前等待的宽限时长
containers:
- name: vito-demo-c-nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
lifecycle: # 容器生命周期配置
postStart: # 启动阶段做的事情,不一定在容器的 command 之前运行
exec:
command:
- sh
- -c
- "echo 'postStart...' > /info.log"
preStop: # 结束阶段做的事情,例如:数据清理、数据销毁、服务主动从注册中心下线......
exec:
command:
- sh
- -c
- "sleep 20; echo 'preStop...'"
command:
- nginx
- -g
- 'daemon off;'
workingDir: /usr/share/nginx/html
ports:
- name: http
containerPort: 80
protocol: TCP
- name: https
containerPort: 443
env:
- name: JVM_OPTS
value: '-Xms128m -Xmx128m'
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi
restartPolicy: OnFailure
[root@k8s-master1 test]# kubectl create -f vito-demo-po-nginx.yaml
pod/vito-demo-po-nginx created
[root@k8s-master1 test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
vito-demo-po-nginx 1/1 Running 0 4s 10.244.37.207 k8s-node1.zhch.lan <none> <none>
[root@k8s-master1 test]# time kubectl delete po vito-demo-po-nginx
pod "vito-demo-po-nginx" deleted
real 0m20.685s
user 0m0.071s
sys 0m0.033s
# delete 期间查看 pod
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
vito-demo-po-nginx 1/1 Terminating 0 12s 10.244.37.207 k8s-node1.zhch.lan <none> <none>
Init Containers
-
https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/init-containers/
-
Init Containers 与普通的容器非常像,除了如下两点:
-
它们总是运行到完成。
-
每个都必须在下一个启动之前成功完成。
-
-
例子
- 定义了一个具有 2 个 Init 容器的简单 Pod。
- 第一个等待
myservice
启动 - 第二个等待
mydb
启动。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@k8s-master1 test]# vim myapp-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
initContainers:
- name: init-myservice
image: busybox:1.28
imagePullPolicy: IfNotPresent
command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
- name: init-mydb
image: busybox:1.28
imagePullPolicy: IfNotPresent
command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
containers:
- name: myapp-container
image: busybox:1.28
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
[root@k8s-master1 test]# kubectl create -f myapp-pod.yaml
pod/myapp-pod created
# Pod 处于 init 阶段
[root@k8s-master1 test]# kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 21s
1
2
3
4
5
6
7
8
9
10
11
# 第一个 Init Container 运行中 ...
[root@k8s-master1 test]# kubectl logs myapp-pod -c init-myservice
nslookup: can't resolve 'myservice.default.svc.cluster.local'
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
waiting for myservice
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
nslookup: can't resolve 'myservice.default.svc.cluster.local'
1
2
3
# 第一个 Init Container 运行完成之前,第二个 Init Container 不会开始运行
[root@k8s-master1 test]# kubectl logs myapp-pod -c init-mydb
Error from server (BadRequest): container "init-mydb" in pod "myapp-pod" is waiting to start: PodInitializing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 创建并运行 myservice
[root@k8s-master1 test]# vim myservice.yaml
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
[root@k8s-master1 test]# kubectl create -f myservice.yaml
service/myservice created
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 第一个 Init Container 运行完成
[root@k8s-master1 test]# kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:1/2 0 5m4s
# 第二个 Init Container 开始运行
[root@k8s-master1 test]# kubectl logs myapp-pod -c init-mydb
nslookup: can't resolve 'mydb.default.svc.cluster.local'
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
waiting for mydb
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
waiting for mydb
nslookup: can't resolve 'mydb.default.svc.cluster.local'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 创建并运行 mydb
[root@k8s-master1 test]# vim mydb.yaml
apiVersion: v1
kind: Service
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
[root@k8s-master1 test]# kubectl create -f mydb.yaml
service/mydb created
1
2
3
4
5
6
7
8
# 所有 Pod.spec.initContainers 运行完成,Pod.spec.containers 开始运行
[root@k8s-master1 test]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-pod 1/1 Running 0 3m21s 10.244.8.245 k8s-node2.zhch.lan <none> <none>
[root@k8s-master1 test]# kubectl logs myapp-pod
Defaulted container "myapp-container" out of: myapp-container, init-myservice (init), init-mydb (init)
The app is running!
1
2
3
4
5
6
[root@k8s-node2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2d6ae6e7746 8c811b4aec35 "sh -c 'echo The app…" 6 minutes ago Up 6 minutes k8s_myapp-container_myapp-pod_default_0ba72446-7aaf-4a4c-bf94-f0b2b1bb5594_0
5a419815ecc3 8c811b4aec35 "sh -c 'until nslook…" 8 minutes ago Exited (0) 7 minutes ago k8s_init-mydb_myapp-pod_default_0ba72446-7aaf-4a4c-bf94-f0b2b1bb5594_0
65ce33dc07e4 8c811b4aec35 "sh -c 'until nslook…" 10 minutes ago Exited (0) 8 minutes ago k8s_init-myservice_myapp-pod_default_0ba72446-7aaf-4a4c-bf94-f0b2b1bb5594_0
feaf1ece08b1 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.8 "/pause" 10 minutes ago Up 10 minutes k8s_POD_myapp-pod_default_0ba72446-7aaf-4a4c-bf94-f0b2b1bb5594_0