名称空间,亲和性,pod生命周期,健康检查

河曲智叟  金牌会员 | 2024-6-17 03:30:20 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 526|帖子 526|积分 1578

一、名称空间

1、切换名称空间
  1. [root@master pod]# kubectl create ns test
  2. namespace/test created
  3. [root@master pod]# kubectl get ns
  4. NAME              STATUS   AGE
  5. default           Active   10h
  6. kube-node-lease   Active   10h
  7. kube-public       Active   10h
  8. kube-system       Active   10h
  9. test              Active   2s
  10. [root@master pod]# kubectl config set-context --current --namespace=kube-system
  11. Context "kubernetes-admin@kubernetes" modified.
  12. [root@master pod]# kubectl get pod
  13. NAME                                      READY   STATUS    RESTARTS        AGE
  14. calico-kube-controllers-d886b8fff-mbdz7   1/1     Running   0               6h42m
  15. calico-node-48tnk                         1/1     Running   0               6h46m
  16. calico-node-jq7mr                         1/1     Running   0               6h46m
  17. calico-node-pdwcr                         1/1     Running   0               6h46m
  18. coredns-567c556887-99cqw                  1/1     Running   1 (6h44m ago)   10h
  19. coredns-567c556887-9sbfp                  1/1     Running   1 (6h44m ago)   10h
  20. etcd-master                               1/1     Running   1 (6h44m ago)   10h
  21. kube-apiserver-master                     1/1     Running   1 (6h44m ago)   10h
  22. kube-controller-manager-master            1/1     Running   1 (6h44m ago)   10h
  23. kube-proxy-7dl5r                          1/1     Running   1 (6h50m ago)   10h
  24. kube-proxy-pvbrg                          1/1     Running   1 (6h44m ago)   10h
  25. kube-proxy-xsqt9                          1/1     Running   1 (6h50m ago)   10h
  26. kube-scheduler-master                     1/1     Running   1 (6h44m ago)   10h
  27. [root@master pod]# kubectl config set-context --current --namespace=default
  28. Context "kubernetes-admin@kubernetes" modified.
  29. [root@master pod]# kubectl get pod
  30. NAME     READY   STATUS    RESTARTS   AGE
  31. nginx1   1/1     Running   0          8m44s
复制代码
2、设置名称空间资源限额


  • 就是不能超过这个名称空间的限制
  • 限制这个名称空间全部pod的类型的限制
  1. [root@master ns]# cat test.yaml
  2. apiVersion: v1
  3. kind: ResourceQuota  #这个是资源配额
  4. metadata:
  5.   name: mem-cpu-qutoa
  6.   namespace: test  
  7. spec:
  8.   hard:  #限制资源
  9.      requests.cpu: "2" #最少2个cpu
  10.      requests.memory: 2Gi
  11.      limits.cpu: "4"   #最大4个cpu
  12.      limits.memory: 4Gi
  13. #查看名称空间详细信息
  14. [root@master ns]# kubectl describe ns test
  15. Name:         test
  16. Labels:       kubernetes.io/metadata.name=test
  17. Annotations:  <none>
  18. Status:       Active
  19. Resource Quotas
  20.   Name:            mem-cpu-qutoa
  21.   Resource         Used  Hard
  22.   --------         ---   ---
  23.   limits.cpu       0     4
  24.   limits.memory    0     4Gi
  25.   requests.cpu     0     2
  26.   requests.memory  0     2Gi
  27. No LimitRange resource.
  28. #定义了名称空间限制的话,创建Pod必须设置资源限制,否则会报错
  29. [root@master pod]# cat nginx.yaml
  30. apiVersion: v1
  31. kind: Pod
  32. metadata:
  33.   name: nginx1
  34.   namespace: test
  35.   labels:
  36.     app: nginx-pod
  37. spec:
  38. containers:
  39. - name: nginx01
  40.    image: docker.io/library/nginx:1.9.1
  41.    imagePullPolicy: IfNotPresent
  42.    resources:  #pod资源的限制,如果不做限制的话,pod出现了问题的话,一直吃内存的话,就会出现问题
  43.      limits:
  44.        memory: "2Gi"  #内存为2g
  45.        cpu: "2m"  #单位为毫核,1000m=1核      
复制代码
二、标签


  • 这个非常的重要,由于很多的资源类型都是靠这个标签进行管理的(识别到了)
  • 服务或者控制器等都是靠这个标签来进行管理的
  1. #打上标签
  2. [root@master /]# kubectl label pods nginx1 test=01
  3. pod/nginx1 labeled
  4. [root@master /]# kubectl get pod --show-labels
  5. NAME     READY   STATUS    RESTARTS   AGE   LABELS
  6. nginx1   1/1     Running   0          45m   app=nginx-pod,test=01
  7. #具有这个标签的pod进行列出
  8. [root@master /]# kubectl get pods -l app=nginx-pod
  9. NAME     READY   STATUS    RESTARTS   AGE
  10. nginx1   1/1     Running   0          48m
  11. #查看所有名称空间和标签
  12. [root@master /]# kubectl get pods --all-namespaces --show-labels
  13. #查看这个键app对应的值是什么
  14. [root@master /]# kubectl get pods -L app
  15. NAME     READY   STATUS    RESTARTS   AGE   APP
  16. nginx1   1/1     Running   0          50m   nginx-pod
  17. #删除这个标签
  18. [root@master ~]# kubectl label pod nginx1 app-
  19. pod/nginx1 unlabeled
  20. [root@master ~]# kubectl get pod --show-labels
  21. NAME     READY   STATUS    RESTARTS   AGE   LABELS
  22. nginx1   1/1     Running   0          57m   test=01
  23. s
复制代码
三、亲和性

1、node节点选择器

就是根据主机名或者标签进行pod的调度,属于强制性的调度,不存在的也能进行调度,是pending的状态
1、nodename
  1. [root@master pod]# cat pod1.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: pod1
  6.   namespace: test
  7. spec:
  8.   nodeName: node1  #调度到node1主机上面
  9.   containers:
  10.     - name: pod1
  11.       image: docker.io/library/nginx
  12.       imagePullPolicy: IfNotPresent
  13. [root@master pod]# kubectl get pod -n test -o wide
  14. NAME     READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
  15. nginx1   1/1     Running   0          12h   10.244.104.5     node2   <none>           <none>
  16. pod1     1/1     Running   0          34s   10.244.166.130   node1   <none>           <none>
复制代码
2、nodeselector
  1. #给主机名打上标签,以便进行调度
  2. [root@master ~]# kubectl label nodes node1 app=node1
  3. node/node1 labeled
  4. [root@master ~]# kubectl get nodes node1 --show-labels
  5. NAME    STATUS   ROLES    AGE   VERSION   LABELS
  6. node1   Ready    <none>   23h   v1.26.0   app=node1,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux
  7. [root@master pod]# cat pod2.yaml
  8. apiVersion: v1
  9. kind: Pod
  10. metadata:
  11.   name: pod2
  12.   namespace: test
  13. spec:
  14.   nodeSelector:  #根据主机名的标签进行调度
  15.     app: node1   #这种键值的形式来表现出来
  16.   containers:
  17.   - name: pod2
  18.     image: docker.io/library/nginx
  19.     imagePullPolicy: IfNotPresent   
  20. [root@master pod]# kubectl get pod -n test -o wide
  21. NAME     READY   STATUS    RESTARTS   AGE     IP               NODE    NOMINATED NODE   READINESS GATES
  22. nginx1   1/1     Running   0          12h     10.244.104.5     node2   <none>           <none>
  23. pod1     1/1     Running   0          9m28s   10.244.166.130   node1   <none>           <none>
  24. pod2     1/1     Running   0          12s     10.244.166.131   node1   <none>           <none>
复制代码
2、node亲和性


  • 根据node上面的标签进行调度
  • 根据的是node和pod之间的关系进行调度的
1、软亲和性


  • 如果没有符合条件的,就随机选择一个进行调度
  1. [root@master pod]# cat pod4.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: pod4
  6.   namespace: test
  7. spec:
  8.   affinity:
  9.     nodeAffinity:
  10.       preferredDuringSchedulingIgnoredDuringExecution:
  11.       - preference:
  12.           matchExpressions:   #匹配节点上面的标签
  13.           - key: app
  14.             operator: In
  15.             values: ["node1"]
  16.         weight: 1   #根据权重来调度
  17.   containers:
  18.   - name: pod4
  19.     image: docker.io/library/nginx
  20.     imagePullPolicy: IfNotPresent
  21. [root@master pod]# kubectl get pod -n test -o wide
  22. NAME   READY   STATUS    RESTARTS   AGE     IP               NODE    NOMINATED NODE   READINESS GATES
  23. pod3   1/1     Running   0          6m52s   10.244.166.133   node1   <none>           <none>
  24. pod4   1/1     Running   0          40s     10.244.166.135   node1   <none>           <none>
复制代码
2、硬亲和性
  1. [root@master pod]# cat pod3.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: pod3
  6.   namespace: test
  7. spec:
  8.   affinity:
  9.     nodeAffinity:
  10.       requiredDuringSchedulingIgnoredDuringExecution:  #硬限制
  11.         nodeSelectorTerms:   #根据这个node上面的标签来进行调度
  12.         - matchExpressions:
  13.           - key: app
  14.             operator: In
  15.             values: ["node1"]   #调度到上面有app=node1这个标签的节点上面去
  16.   containers:
  17.   - name: pod3
  18.     image: docker.io/library/nginx:1.9.1
  19.     imagePullPolicy: IfNotPresent
复制代码
3、pod亲和性


  • 就是几个pod之间有依赖的关系,就放在一起,如许效率就快一点,网站服务和数据库服务就需要在一起,进步效率
  • 根据正在运行的pod上面的标签进行调度
1、软亲和性
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   name: pod7
  5.   namespace: test
  6. spec:
  7.   affinity:
  8.     podAffinity:
  9.       preferredDuringSchedulingIgnoredDuringExecution:
  10.       - podAffinityTerm:
  11.           labelSelector:
  12.             matchExpressions:
  13.             - key: app
  14.               operator: In
  15.               values: ["pod4"]
  16.           topologyKey: app
  17.         weight: 1
  18.   containers:
  19.   - name: pod7
  20.     image: docker.io/library/nginx
  21.     imagePullPolicy: IfNotPresent
  22. [root@master pod]# kubectl get pod -n test -o wide
  23. NAME   READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
  24. pod4   1/1     Running   0          24m   10.244.166.136   node1   <none>           <none>
  25. pod5   1/1     Running   0          21m   10.244.166.137   node1   <none>           <none>
  26. pod7   1/1     Running   0          51s   10.244.166.139   node1   <none>           <none>
复制代码
2、硬亲和性
  1. [root@master pod]# cat pod5.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: pod5
  6.   namespace: test
  7. spec:
  8.   affinity:
  9.     podAffinity:
  10.       requiredDuringSchedulingIgnoredDuringExecution:
  11.       - labelSelector:
  12.           matchExpressions:
  13.           - key: app
  14.             operator: In
  15.             values: ["pod4"]
  16.         topologyKey: kubernetes.io/hostname   #这个就是拓扑域,每个节点的这个都不一样。node1,node2等
  17.   containers:
  18.   - name: pod5
  19.     image: docker.io/library/nginx
  20.     imagePullPolicy: IfNotPresent
  21. #关于这个topologyKey的值的选择,一般就是节点上面的标签
  22. apiVersion: v1
  23. kind: Pod
  24. metadata:
  25.   name: pod6
  26.   namespace: test
  27. spec:
  28.   affinity:
  29.     podAffinity:
  30.       requiredDuringSchedulingIgnoredDuringExecution:
  31.       - labelSelector:
  32.           matchExpressions:
  33.           - key: app
  34.             operator: In
  35.             values: ["pod4"]
  36.         topologyKey: app2  #这个是node2上面的标签,调度到pod包含这个app=pod4这个标签,并且节点是标签是app2上面的节点上面
  37.   containers:
  38.   - name: pod6
  39.     image: docker.io/library/nginx
  40.     imagePullPolicy: IfNotPresent
  41. [root@master pod]# cat pod5.yaml
  42. apiVersion: v1
  43. kind: Pod
  44. metadata:
  45.   name: pod6
  46.   namespace: test
  47. spec:
  48.   affinity:
  49.     podAffinity:
  50.       requiredDuringSchedulingIgnoredDuringExecution:
  51.       - labelSelector:
  52.           matchExpressions:
  53.           - key: app
  54.             operator: In
  55.             values: ["pod4"]
  56.         topologyKey: app  #调度到pod包含了app的标签,并且值在app节点上面去了
  57.   containers:
  58.   - name: pod6
  59.     image: docker.io/library/nginx
  60.     imagePullPolicy: IfNotPresent
  61. # operator: DoesNotExist情况
  62. apiVersion: v1
  63. kind: Pod
  64. metadata:
  65.   name: pod6
  66.   namespace: test
  67. spec:
  68.   affinity:
  69.     podAffinity:
  70.       requiredDuringSchedulingIgnoredDuringExecution:
  71.       - labelSelector:
  72.           matchExpressions:
  73.           - key: app
  74.             operator: DoesNotExist
  75.         topologyKey: app   #调度到key不包含app并且节点标签为app的节点上面,还是调度到app节点上面去了
  76.   containers:
  77.   - name: pod6
  78.     image: docker.io/library/nginx
  79.     imagePullPolicy: IfNotPresent
复制代码
4、pod反亲和性

就是当2个都是占内存比较高的Pod,就使用和这个反亲和性进行分开
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   name: pod8
  5.   namespace: test
  6. spec:
  7.   affinity:
  8.     podAntiAffinity:
  9.       requiredDuringSchedulingIgnoredDuringExecution:
  10.       - labelSelector:
  11.           matchExpressions:
  12.           - key: app
  13.             operator: In
  14.             values: ["pod4"]
  15.         topologyKey: kubernetes.io/hostname   #调度到不能包含app=pod4上面的节点,调度到node1上
  16.   containers:
  17.   - name: pod8
  18.     image: docker.io/library/nginx
  19.     imagePullPolicy: IfNotPresent
  20. [root@master pod]# kubectl get pod -n test -o wide
  21. NAME   READY   STATUS    RESTARTS   AGE     IP               NODE    NOMINATED NODE   READINESS GATES
  22. pod4   1/1     Running   0          36m     10.244.166.136   node1   <none>           <none>
  23. pod5   1/1     Running   0          33m     10.244.166.137   node1   <none>           <none>
  24. pod6   1/1     Running   0          7m42s   10.244.166.140   node1   <none>           <none>
  25. pod7   1/1     Running   0          12m     10.244.166.139   node1   <none>           <none>
  26. pod8   1/1     Running   0          8s      10.244.104.6     node2   <none>           <none>=
复制代码
5、污点


  • 在node上面进行打污点
  • kubectl explain node.spec.taints
  • 手动打污点, kubectl taint nodes node1 a=b:NoSchedule
  • 污点三个等级

    • NoExecute 节点上面的pod都移除掉,不能调度到这个节点上
    • NoSchedule 节点上面存在的pod保留,但是新创建的pod不能调度到这个节点上面
    • PreferNoSchedule pod不到万不得已的环境下,才气调度到这个节点上面

  1. #给node1打上一个污点
  2. [root@master pod]# kubectl get pod -n test -o wide
  3. NAME   READY   STATUS    RESTARTS   AGE     IP               NODE    NOMINATED NODE   READINESS GATES
  4. pod4   1/1     Running   0          41m     10.244.166.136   node1   <none>           <none>
  5. pod5   1/1     Running   0          37m     10.244.166.137   node1   <none>           <none>
  6. pod6   1/1     Running   0          12m     10.244.166.140   node1   <none>           <none>
  7. pod7   1/1     Running   0          17m     10.244.166.139   node1   <none>           <none>
  8. pod8   1/1     Running   0          4m33s   10.244.104.6     node2   <none>           <none>
  9. [root@master pod]# kubectl taint node node1 app=node1:NoExecute
  10. node/node1 tainted
  11. #发现这个节点上面的pod都销毁了
  12. [root@master pod]# kubectl get pod -n test -o wide
  13. NAME   READY   STATUS    RESTARTS   AGE     IP             NODE    NOMINATED NODE   READINESS GATES
  14. pod8   1/1     Running   0          6m21s   10.244.104.6   node2   <none>           <none>
  15. #去除污点
  16. [root@master pod]# kubectl taint node node1 app-
  17. node/node1 untainted
  18. [root@master pod]# kubectl describe node node1 | grep -i taint
  19. Taints:             <none>
复制代码
6、容忍度


  • 在pod上面进行容忍度,就是会容忍node上面的污点,从而能进行调度
  • kubectl explain pod.spec.tolerations
  1. #就是节点上面有污点但是pod上面有容忍度可以容忍这个污点来进行调度到指定的节点上面去
  2. #给node1打上污点
  3. [root@master pod]# kubectl taint node node1 app=node1:NoExecute
  4. node/node1 tainted
  5. #进行调度到node1上
  6. apiVersion: v1
  7. kind: Pod
  8. metadata:
  9.   name: pod10
  10.   namespace: test
  11. spec:
  12.   tolerations:
  13.   - key: "app"
  14.     operator: Equal  #就是key和values,effect必须和node上面完全匹配才行 #exists,只要对应的键是存在的,其值被自动定义成通配符
  15.     value: "node1"
  16.     effect: NoExecute
  17.   containers:
  18.   - name: pod10
  19.     image: docker.io/library/nginx:1.9.1  
  20. [root@master pod]# kubectl get pod -n test -o wide
  21. NAME    READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
  22. pod10   1/1     Running   0          58s   10.244.166.142   node1   <none>           <none>
  23. pod8    1/1     Running   0          27m   10.244.104.6     node2   <none>           <none>
  24. apiVersion: v1
  25. kind: Pod
  26. metadata:
  27.   name: pod11
  28.   namespace: test
  29. spec:
  30.   tolerations:
  31.   - key: "app"
  32.     operator: Exists   #容忍无论app,NoExecute的值为多少,都能进行调度
  33.     value: ""
  34.     effect: NoExecute
  35.   containers:
  36.   - name: pod11
  37.     image: docker.io/library/nginx:1.9.1  
复制代码
四:pod的生命周期



  • init容器,初始化的容器,就是必须要经过这个阶段才气运行主容器
  • 主容器,里面有启动前钩子和启动后钩子
1、初始化容器
  1. [root@master pod]# cat init.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name:  init-pod
  6.   namespace: test
  7. spec:
  8.   initContainers:
  9.   - name: init-pod1
  10.     image: docker.io/library/nginx:1.9.1
  11.     command: ["/bin/bash","-c","touch /11.txt"]
  12.   containers:
  13.   - name: main-pod
  14.     image: docker.io/library/nginx:1.9.1
  15. [root@master pod]# kubectl get pod -n test -w
  16. NAME       READY   STATUS    RESTARTS   AGE
  17. init-pod   0/1     Pending   0          0s
  18. init-pod   0/1     Pending   0          0s
  19. init-pod   0/1     Init:0/1   0          0s
  20. init-pod   0/1     Init:0/1   0          1s
  21. init-pod   0/1     PodInitializing   0          2s
  22. init-pod   1/1     Running           0          3s
  23. #如果初始化错误的话,会一直陷入重启的状态,这个跟pod的重启策略有关
  24. [root@master pod]# cat init.yaml
  25. apiVersion: v1
  26. kind: Pod
  27. metadata:
  28.   name:  init-pod
  29.   namespace: test
  30. spec:
  31.   initContainers:
  32.   - name: init-pod1
  33.     image: docker.io/library/nginx:1.9.1
  34.     command: ["/bin/bash","-c","qwe /11.txt"]
  35.   containers:
  36.   - name: main-pod
  37.     image: docker.io/library/nginx:1.9.1
  38. [root@master pod]# kubectl get pod -n test -w
  39. NAME       READY   STATUS    RESTARTS   AGE
  40. init-pod   0/1     Pending   0          0s
  41. init-pod   0/1     Pending   0          0s
  42. init-pod   0/1     Init:0/1   0          0s
  43. init-pod   0/1     Init:0/1   0          0s
  44. init-pod   0/1     Init:0/1   0          1s
  45. init-pod   0/1     Init:Error   0          2s
  46. init-pod   0/1     Init:Error   1 (2s ago)   3s
  47. init-pod   0/1     Init:CrashLoopBackOff   1 (2s ago)   4s
  48. init-pod   0/1     Init:Error              2 (14s ago)   16s
复制代码
2、启动前钩子


  • 就是在主容器运行的前,执行这个钩子
  • 失败的话,会一直重启(重启策略决定的),就不会运行主容器了
  • 有三种的写法
1、exec
  1. [root@master pod]# cat pre.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: pre-pod
  6.   namespace: test
  7. spec:
  8.   containers:
  9.   - name: pre-pod
  10.     image: docker.io/library/nginx:1.9.1
  11.     lifecycle:
  12.       postStart:
  13.          exec:
  14.            command: ["/bin/bash","-c","touch /11.txt"]
  15. [root@master pod]# kubectl exec -n test -ti pre-pod -- /bin/bash
  16. root@pre-pod:/# ls
  17. 11.txt        boot  etc   lib    media  opt        root  sbin  sys  usr
  18. bin        dev   home  lib64  mnt          proc        run   srv   tmp  var
  19. root@pre-pod:/# cat 11.txt
  20. #如果启动前钩子钩子报错的话,后面的主容器不会运行了
复制代码
3、启动后钩子
  1. [root@master pod]# cat pre.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: pre-pod
  6.   namespace: test
  7. spec:
  8.   containers:
  9.   - name: pre-pod
  10.     image: docker.io/library/nginx:1.9.1
  11.     lifecycle:
  12.       preStop:
  13.          exec:
  14.            command: ["/bin/bash","-c","touch /11.txt"]
复制代码
4、pod重启策略和pod的状态


  • 用于设置pod的值
  • Always,当容器出现任何状况的话,就自动进行重启,这个是默认的值
  • OnFailure,当容器终止运行且退出码不为0时,kubelet自动重启该容器
  • Never,不论容器的状态如何,kubelet都不会重启该容器
  • pod的状态
    1、pending,请求创建Pod时,条件不满足,调度没有进行完成没有一个节点符合,或者是处于下载镜像的环境

    • running 就是已经调度到一个节点上面了,里面的容器至少有一个创建出来了
    • succeeded pod里面的全部容器都成功的被终止了,并且不会在重启了
    • Failed 里面的全部容器都已经终止了,并且至少有一个容器是由于失败终止的,就是非0状态重启的
    • Unknown 未知状态,就是apiserver和kubelet出现了问题
    • Evicted状态,内存和硬盘资源不敷
    • CrashLoopBackOff 容器曾经启动了,但是又异常退出了
    • Error pod启动过程中发生了错误
    • Completed 说明pod已经完成了工作,

  1. #在容器里面设置一个启动前钩子,钩子会失败,然后重启策略设置为Never
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: pre-pod
  6.   namespace: test
  7. spec:
  8.   restartPolicy: Never
  9.   containers:
  10.   - name: pre-pod
  11.     image: docker.io/library/nginx:1.9.1
  12.     lifecycle:
  13.       postStart:
  14.         exec:
  15.           command: ["/bin/bash","-c","qwe /11.txt"]
  16. #这个钩子失败了,然后pod不进行重启策略
  17. [root@master pod]# kubectl get pod -n test -w
  18. NAME      READY   STATUS    RESTARTS   AGE
  19. pre-pod   0/1     Pending   0          0s
  20. pre-pod   0/1     Pending   0          0s
  21. pre-pod   0/1     ContainerCreating   0          0s
  22. pre-pod   0/1     ContainerCreating   0          0s
  23. pre-pod   0/1     Completed           0          2s
  24. pre-pod   0/1     Completed           0          3s
  25. pre-pod   0/1     Completed           0          4s
  26. #查看详细信息
  27. #正常退出了
  28. Events:
  29.   Type     Reason               Age   From               Message
  30.   ----     ------               ----  ----               -------
  31.   Normal   Scheduled            12m   default-scheduler  Successfully assigned test/pre-pod to node1
  32.   Normal   Pulled               12m   kubelet            Container image "docker.io/library/nginx:1.9.1" already present on machine
  33.   Normal   Created              12m   kubelet            Created container pre-pod
  34.   Normal   Started              12m   kubelet            Started container pre-pod
  35.   Warning  FailedPostStartHook  12m   kubelet            PostStartHook failed
  36.   Normal   Killing              12m   kubelet       s     FailedPostStartHook
复制代码
五、pod健康检查(主要就是容器里面)

1、liveness probe(存活探测)


  • 用于检测pod内的容器是否处于运行的状态,当这个探测失效时,k8s会根据这个重启策略决定是否重启改容器
  • 适用于在容器发生故障时进行重启,web步伐等
  • 主要就是检测pod是否运行的
  • 支持三种格式,exec,tcp,httpget
  • 探测效果有三个值,Success表现通过了检测,Failure表现未通过检测,Unknown表现检测没有正常的运行
  • kubectl explain pod.spec.containers.livenessProbe
1、参数详解
  1. livenessProbe:
  2.   initialDelaySeconds: #pod启动后首次进行检查的等待时间,单位为秒
  3.   periodSeconds: #检查的间隔时间,默认为10秒
  4.   timeoutSeconds: #探针执行检测请求后,等待响应的超时时间,默认为1秒
  5.   successThreshold: #连续探测几次成功,才认为探测成功,默认为1,在liveness中,必须为1,最小值为1
  6.   failureThreshold: #探测失败的重试次数,重试一定次数后将认为失败,在readiness探针中,Pod会被标记未就绪,默认为3,最小值为1
复制代码
2、exec格式
  1. [root@master pod]# cat liveness.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: live1
  6.   namespace: test
  7. spec:
  8.   containers:
  9.   - name: live1
  10.     image: docker.io/library/nginx:1.9.1
  11.     livenessProbe:
  12.       exec:
  13.         command: ["/bin/bash","-c","touch /11.txt"]
  14.       failureThreshold: 3  #失败三次就认定为失败
  15.       initialDelaySeconds: 3  #进行探测的时候,等待三秒
  16.       periodSeconds: 5   #检查的时间间隔为10s
  17.       successThreshold: 1 #必须为1,有1次成功即可
  18.       timeoutSeconds: 10  #执行请求后,等待的时间为10s
  19. [root@master pod]# kubectl get pod -n test -w
  20. NAME      READY   STATUS      RESTARTS   AGE
  21. pre-pod   0/1     Completed   0          4h45m
  22. live1     0/1     Pending     0          0s
  23. live1     0/1     Pending     0          0s
  24. live1     0/1     ContainerCreating   0          0s
  25. live1     0/1     ContainerCreating   0          1s
  26. live1     1/1     Running             0          2s
  27. live1     1/1     Running             0          30s
复制代码
3、httpget格式
  1. #格式说明
  2. httpGet:
  3.   scheme: #用于连接host的协议,默认为http
  4.   host:  #要连接的主机名,默认为pod的ip,就是容器里面的主机名
  5.   port:  #容器上要访问端口号或名称
  6.   path:   #http服务器上的访问url
  7.   httpHeaders:   #自定义http请求headers,允许重复
  8. [root@master pod]# cat liveness.yaml
  9. apiVersion: v1
  10. kind: Pod
  11. metadata:
  12.   name: live1
  13.   namespace: test
  14. spec:
  15.   containers:
  16.   - name: live1
  17.     image: docker.io/library/nginx:1.9.1
  18.     livenessProbe:
  19.       httpGet:
  20.         port: 80
  21.         scheme: HTTP
  22.         path: /index.html   #就是在容器内部curl localhost:80/index.html检测
  23.       failureThreshold: 3    #返回了一个成功的 HTTP 响应(状态码在 200-399 之间)就是成功的
  24.       initialDelaySeconds: 3
  25.       periodSeconds: 5
  26.       successThreshold: 1
  27.       timeoutSeconds: 10
  28. #可以运行
  29. live1     0/1     ContainerCreating   0          0s
  30. live1     0/1     ContainerCreating   0          1s
  31. live1     1/1     Running             0          2s
  32. live1     1/1     Running             0          42s
复制代码
4、tcp方式健康检查
  1. [root@master pod]# cat liveness.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: live1
  6.   namespace: test
  7. spec:
  8.   containers:
  9.   - name: live1
  10.     image: docker.io/library/nginx:1.9.1
  11.     livenessProbe:
  12.       tcpSocket:
  13.         port: 80   #发送一个探针,尝试连接容器80端口
  14.       failureThreshold: 3
  15.       initialDelaySeconds: 3
  16.       periodSeconds: 5
  17.       successThreshold: 1
  18.       timeoutSeconds: 10
复制代码
2、readiness probe(就绪性探测)


  • 就是pod里面的容器运行了,但是提供服务的步伐,需要读取这个网页的配置文件,才气提供服务
  • 以是的话需要这个就绪性探测,服务器起来了,就能提供这个服务了
  • 防止Pod起来了,但是里面的服务是假的服务这种环境
  • 也支持三种
  1. [root@master pod]# cat liveness.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: live1
  6.   namespace: test
  7. spec:
  8.   containers:
  9.   - name: live1
  10.     image: docker.io/library/nginx:1.9.1
  11.     readinessProbe:
  12.       httpGet:
  13.         port: 80   #发送一个请求
  14.       failureThreshold: 3
  15.       initialDelaySeconds: 3
  16.       periodSeconds: 5
  17.       successThreshold: 1
  18.       timeoutSeconds: 10
  19. #在检测的时候的等待几秒钟
  20. [root@master pod]# kubectl get pod -n test -w
  21. NAME      READY   STATUS      RESTARTS   AGE
  22. pre-pod   0/1     Completed   0          5h11m
  23. live1     0/1     Pending     0          0s
  24. live1     0/1     Pending     0          0s
  25. live1     0/1     ContainerCreating   0          0s
  26. live1     0/1     ContainerCreating   0          0s
  27. live1     0/1     Running             0          1s
  28. live1     1/1     Running             0          5s
复制代码
3、(启动探测)


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表