文章目录
Pod生命周期
下面是使用 Init 容器的示例
vim init.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busyboxplus
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busyboxplus
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
- name: init-mydb
image: busyboxplus
command: ['sh', '-c', "until nslookup mydb.default.svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
“until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done”]
执行nslookup指令解析myservice.default.svc.cluster.local这个地址,如果解析成功则继续,不成功则一直处于初始化状态,循环do里面的内容。
由于没有加入服务,则会一直循环
将init.yaml中的配置应用到pod
kubectl apply -f init.yaml
显示运行中的Pod
kubectl get pod
加入服务
vim init.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busyboxplus
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busyboxplus
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
- name: init-mydb
image: busyboxplus
command: ['sh', '-c', "until nslookup mydb.default.svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
显示运行中的Pod,初始化完成,status显示running
将pod删除
kubectl delete -f init.yaml
下面是使用livenessProbe(存活态探针)的示例
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
# hostNetwork: true
# nodeName: server4
#imagePullSecrets:
containers:
- name: myapp
image: myapp