KUBERNETES-1-5-控制器应用二

1.kubectl explain pods.spec.containers查看容器信息。 lifecycle    选取pod整个生命周期针,针对启动后和终止前。livenessProbe    进行存活性探测针状态描述。 readinessProbe    进行就绪性探测针状态描述。

[root@master manifests]# kubectl explain pods.spec.containers
   lifecycle    
     Actions that the management system should take in response to container
     lifecycle events. Cannot be updated.
   livenessProbe    
     Periodic probe of container liveness. Container will be restarted if the
     probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
   readinessProbe    
     Periodic probe of container service readiness. Container will be removed
     from service endpoints if the probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
 

2.kubectl explain pods.spec.containers.livenessProbe查看livenessProbe下的三种探针类型,exec    ,httpGet    ,tcpSocket    。   failureThreshold    指明失败后继续探测的次数。periodSeconds    指明再次探测间隔的时间。timeoutSeconds    指明探测等待的时间。initialDelaySeconds    指初始化延迟探测的时间,因为容器不可能一启动就马上运行,等待一段时间其完全启动。

[root@master manifests]# kubectl explain pods.spec.containers.livenessProbe

   exec    
     One and only one of the following should be specified. Exec specifies the
     action to take.

   failureThreshold    
     Minimum consecutive failures for the probe to be considered failed after
     having succeeded. Defaults to 3. Minimum value is 1.

   httpGet    
     HTTPGet specifies the http request to perform.

   initialDelaySeconds    
     Number of seconds after the container has started before liveness probes
     are initiated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   periodSeconds    
     How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
     value is 1.

   successThreshold    
     Minimum consecutive successes for the probe to be considered successful
     after having failed. Defaults to 1. Must be 1 for liveness. Minimum value
     is 1.

   tcpSocket    
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

   timeoutSeconds    
     Number of seconds after which the probe times out. Defaults to 1 second.
     Minimum value is 1. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
 

3.编辑liveness-exec.yaml 文件,kubectl create -f liveness-exec.yaml使用资源脚本创建pod,使用 livenessProbe:探针探测/tmp/healty文件,因为command: 在执行的过程中已经将/tmp/healty文件删除,kubectl get pods可以获得liveness-exec-pod的RESTARTS的次数为3,对liveness-exec.yaml 进行验证。

[root@master manifests]# vim liveness-exec.yaml
[root@master manifests]# 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 30; rm -f /tmp/healthy; sleep 3600"]
    livenessProbe:
      exec:
        command: ["test","-e","/tmp/healty"]
      initialDelaySeconds: 1
      periodSeconds: 3
[root@master manifests]# kubectl create -f liveness-exec.yaml 
pod/liveness-exec-pod created
[root@master manifests]# kubectl get pods
NAME                          READY     STATUS      RESTARTS   AGE
client                        0/1       Completed   0          3d
liveness-exec-pod             1/1       Running     3          2m
myapp-848b5b879b-7h254        1/1       Running     2          3d
myapp-848b5b879b-d7rjs        1/1       Running     2          3d
myapp-848b5b879b-wv5cz        1/1       Running     2          3d
nginx-deploy-5b595999-tj8ms   1/1       Running     2          3d
pod-demo                      2/2       Running     4          4h
 

4.创建liveness-httpget.yaml脚本, ports:暴露containerPort: 80端口,使用httpGet:探针探测path: /index.html,kubectl create -f liveness-httpget.yaml 把资源运行起来。kubectl describe pods liveness-httpget-pod获取pod的详细信息。

[root@master manifests]# kubectl explain pods.spec.containers.livenessProbe.tcpSocket
[root@master manifests]# kubectl explain pods.spec.containers.livenessProbe.httpGet

[root@master manifests]# cp liveness-exec.yaml liveness-httpget.yaml 
[root@master manifests]# vim liveness-httpget.yaml 
[root@master manifests]# cat liveness-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: liveness-httpget-container
    image: ikubernetes/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    livenessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
[root@master manifests]# kubectl create -f liveness-httpget.yaml 
pod/liveness-httpget-pod created
[root@master manifests]# kubectl get pods
NAME                          READY     STATUS             RESTARTS   AGE
client                        0/1       Completed          0          3d
liveness-exec-pod             0/1       CrashLoopBackOff   31         1h
liveness-httpget-pod          1/1       Running            0          3m
myapp-848b5b879b-7h254        1/1       Running            2          3d
myapp-848b5b879b-d7rjs        1/1       Running            2          3d
myapp-848b5b879b-wv5cz        1/1       Running            2          3d
nginx-deploy-5b595999-tj8ms   1/1       Running            2          3d
pod-demo                      2/2       Running            6          6h
[root@master manifests]# kubectl describe pods liveness-httpget-pod
Name:               liveness-httpget-pod
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node2.example.com/172.20.0.130
Start Time:         Mon, 10 Dec 2018 07:06:54 -0500
Labels:            
Annotations:        
Status:             Running
IP:                 10.244.2.20
Containers:
  liveness-httpget-container:
    Container ID:   docker://a6cf7201f11f4091a0bc8ced22f59d58706534785fc342bc8ed44a3120ad44da
    Image:          ikubernetes/myapp:v1
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 10 Dec 2018 07:06:54 -0500
    Ready:          True
    Restart Count:  0
    Liveness:       http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-s5rf4 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-s5rf4:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-s5rf4
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From                        Message
  ----    ------     ----  ----                        -------
  Normal  Scheduled  3m    default-scheduler           Successfully assigned default/liveness-httpget-pod to node2.example.com
  Normal  Pulled     3m    kubelet, node2.example.com  Container image "ikubernetes/myapp:v1" already present on machine
  Normal  Created    3m    kubelet, node2.example.com  Created container
  Normal  Started    3m    kubelet, node2.example.com  Started container
 

5.kubectl exec -it liveness-httpget-pod -- /bin/sh以交互方式进入pod。rm -f /usr/share/nginx/html/index.html 删除网页默认页面。系统终止后,kubectl describe pods liveness-httpget-pod查看详细信息。可以看到最终状态 Last State:     Terminated。 还可以看到系统重启信息Restart Count:  2。

注:这里碰到交互访问的问题,在检查/etc/hosts没有问题的情况下,原因在于node上的防火墙没有关。

[root@master ~]# kubectl exec -it liveness-httpget-pod -- /bin/sh
Error from server: error dialing backend: dial tcp 172.20.0.130:10250: connect: no route to host
 

[root@master ~]# kubectl exec -it liveness-httpget-pod -- /bin/sh
/ # rm -f /usr/share/nginx/html/index.html 
/ # command terminated with exit code 137
[root@master ~]# kubectl describe pods liveness-httpget-pod
Name:               liveness-httpget-pod
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node2.example.com/172.20.0.130
Start Time:         Mon, 10 Dec 2018 07:06:54 -0500
Labels:            
Annotations:        
Status:             Running
IP:                 10.244.2.25
Containers:
  liveness-httpget-container:
    Container ID:   docker://185f00f15af577144bd0dd4752d71683e619565af4d60cb8981150c31b947b51
    Image:          ikubernetes/myapp:v1
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 10 Dec 2018 08:53:31 -0500
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Mon, 10 Dec 2018 08:44:17 -0500
      Finished:     Mon, 10 Dec 2018 08:53:31 -0500
    Ready:          True
    Restart Count:  2
    Liveness:       http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-s5rf4 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-s5rf4:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-s5rf4
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason                  Age                From                        Message
  ----     ------                  ----               ----                        -------
  Warning  Unhealthy               16m                kubelet, node2.example.com  Liveness probe failed: Get http://10.244.2.20:80/index.html: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
  Normal   SandboxChanged          16m (x3 over 16m)  kubelet, node2.example.com  Pod sandbox changed, it will be killed and re-created.
  Warning  FailedCreatePodSandBox  10m                kubelet, node2.example.com  Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "65be288050b14b8cbba7cd9173ff101deac25592ad24b8a16db549e9ca344818" network for pod "liveness-httpget-pod": NetworkPlugin cni failed to set up pod "liveness-httpget-pod_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  10m                kubelet, node2.example.com  Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "deb91a662795f76623af8dbe8bd8e19e975ba2254a7316b8903687df73e59a56" network for pod "liveness-httpget-pod": NetworkPlugin cni failed to set up pod "liveness-httpget-pod_default" network: open /run/flannel/subnet.env: no such file or directory
  Warning  FailedCreatePodSandBox  10m                kubelet, node2.example.com  Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "362a9522bc0603da07d5383eefe876202dece63160a399feac32026044fb4753" network for pod "liveness-httpget-pod": NetworkPlugin cni failed to set up pod "liveness-httpget-pod_default" network: open /run/flannel/subnet.env: no such file or directory
  Normal   SandboxChanged          10m (x4 over 10m)  kubelet, node2.example.com  Pod sandbox changed, it will be killed and re-created.
  Normal   Pulled                  1m (x2 over 10m)   kubelet, node2.example.com  Container image "ikubernetes/myapp:v1" already present on machine
  Normal   Created                 1m (x2 over 10m)   kubelet, node2.example.com  Created container
  Normal   Started                 1m (x2 over 10m)   kubelet, node2.example.com  Started container
  Warning  Unhealthy               1m (x3 over 1m)    kubelet, node2.example.com  Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing                 1m                 kubelet, node2.example.com  Killing container with id docker://liveness-httpget-container:Container failed liveness probe.. Container will be killed and recreated.
 

6.kubectl exec -it liveness-httpget-pod -- /bin/sh重启pod。ls /usr/share/nginx/html/查看发现 index.html再次被创建。

[root@master ~]# kubectl exec -it liveness-httpget-pod -- /bin/sh
/ # ls /usr/share/nginx/html/
50x.html    index.html
/ # exit
 

7.创建资源文件readiness-httpget.yaml。kubectl create -f readiness-httpget.yaml把资源跑起来。kubectl exec -it readiness-httpget-pod -- /bin/sh进入交互界面。rm -f /usr/share/nginx/html/index.html删除readinessProbe:探针探测的path: /index.html文件信息。kubectl get pods查看pod状态已经宕掉了。 echo helloworld > /usr/share/nginx/html/index.html重新创建文件,kubectl get pods查看pod状态已经起来。

[root@master manifests]# cp liveness-httpget.yaml readiness-httpget.yaml
[root@master manifests]# vim readiness-httpget.yaml

[root@master manifests]# cat readiness-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
  - name: readiness-httpget-container
    image: ikubernetes/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    readinessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
[root@master manifests]# kubectl create -f readiness-httpget.yaml
pod/readiness-httpget-pod created
[root@master manifests]# kubectl get pods
NAME                          READY     STATUS      RESTARTS   AGE
client                        0/1       Completed   0          3d
liveness-httpget-pod          1/1       Running     2          2h
myapp-848b5b879b-7h254        1/1       Running     3          3d
myapp-848b5b879b-d7rjs        1/1       Running     4          3d
myapp-848b5b879b-wv5cz        1/1       Running     3          3d
nginx-deploy-5b595999-tj8ms   1/1       Running     4          3d
pod-demo                      2/2       Running     9          8h
readiness-httpget-pod         1/1       Running     0          9s
[root@master manifests]# kubectl exec -it readiness-httpget-pod -- /bin/sh
/ # rm -f /usr/share/nginx/html/index.html

/ # ps
PID   USER     TIME   COMMAND
    1 root       0:00 nginx: master process nginx -g daemon off;
    7 nginx      0:00 nginx: worker process
    8 root       0:00 /bin/sh
   14 root       0:00 ps
[root@master manifests]# kubectl get pods
NAME                          READY     STATUS      RESTARTS   AGE
client                        0/1       Completed   0          3d
liveness-httpget-pod          1/1       Running     2          2h
myapp-848b5b879b-7h254        1/1       Running     3          3d
myapp-848b5b879b-d7rjs        1/1       Running     4          3d
myapp-848b5b879b-wv5cz        1/1       Running     3          3d
nginx-deploy-5b595999-tj8ms   1/1       Running     4          3d
pod-demo                      2/2       Running     9          8h
readiness-httpget-pod         0/1       Running     0          1m

/ # echo helloworld > /usr/share/nginx/html/index.html
[root@master manifests]# kubectl get pods
NAME                          READY     STATUS      RESTARTS   AGE
client                        0/1       Completed   0          3d
liveness-httpget-pod          1/1       Running     2          2h
myapp-848b5b879b-7h254        1/1       Running     3          3d
myapp-848b5b879b-d7rjs        1/1       Running     4          3d
myapp-848b5b879b-wv5cz        1/1       Running     3          3d
nginx-deploy-5b595999-tj8ms   1/1       Running     4          3d
pod-demo                      2/2       Running     9          8h
readiness-httpget-pod         1/1       Running     0          3m
 

8.vim poststart-pod.yaml创建资源文件。kubectl create -f poststart-pod.yaml 把资源跑起来。 pod会先执行command: ["/bin/sh","-c","sleep 3600"],同时触发lifecycle:的postStart:,执行command: ['mkdir','-p','/data/web/html']。这里一定要区分命令执行的先后,否则起pod的时候会报错。kubectl exec -it poststart-pod -- /bin/sh可以通过交互模式验证。

[root@master manifests]# vim poststart-pod.yaml
[root@master manifests]# 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: ["/bin/sh","-c","sleep 3600"]
[root@master manifests]# kubectl create -f poststart-pod.yaml 
pod/poststart-pod created
[root@master manifests]# kubectl get pods
NAME                          READY     STATUS      RESTARTS   AGE
client                        0/1       Completed   0          3d
liveness-httpget-pod          1/1       Running     2          2h
myapp-848b5b879b-7h254        1/1       Running     3          3d
myapp-848b5b879b-d7rjs        1/1       Running     4          3d
myapp-848b5b879b-wv5cz        1/1       Running     3          3d
nginx-deploy-5b595999-tj8ms   1/1       Running     4          3d
pod-demo                      2/2       Running     9          8h
poststart-pod                 1/1       Running     0          3s
readiness-httpget-pod         1/1       Running     0          25m
[root@master manifests]# kubectl exec -it poststart-pod -- /bin/sh
/ # ls /
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # ls /data/web/html
/ # exit
 

你可能感兴趣的:(KUBERNETES)