# kubectl explain pod.spec.containers
spec.containers <[]object>
- name <string>
  image <string>
  imagePullPolicy: <string>  #如果标签是latest则默认值是Always,如果是其他标签则默认值是IfNotPresent
    Always:总是去仓库下载,latest标签的镜像用
    Never:本地有就用,没有就不用
    IfNotPresent:本地有用本地的,本地没有去仓库下载
  ports:
  - name: http
    containerPort: 80
  - name: https
    containerPort: 443
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
# kubectl get pods --show-labels  #查看所有pod的标签
# kubectl get pods --show-labels -L app  #显示拥有app标签的值
# kubectl get pods --show-labels -L app,run  #显示多个标签的标签值
# kubectl get pods --show-labels -l app  #过滤拥有app标签的pod
# kubectl get pods --show-labels -l app=myapp  #基于等值的标签选择器(=, ==, !=)
# kubectl get pods --show-labels -l "app in (myapp,noapp)"  #基于集合关系的标签选择器(in, ontin)
# kubectl label pods pod-demo release=canary  #给pod打标
# kubectl label pods pod-demo release=stable --overwrite  #修改标签的值
# kubectl get nodes --show-labels  #基于nodeSelector节点选择器
# kubectl label node node01 disktype=ssd  #给node01增加disktype=ssd的标签
spec:
  nodeSelector:  #使其pod只能运行在拥有disktype=ssd标签的node上
    disktype: ssd
spec:
  nodeName: node01  #使其pod只能运行在node01上
metadata:
  annotations:
    dongfei.tech/created-by: "cluster admin"
# kubectl describe pods pod-demo |grep Annotations
spec:
  restartPolicy:
    Always:默认,总是重启
    OnFailure:Pod失败则会重启
    Never:不会重启
# cat liveness-exec.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c","touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 3600"]
    livenessProbe:
      exec:
        command: ["test","-e","/tmp/healthy"]  #探测命令
      initialDelaySeconds: 1  #初始化延迟时间,默认0s
      periodSeconds: 3  #隔多长时间探测一次,默认10s
      failureThreshold: 3  #探测失败3次为失败,默认3次
      successThreshold: 1  #探测成功1次为成功
  restartPolicy: Always  #探测失败时的重启策略
# kubectl get pods -w  #监控POD状态
# kubectl describe pods liveness-exec-pod |grep "Restart Count"  #查看Pod重启次数
# cat liveness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: liveness-httpget-container
    image: dongfeimg/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    livenessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
# kubectl exec -it liveness-httpget-pod -- /bin/sh  #手动连入pod
/ # rm -f /usr/share/nginx/html/index.html  #删除index.html文件,探测失败会重启
# cat readiness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
  - name: readiness-httpget-container
    image: dongfeimg/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    readinessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
# cat poststart-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: poststart-pod
  namespace: default
spec:
  containers:
  - name: busybox-httpd
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ["mkdir","-p","/data/web/html"]  #在command命令后执行此命令
    command: ["/bin/sh","-c","sleep 3600"]
原文:https://www.cnblogs.com/L-dongf/p/11158763.html