1、 pod生命周期简介
pause容器提供pod最底层的环境 初始化容器必须按照顺序执行,第一个启动成功后退出,第二个才能运行 初始化完成后进入主容器阶段,主容器启动之后怎么判断状态为running 里面有存活探针为liveness,探针判定容器为检测通过,容器就是running rediness为就绪探针,容器运行成功了,但是比如里面80端口有没有冲突,有没有正常对外发布服务如
2、init初始化容器
官方文档例子
[root@server2 ~]# vim myapp.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
initContainers: 初始化容器
- name: init-myservice
image: busybox:latest
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"] 表示dns解析默认namespace service服务 cluster.local表示本地集群,如果解析不成功,休眠2s继续解析,直到解析成功为止,do后面是输出的信息
containers:
- name: myapp
image: myapp:v1
imagePullPolicy: IfNotPresent
[root@server2 ~]# kubectl apply -f myapp.yaml 运行
pod/myapp created
[root@server2 ~]# kubectl get pod 查看pod
NAME READY STATUS RESTARTS AGE
myapp 0/1 Init:0/1 0 38s 显示当前有一个初始化容器,还没有启动起来,需要检测myservice服务
[root@server2 ~]# vim myservice.yaml 创建服务
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80 外部端口
targetPort: 80 容器端口
[root@server2 ~]# kubectl apply -f myservice.yaml 运行
service/myservice created
[root@server2 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d19h
myservice ClusterIP 10.103.223.174 <none> 80/TCP 35s 可以发现创建了一个myservice服务
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 9m47s 此时初始化容器运行成功后退出,主容器开始运行
[root@server2 ~]# kubectl run demo --image=busybox -it --restart=Never
/ # cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local 有解析
options ndots:5
/ # ping myservice
PING myservice (10.103.223.174): 56 data bytes 可以解析到ip地址
3、 探针
存活探针
探针官方文档例子 通过tcp协议检测80端口:
[root@server2 ~]# vim myapp.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
# initContainers:
# - name: init-myservice
# image: busybox:latest
# command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
containers:
- name: myapp
image: myapp:v1
imagePullPolicy: IfNotPresent
livenessProbe:
tcpSocket: 表示tcp协议检测80端口
port: 8080 专门写一个错的,为了实验效果
initialDelaySeconds: 2 表示等容器启动之后延迟2s去检测80端口开没开
periodSeconds: 3 表示检测的间隔为3s
timeoutSeconds: 1 表示检测的超时
[root@server2 ~]# kubectl apply -f myapp.yaml 运行
pod/myapp created
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 0/1 CrashLoopBackOff 6 (64s ago) 4m37s 可以发现存活探针检测不过,连接8080端口失败
[root@server2 ~]# kubectl delete -f myapp.yaml 删除
[root@server2 ~]# vim myapp.yaml
pod "myapp" deleted
kind: Pod
metadata:
name: myapp
spec:
# initContainers:
# - name: init-myservice
# image: busybox:latest
# command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
containers:
- name: myapp
image: myapp:v1
imagePullPolicy: IfNotPresent
livenessProbe:
tcpSocket:
port: 80 将端口改成80
initialDelaySeconds: 2
periodSeconds: 3
timeoutSeconds: 1
[root@server2 ~]# kubectl apply -f myapp.yaml 运行
[root@server2 ~]# kubectl get pod 此时就可以运行了
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 2m17s
[root@server2 ~]# kubectl delete -f myapp.yaml 删除
pod "myapp" deleted
就绪探针
[root@server2 ~]# vim myapp.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
# initContainers:
# - name: init-myservice
# image: busybox:latest
# command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
containers:
- name: myapp
image: myapp:v1
imagePullPolicy: IfNotPresent
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 2
periodSeconds: 3
timeoutSeconds: 2
readinessProbe:
httpGet:
path: /test.html
port: 80 表示访问80端口,然后去拿/test.html这个发布页面,有就是就绪了,没有就是没有就绪
initialDelaySeconds: 2
periodSeconds: 3
timeoutSeconds: 2
[root@server2 ~]# kubectl apply -f myapp.yaml 运行
pod/myapp created
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 0/1 Running 0 21s 可以发现存活探针已经通过,就绪探针没有通过
[root@server2 ~]# kubectl exec -it myapp -- sh 进入容器执行
/ # cd /usr/share/
/usr/share # cd nginx/html/ 进入nginx默认发布目录
/usr/share/nginx/html # ls
50x.html index.html
/usr/share/nginx/html # echo westos > test.html 创建 test.html 发布页面
/usr/share/nginx/html # [root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 15m 就绪探针已经通过
[root@server2 ~]# kubectl get pod -o wide
NAME READY(就绪探针看此) STATUS (存活探针看此) RESTARTS(重启此数) AGE IP NODE NOMINATED NODE READINESS GATES
myapp 0/1 Running 0 4m4s 10.244.1.19 server3 <none> <none> 容器在server3上运行
[root@server2 ~]# curl 10.244.1.19/test.html
westos
|