Pod 管理命令
创建Pod:
kubectl apply -f pod.yaml
kubectl run nginx --image=nginx
查看Pod:
kubectl get pods
kubectl describe pod <Pod名称>
查看日志:
kubectl logs <Pod名称> [-c CONTAINER]
kubectl logs <Pod名称> [-c CONTAINER] -f
进入容器终端:
kubectl exec <Pod名称> [-c CONTAINER] --bash -it
删除Pod:
kubectl delete <Pod名称>
重启策略:
Always
OnFailure
Never
健康检查:
livenessProbe: 如果检查失败, 杀死容器,然后根据restartPolicy来重启
readinessProbe: 如果检查失败,把Pod从service endpoint中剔除
检查方法:
httpGet
exec
tcpSocket
livenessProbe 案例
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
image: k8s.gcr.io/busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
Init Container
用于初始化工作,执行完就结束,可以理解为一次性任务;支持大部分应用容器配置,但不支持健康检查;优先应用容器执行
•Infrastructure Container:基础容器 – 维护整个Pod网络空间
•InitContainers:初始化容器 --先于业务容器开始执行
•Containers:业务容器 – 并行启动
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
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
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"]