k8s学习 - Pod生命周期管理-2

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"]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值