Pod健康检查(11)

介绍了Kubernetes中如何使用探针进行Pod健康检查
 掌握存活探针的概念和使用方式
 掌握就绪探针的概念和使用方式

目录

  1. Pod探针基本概念
  2. 使用存活探针
  3. 使用就绪探针

1.Pod探针基本概念

1.1 Pod状态

  • Pod的状态信息在PodStatus中定义,其中有一个phase字段,就是我们熟悉的以下一些状态。


    image.png

1.2 更准确的判断Pod状态

  • Kubernetes借助探针(Probes)机制,探针可以会周期性的监测容器运行的状态,
    返回结果
     Liveness探针:存活探针。Liveness探针用户捕获容器的状态是否处于存活状态。如果探测失败,kubelet会根据重启策略尝试恢复容器。
     Readiness探针:就绪探针。如果readiness探针探测失败,则kubelet认为该容器没有准备好对外提供服务,则endpointcontroller会从与pod匹配的所有服务的端点中删除该Pod的地址。

1.3 存活探针和就绪探针对比

  • 就 绪 探 针 与 存 活 探 针 一 致 , 可 以 使 用 ExecAction , TCPSocketAction , HTTPGetAction三种方法
  • 就绪探针用于检测和显示Pod是否已经准备好对外提供业务。在实际使用场景中,就绪探针需要和业务绑定


    image.png

[图片上传中...(image.png-441724-1645689273374-0)]

1.4 容器探针

  • Kubelet可以周期性的执行Container的诊断。为了执行诊断,kubelet调用Container实现的Handler,有三种Handler类型。执行的周期用户自定义,每隔多久探测一次等等参数。
     ExecAction:在容器内执行指定命令,如果命令退出时返回码为0(表示命令成功执行了),则认为诊断成功。
     TCPSocketAction:对指定端口上的容器的Ip地址进行TCP检查。如果端口打开,则认为诊断成功。
     HTTPGetAction: 对指定端口和路径上的容器IP地址执行HTTP Get请求。如果响应的状态码≥200且<400,则诊断认为是成功的。
    注:http状态返回码
    100:http请求发送成功
    200:http返回成功
    300:http转发成功
    400:客户端错误如404
    500:服务器端错误

1.5 检测结果

由kubelet对pod的状态进行检测

image.png

2 使用存活探针

2.1 存活探针案例

  • 本案例采用execaction模式的存活探针。
  • livenessProbe字段详细定义了存活
    探针,包括:
     Handler采用exec
     使用方式是运行cat /tmp/healthy命令
     探测延迟和探测周期是5秒钟。
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
    containers:
    - name: liveness
      args: 
      - /bin/sh
      - -c 
      - touch /tmp/healthy; sleep 30;rm -rf /tmp/healthy; sleep 600
      image: busybox
      livenessProbe:
        exec:
          command:
          - cat
          - /tmp/healthy
        initialDelaySeconds: 5
        periodSeconds: 5

2.2 Liveness探针流程

image.png

2.3 查看存活探针信息

  • 使用describe命令查看pod信息,主要观察podid 是否每次在liveness检测发现失败后完成了自动的重启。
[root@k8s-master ~]# k describe pod liveness-exec 
Name:         liveness-exec
Namespace:    default
Priority:     0
Node:         k8s-node1/192.168.227.11
Start Time:   Mon, 28 Feb 2022 13:21:45 +0800
Labels:       test=liveness
Annotations:  <none>
Status:       Running
IP:           10.244.1.172
IPs:          <none>
Containers:
  liveness:
    Container ID:  docker://41452c228adc587f887cf89dde12055be0a9fff34e3563184d29652943ccd41b
    Image:         busybox
.........
Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  109s                default-scheduler  Successfully assigned default/liveness-exec to k8s-node1
  Warning  Unhealthy  47s (x3 over 57s)   kubelet            Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
  Normal   Killing    47s                 kubelet            Container liveness failed liveness probe, will be restarted
  Normal   Pulling    17s (x2 over 108s)  kubelet            Pulling image "busybox"
  Normal   Pulled     1s (x2 over 92s)    kubelet            Successfully pulled image "busybox"
  Normal   Created    1s (x2 over 92s)    kubelet            Created container liveness
  Normal   Started    1s (x2 over 92s)    kubelet            Started container liveness

2.4 探针高级配置

  • 在上一步骤中使用describe命令可以看到探针的一些策略
Liveness: exec [cat /tmp/healthy] delay=5s timeout=1s period=5s #success=1 #failure=3
  • Delay=5s表示探针在容器启动后5秒开始进行第一次探测
  • Timeout=1s表示容器必须在1秒内反馈信息给探针,否则视为失败。
  • Period=5s表示每5秒探针进行一次探测。
  • success=1表示探测连续成功1次,表示成功。
  • failure=3表示探测连续失败3次,视为Pod处于failure状态,重启容器。
    高级配置参数可以在配置参数时指定,以下为配置样例。实现的功能与之前配置的探针一致。
livenessProbe:
       exec:
       command:
           - cat
           - /tmp/healthy
       initialDelaySeconds: 5
       periodSeconds: 5
       timeoutSeconds: 1
       successThreshold: 1
       failureThreshold: 3

2.5 存活探针 - HTTP

  • HTTP方式的存活探针,通过get方法定期向容器发送http请求。方法中定义了请求路径、端口、请求头等信息。
  • 由于探针仅在返回码≥200,小于400的情况下返回正常,10秒后探针检测失败,kubelet会重启容器。
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-http
spec:
   containers:
   - name: liveness
     image: mirrorgooglecontainers/liveness   <===http的测试镜像
     args: 
     - /server
     livenessProbe:
       httpGet:
       path: /healthz
       port: 8080
       httpHeaders: 
       - name: X-Custom-Header
          value: Awesome
       initialDelaySeconds: 3
       periodSeconds: 3

2.6 存活探针 - TCP

  • TCP探针检测能否建立连接。实验中部署一个telnet服务,探针探测23端口。
  • TCP探针参数与HTTP探针相似。
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: httpd
  name: liveness-tcp
spec:
   containers:
   - name: httpd
     image: httpd
     livenessProbe:
       tcpSocket:
         port: 8080
       httpHeaders: 
       initialDelaySeconds: 10
       periodSeconds: 10

存活探针 tcp探测失败示例

[root@k8s-master ~]# k apply -f /tmp/liveness-tcp.yaml 
pod/liveness-tcp created
[root@k8s-master ~]# k get pod
NAME                     READY   STATUS              RESTARTS   AGE
lablepod-fln79           0/1     Completed           2          18d
liveness-exec            1/1     Running             12         37m
liveness-tcp             0/1     ContainerCreating   0          3s
myapp-pod                1/1     Running             75         30d
mydep-65bbdb4c9f-bvwmp   1/1     Running             18         31d
mydep-65bbdb4c9f-sswdr   1/1     Running             19         31d
mydep-65bbdb4c9f-swgvz   1/1     Running             20         31d
mypod                    0/1     ImagePullBackOff    0          30d
nginx-daemonset-966k2    1/1     Running             7          5d3h
nginx-daemonset-q6vh2    1/1     Running             5          5d3h
pi-8ggjj                 0/1     Completed           0          5d3h
[root@k8s-master ~]# k get pod -w
NAME                     READY   STATUS              RESTARTS   AGE
lablepod-fln79           0/1     Completed           2          18d
liveness-exec            1/1     Running             13         37m
liveness-tcp             0/1     ContainerCreating   0          13s
myapp-pod                1/1     Running             75         30d
mydep-65bbdb4c9f-bvwmp   1/1     Running             18         31d
mydep-65bbdb4c9f-sswdr   1/1     Running             19         31d
mydep-65bbdb4c9f-swgvz   1/1     Running             20         31d
mypod                    0/1     ImagePullBackOff    0          30d
nginx-daemonset-966k2    1/1     Running             7          5d3h
nginx-daemonset-q6vh2    1/1     Running             5          5d3h
pi-8ggjj                 0/1     Completed           0          5d3h
NAME                     AGE
liveness-tcp             26s
^C[root@k8s-master ~]# k get pod
NAME                     READY   STATUS             RESTARTS   AGE
lablepod-fln79           0/1     Completed          2          18d
liveness-exec            1/1     Running            13         38m
liveness-tcp             1/1     Running            0          33s
myapp-pod                1/1     Running            75         30d
mydep-65bbdb4c9f-bvwmp   1/1     Running            18         31d
mydep-65bbdb4c9f-sswdr   1/1     Running            19         31d
mydep-65bbdb4c9f-swgvz   1/1     Running            20         31d
mypod                    0/1     ImagePullBackOff   0          30d
nginx-daemonset-966k2    1/1     Running            7          5d3h
nginx-daemonset-q6vh2    1/1     Running            5          5d3h
pi-8ggjj                 0/1     Completed          0          5d3h
[root@k8s-master ~]# k exec -it liveness-tcp /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@liveness-tcp:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  modules
root@liveness-tcp:/usr/local/apache2# cd conf
root@liveness-tcp:/usr/local/apache2/conf# ls
extra  httpd.conf  magic  mime.types  original
root@liveness-tcp:/usr/local/apache2/conf# cat httpd.conf | grep 80
#Listen 12.34.56.78:80
Listen 80
#ServerName www.example.com:80
root@liveness-tcp:/usr/local/apache2/conf# cat httpd.conf -n | grep 80
    51  #Listen 12.34.56.78:80
    52  Listen 80
    80  #LoadModule authz_dbd_module modules/mod_authz_dbd.so
   180  #LoadModule asis_module modules/mod_asis.so
   241  #ServerName www.example.com:80
   280  
   380      # directives as to Alias.
   480  # MaxRanges: Maximum number of Ranges in a request before
root@liveness-tcp:/usr/local/apache2/conf# sed -i "52c Listen 8080"
sed: no input files
root@liveness-tcp:/usr/local/apache2/conf# sed -i "52c Listen 8080 httpd.conf"
sed: no input files
root@liveness-tcp:/usr/local/apache2/conf# sed -i "52c Listen 8080 " httpd.conf 
root@liveness-tcp:/usr/local/apache2/conf# sed -i "241c ServerName localhost:8080" httpd.conf 
root@liveness-tcp:/usr/local/apache2/conf# cat httpd.conf -n | grep 80
    51  #Listen 12.34.56.78:80
    52  Listen 8080 
    80  #LoadModule authz_dbd_module modules/mod_authz_dbd.so
   180  #LoadModule asis_module modules/mod_asis.so
   241  ServerName localhost:8080
   280  
   380      # directives as to Alias.
   480  # MaxRanges: Maximum number of Ranges in a request before
root@liveness-tcp:/usr/local/apache2/conf# httpd -k restart
root@liveness-tcp:/usr/local/apache2/conf# exit
exit
[root@k8s-master ~]# k get pod
NAME                     READY   STATUS             RESTARTS   AGE
lablepod-fln79           0/1     Completed          2          18d
liveness-exec            1/1     Running            14         45m
liveness-tcp             1/1     Running            0          7m31s
myapp-pod                1/1     Running            76         30d
mydep-65bbdb4c9f-bvwmp   1/1     Running            18         31d
mydep-65bbdb4c9f-sswdr   1/1     Running            19         31d
mydep-65bbdb4c9f-swgvz   1/1     Running            20         31d
mypod                    0/1     ImagePullBackOff   0          30d
nginx-daemonset-966k2    1/1     Running            7          5d3h
nginx-daemonset-q6vh2    1/1     Running            5          5d3h
pi-8ggjj                 0/1     Completed          0          5d3h
[root@k8s-master ~]# k describe p
persistentvolumeclaims             poddisruptionbudgets.policy        podsecuritypolicies.extensions     podtemplates
persistentvolumes                  pods                               podsecuritypolicies.policy         priorityclasses.scheduling.k8s.io
[root@k8s-master ~]# k describe pods liveness-tcp
Name:         liveness-tcp
Namespace:    default
Priority:     0
Node:         k8s-node2/192.168.227.12
Start Time:   Mon, 28 Feb 2022 16:39:46 +0800
Labels:       app=httpd
Annotations:  <none>
Status:       Running
IP:           10.244.2.228
IPs:          <none>
Containers:
  httpd:
    Container ID:   docker://1558d7fd06c0c938e02463e39f38636269991551ea72e86f9d34e80ea552cbc7
    Image:          httpd
    Image ID:       docker-pullable://httpd@sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 28 Feb 2022 16:40:11 +0800
    Ready:          True
    Restart Count:  0
    Liveness:       tcp-socket :80 delay=10s timeout=1s period=10s #success=1 #failure=3
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75t79 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75t79:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75t79
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  7m54s               default-scheduler  Successfully assigned default/liveness-tcp to k8s-node2
  Normal   Pulled     7m29s               kubelet            Successfully pulled image "httpd"
  Normal   Created    7m29s               kubelet            Created container httpd
  Normal   Started    7m29s               kubelet            Started container httpd
  Warning  Unhealthy  10s (x3 over 30s)   kubelet            Liveness probe failed: dial tcp 10.244.2.228:80: connect: connection refused
  Normal   Killing    10s                 kubelet            Container httpd failed liveness probe, will be restarted
  Normal   Pulling    8s (x2 over 7m53s)  kubelet            Pulling image "httpd"
[root@k8s-master ~]# k get pod -w
NAME                     READY   STATUS             RESTARTS   AGE
lablepod-fln79           0/1     Completed          2          18d
liveness-exec            1/1     Running            14         45m
liveness-tcp             1/1     Running            0          8m2s
myapp-pod                1/1     Running            76         30d
mydep-65bbdb4c9f-bvwmp   1/1     Running            18         31d
mydep-65bbdb4c9f-sswdr   1/1     Running            19         31d
mydep-65bbdb4c9f-swgvz   1/1     Running            20         31d
mypod                    0/1     ImagePullBackOff   0          30d
nginx-daemonset-966k2    1/1     Running            7          5d3h
nginx-daemonset-q6vh2    1/1     Running            5          5d3h
pi-8ggjj                 0/1     Completed          0          5d3h
NAME                     AGE
liveness-tcp             8m3s
liveness-exec            45m
^C[root@k8s-master ~]# k get pod
NAME                     READY   STATUS             RESTARTS   AGE
lablepod-fln79           0/1     Completed          2          18d
liveness-exec            1/1     Running            15         46m
liveness-tcp             1/1     Running            1          8m32s
myapp-pod                1/1     Running            76         30d
mydep-65bbdb4c9f-bvwmp   1/1     Running            18         31d
mydep-65bbdb4c9f-sswdr   1/1     Running            19         31d
mydep-65bbdb4c9f-swgvz   1/1     Running            20         31d
mypod                    0/1     ImagePullBackOff   0          30d
nginx-daemonset-966k2    1/1     Running            7          5d3h
nginx-daemonset-q6vh2    1/1     Running            5          5d3h
pi-8ggjj                 0/1     Completed          0          5d3h
[root@k8s-master ~]# k describe pod liveness-tcp
Name:         liveness-tcp
Namespace:    default
Priority:     0
Node:         k8s-node2/192.168.227.12
Start Time:   Mon, 28 Feb 2022 16:39:46 +0800
Labels:       app=httpd
Annotations:  <none>
Status:       Running
IP:           10.244.2.228
IPs:          <none>
Containers:
  httpd:
    Container ID:   docker://ba614058746d64b77ab003b8eea74d506e237660124e9bba2894c90cc97ddc13
    Image:          httpd
    Image ID:       docker-pullable://httpd@sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 28 Feb 2022 16:47:48 +0800
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Mon, 28 Feb 2022 16:40:11 +0800
      Finished:     Mon, 28 Feb 2022 16:47:31 +0800
    Ready:          True
    Restart Count:  1
    Liveness:       tcp-socket :80 delay=10s timeout=1s period=10s #success=1 #failure=3
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75t79 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75t79:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75t79
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Scheduled  9m17s                default-scheduler  Successfully assigned default/liveness-tcp to k8s-node2
  Warning  Unhealthy  93s (x3 over 113s)   kubelet            Liveness probe failed: dial tcp 10.244.2.228:80: connect: connection refused
  Normal   Killing    93s                  kubelet            Container httpd failed liveness probe, will be restarted
  Normal   Pulling    91s (x2 over 9m16s)  kubelet            Pulling image "httpd"
  Normal   Pulled     75s (x2 over 8m52s)  kubelet            Successfully pulled image "httpd"
  Normal   Created    75s (x2 over 8m52s)  kubelet            Created container httpd
  Normal   Started    75s (x2 over 8m52s)  kubelet            Started container httpd
[root@k8s-master ~]#

3 使用就绪探针

3.1 就绪探针

image.png
  • Pod处于存活状态并不意味着可以提供服务,创建完成后通常需要进行诸如准备数据、安装和运行程序等步骤,才能对外提供服务。
  • Liveness探针指示Pod是否处于存活状态,readiness探针则可指示容器是否已经一切准备就绪,可以对外提供服务。

3.2 存活探针和就绪探针对比

  • 就 绪 探 针 与 存 活 探 针 一 致 , 可 以 使 用 ExecAction , TCPSocketAction ,HTTPGetAction三种方法。
  • 就绪探针用于检测和显示Pod是否已经准备好对外提供业务。在实际使用场景中,就绪探针需要和业务绑定。


    image.png

3.3 创建HTTP服务 (示例)

注意观察pod 和 endpoint的变化
1)创建httpd pod,加入exec 就绪探针
2) 创建service(endpoint)
操作:
1.整理环境,清理所有pod/deployment/deamonse/service

[root@k8s-master ~]# k get pod
NAME                     READY   STATUS             RESTARTS   AGE
lablepod-fln79           0/1     Completed          2          18d
liveness-exec            1/1     Running            30         17h
liveness-tcp             1/1     Running            3          17h
myapp-pod                1/1     Running            78         30d
mydep-65bbdb4c9f-bvwmp   1/1     Running            20         31d
mydep-65bbdb4c9f-sswdr   1/1     Running            21         31d
mydep-65bbdb4c9f-swgvz   1/1     Running            22         31d
mypod                    0/1     ImagePullBackOff   0          30d
nginx-daemonset-966k2    1/1     Running            9          5d20h
nginx-daemonset-q6vh2    1/1     Running            7          5d20h
pi-8ggjj                 0/1     Completed          0          5d20h
[root@k8s-master ~]# k delete deployments. mydep
deployment.extensions "mydep" deleted
[root@k8s-master ~]# k get pod
NAME                     READY   STATUS             RESTARTS   AGE
lablepod-fln79           0/1     Completed          2          18d
liveness-exec            1/1     Running            30         17h
liveness-tcp             1/1     Running            3          17h
myapp-pod                1/1     Running            78         30d
mydep-65bbdb4c9f-bvwmp   0/1     Terminating        20         31d
mydep-65bbdb4c9f-sswdr   0/1     Terminating        21         31d
mydep-65bbdb4c9f-swgvz   0/1     Terminating        22         31d
mypod                    0/1     ImagePullBackOff   0          30d
nginx-daemonset-966k2    1/1     Running            9          5d20h
nginx-daemonset-q6vh2    1/1     Running            7          5d20h
pi-8ggjj                 0/1     Completed          0          5d20h
[root@k8s-master ~]# k get deployments.
No resources found in default namespace.
[root@k8s-master ~]# k delete pod --all
pod "lablepod-fln79" deleted
pod "liveness-exec" deleted
pod "liveness-tcp" deleted
pod "myapp-pod" deleted
pod "mypod" deleted
pod "nginx-daemonset-966k2" deleted
pod "nginx-daemonset-q6vh2" deleted
pod "pi-8ggjj" deleted

[root@k8s-master ~]# 
[root@k8s-master ~]# k get pod
NAME                    READY   STATUS    RESTARTS   AGE
nginx-daemonset-s98tm   1/1     Running   0          36s
nginx-daemonset-t2w9n   1/1     Running   0          39s
[root@k8s-master ~]# k get jobs.batch 
NAME       COMPLETIONS   DURATION   AGE
lablepod   1/1           17h        18d
pi         1/1           84s        5d20h
[root@k8s-master ~]# k get deployments.
No resources found in default namespace.
[root@k8s-master ~]# k get deployments. -n kube-system 
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
coredns   2/2     2            2           32d
[root@k8s-master ~]# k describe  pod nginx-daemonset-s98tm
Name:           nginx-daemonset-s98tm
Namespace:      default
Priority:       0
Node:           k8s-node2/192.168.227.12
Start Time:     Tue, 01 Mar 2022 09:49:44 +0800
Labels:         app=nginx
                controller-revision-hash=6dd86d77d
                pod-template-generation=1
Annotations:    <none>
Status:         Running
IP:             10.244.2.239
IPs:            <none>
Controlled By:  DaemonSet/nginx-daemonset
Containers:
  nginx:
    Container ID:   docker://580ddf981f401c8adb58ac9626ff0e8b660ed9b5f7bb51ec75c19fbbc7678726
    Image:          nginx:1.7.9
    Image ID:       docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 01 Mar 2022 09:49:45 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75t79 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75t79:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75t79
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/disk-pressure:NoSchedule op=Exists
                 node.kubernetes.io/memory-pressure:NoSchedule op=Exists
                 node.kubernetes.io/not-ready:NoExecute op=Exists
                 node.kubernetes.io/pid-pressure:NoSchedule op=Exists
                 node.kubernetes.io/unreachable:NoExecute op=Exists
                 node.kubernetes.io/unschedulable:NoSchedule op=Exists
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  2m2s  default-scheduler  Successfully assigned default/nginx-daemonset-s98tm to k8s-node2
  Normal  Pulled     2m2s  kubelet            Container image "nginx:1.7.9" already present on machine
  Normal  Created    2m1s  kubelet            Created container nginx
  Normal  Started    2m1s  kubelet            Started container nginx
[root@k8s-master ~]# k get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          32d
nginx-svc    NodePort    10.100.16.243   <none>        8080:30144/TCP   10d
[root@k8s-master ~]# k get pod
NAME                    READY   STATUS    RESTARTS   AGE
nginx-daemonset-s98tm   1/1     Running   0          2m52s
nginx-daemonset-t2w9n   1/1     Running   0          2m55s
[root@k8s-master ~]# k delete daemonsets. nginx-daemonset 
daemonset.extensions "nginx-daemonset" deleted
[root@k8s-master ~]# k get pod
No resources found in default namespace.
[root@k8s-master ~]#
[root@k8s-master ~]# k get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          32d
nginx-svc    NodePort    10.100.16.243   <none>        8080:30144/TCP   10d
[root@k8s-master ~]# k delete svc nginx-svc
service "nginx-svc" deleted
[root@k8s-master ~]# k get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   32d
[root@k8s-master ~]#

2 创建deployment ,创建service,删除掉某一个httpd的index.html文件观察pod状态和endpoint的范围变化,修复后(touch index.html)后再次观察pod状态和endpoint范围,预期结果恢复

[root@k8s-master tmp]# cat httpdeploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd        
        ports:
        - containerPort: 80
        readinessProbe:
          exec:
            command:
            - cat
            - /usr/local/apache2/htdocs/index.html
          initialDelaySeconds: 5
          periodSeconds: 5
[root@k8s-master tmp]# k get pod
NAME                                READY   STATUS    RESTARTS   AGE
httpd-deployment-859778b7b6-5rwrn   1/1     Running   0          2m9s
httpd-deployment-859778b7b6-fg8vj   1/1     Running   0          2m9s
httpd-deployment-859778b7b6-hfw9t   1/1     Running   0          2m9s
[root@k8s-master tmp]# vim httpsvc.yaml
[root@k8s-master tmp]# k apply -f httpsvc.yaml 
service/httpd-svc created
[root@k8s-master tmp]# k get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
httpd-svc    ClusterIP   10.98.110.166   <none>        8080/TCP   5s
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    33d
[root@k8s-master tmp]# k get endpoints httpd-svc 
NAME        ENDPOINTS                                         AGE
httpd-svc   10.244.1.184:80,10.244.1.185:80,10.244.2.240:80   23s
[root@k8s-master tmp]# k get pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
httpd-deployment-859778b7b6-5rwrn   1/1     Running   0          5m57s   10.244.2.240   k8s-node2   <none>           <none>
httpd-deployment-859778b7b6-fg8vj   1/1     Running   0          5m57s   10.244.1.185   k8s-node1   <none>           <none>
httpd-deployment-859778b7b6-hfw9t   1/1     Running   0          5m57s   10.244.1.184   k8s-node1   <none>           <none>
[root@k8s-master tmp]# k exec -it httpd-deployment-859778b7b6-5rwrn /bin/bash 
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  modules
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2# cd htdoc
bash: cd: htdoc: No such file or directory
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  modules
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2# cd htdocs
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# ll
bash: ll: command not found
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# ls
index.html
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# rm index.html 
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# exit
exit
[root@k8s-master tmp]# cat /tmp/httpdeploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd        
        ports:
        - containerPort: 80
        readinessProbe:
          exec:
            command:
            - cat
            - /usr/local/apache2/htdocs/index.html
          initialDelaySeconds: 5
          periodSeconds: 5
[root@k8s-master tmp]# sleep 10
[root@k8s-master tmp]# k get pods
NAME                                READY   STATUS    RESTARTS   AGE
httpd-deployment-859778b7b6-5rwrn   0/1     Running   0          8m53s
httpd-deployment-859778b7b6-fg8vj   1/1     Running   0          8m53s
httpd-deployment-859778b7b6-hfw9t   1/1     Running   0          8m53s
[root@k8s-master tmp]# k get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE    IP             NODE        NOMINATED NODE   READINESS GATES
httpd-deployment-859778b7b6-5rwrn   0/1     Running   0          9m5s   10.244.2.240   k8s-node2   <none>           <none>
httpd-deployment-859778b7b6-fg8vj   1/1     Running   0          9m5s   10.244.1.185   k8s-node1   <none>           <none>
httpd-deployment-859778b7b6-hfw9t   1/1     Running   0          9m5s   10.244.1.184   k8s-node1   <none>           <none>
[root@k8s-master tmp]# k get endpoints httpd-svc 
NAME        ENDPOINTS                         AGE
httpd-svc   10.244.1.184:80,10.244.1.185:80   4m10s
[root@k8s-master tmp]# k exec -it httpd-deployment-859778b7b6-5rwrn
error: you must specify at least one command for the container
[root@k8s-master tmp]# k exec -it httpd-deployment-859778b7b6-5rwrn -- /bin/bash
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  modules
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2# cd htdocs/
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# ls
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# touch index.html
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# sleep 5
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# k get pod -o wide
bash: k: command not found
root@httpd-deployment-859778b7b6-5rwrn:/usr/local/apache2/htdocs# exit             
exit
command terminated with exit code 127
[root@k8s-master tmp]# k get pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
httpd-deployment-859778b7b6-5rwrn   1/1     Running   0          11m   10.244.2.240   k8s-node2   <none>           <none>
httpd-deployment-859778b7b6-fg8vj   1/1     Running   0          11m   10.244.1.185   k8s-node1   <none>           <none>
httpd-deployment-859778b7b6-hfw9t   1/1     Running   0          11m   10.244.1.184   k8s-node1   <none>           <none>
[root@k8s-master tmp]# k get endpoints httpd-svc 
NAME        ENDPOINTS                                         AGE
httpd-svc   10.244.1.184:80,10.244.1.185:80,10.244.2.240:80   6m35s
[root@k8s-master tmp]# 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容