美文网首页我爱编程
learning kubernetes by minikube,

learning kubernetes by minikube,

作者: caccq | 来源:发表于2018-04-16 16:31 被阅读0次

    playing with probes.

    Goal

    1. Add HTTP health checks to the helloworld deployment
    2. Simulate a failing deployment that fails a readiness probe
    3. Simulate a failing deployment that fails a liveness probe

    exist helloworld deployment yaml file as below

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: helloworld-deployment
    spec:
      selector:
        matchLabels:
          app: helloworld
      replicas: 1 # tells deployment to run 1 pods matching the template
      template: # create pods using pod definition in this template
        metadata:
          labels:
            app: helloworld
        spec:
          containers:
          - name: helloworld
            image: karthequian/helloworld:latest
            ports:
            - containerPort: 80
    

    A readiness probe is used to know when a container is ready to start accepting traffic.

    readinessProbe:
      # length of time to wait for a pod to initialize
      # after pod startup, before applying health checking
      initialDelaySeconds: 10
      # Amount of time to wait before timing out
      initialDelaySeconds: 1
      # Probe for http
      httpGet:
        # Path to probe
        path: /
        # Port to probe
        port: 80
    

    A liveness probe is used to know when a container might need to be restarted.

    livenessProbe:
      # length of time to wait for a pod to initialize
      # after pod startup, before applying health checking
      initialDelaySeconds: 10
      # Amount of time to wait before timing out
      timeoutSeconds: 1
      # Probe for http
      httpGet:
        # Path to probe
        path: /
        # Port to probe
        port: 80
    

    after add the liveness and readiness probe, the yaml file become as
    helloworld-with-probes.yaml

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: helloworld-deployment-with-probe
    spec:
      selector:
        matchLabels:
          app: helloworld
      replicas: 1 # tells deployment to run 1 pods matching the template
      template: # create pods using pod definition in this template
        metadata:
          labels:
            app: helloworld
        spec:
          containers:
          - name: helloworld
            image: karthequian/helloworld:latest
            ports:
            - containerPort: 80
            readinessProbe:
              # length of time to wait for a pod to initialize
              # after pod startup, before applying health checking
              initialDelaySeconds: 10
              # Amount of time to wait before timing out
              initialDelaySeconds: 1
              # Probe for http
              httpGet:
                # Path to probe
                path: /
                # Port to probe
                port: 80
            livenessProbe:
              # length of time to wait for a pod to initialize
              # after pod startup, before applying health checking
              initialDelaySeconds: 10
              # Amount of time to wait before timing out
              timeoutSeconds: 1
              # Probe for http
              httpGet:
                # Path to probe
                path: /
                # Port to probe
                port: 80
    
    localhost:~ xunyang$ kubectl create -f helloworld-with-probes.yaml 
    deployment.apps "helloworld-deployment-with-probe" created
    localhost:~ xunyang$ kubectl get deployment
    NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    helloworld-deployment-with-probe   1         1         1            1           9m
    localhost:~ xunyang$ kubectl describe pod helloworld-deployment-with-probe-7f78698c95-whltr
    Name:           helloworld-deployment-with-probe-7f78698c95-whltr
    Namespace:      default
    Node:           minikube/10.0.2.15
    Start Time:     Mon, 16 Apr 2018 15:44:07 +0800
    Labels:         app=helloworld
                    pod-template-hash=3934254751
    Annotations:    <none>
    Status:         Running
    IP:             172.17.0.4
    Controlled By:  ReplicaSet/helloworld-deployment-with-probe-7f78698c95
    Containers:
      helloworld:
        Container ID:   docker://f27e53a391706040d16409054b9f4792b47e5e6a5ef5749fc839000bf11345ee
        Image:          karthequian/helloworld:latest
        Image ID:       docker-pullable://karthequian/helloworld@sha256:165f87263f1775f0bf91022b48a51265357ba9f36bc3882f5ecdefc7f8ef8f6d
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Mon, 16 Apr 2018 15:44:11 +0800
        Ready:          True
        Restart Count:  0
        Liveness:       http-get http://:80/ delay=10s timeout=1s period=10s #success=1 #failure=3
        Readiness:      http-get http://:80/ delay=1s timeout=1s period=10s #success=1 #failure=3
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from default-token-64pmj (ro)
    Conditions:
      Type           Status
      Initialized    True 
      Ready          True 
      PodScheduled   True 
    Volumes:
      default-token-64pmj:
        Type:        Secret (a volume populated by a Secret)
        SecretName:  default-token-64pmj
        Optional:    false
    QoS Class:       BestEffort
    Node-Selectors:  <none>
    Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                     node.kubernetes.io/unreachable:NoExecute for 300s
    Events:
      Type    Reason                 Age   From               Message
      ----    ------                 ----  ----               -------
      Normal  Scheduled              23m   default-scheduler  Successfully assigned helloworld-deployment-with-probe-7f78698c95-whltr to minikube
      Normal  SuccessfulMountVolume  23m   kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-64pmj"
      Normal  Pulling                23m   kubelet, minikube  pulling image "karthequian/helloworld:latest"
      Normal  Pulled                 23m   kubelet, minikube  Successfully pulled image "karthequian/helloworld:latest"
      Normal  Created                23m   kubelet, minikube  Created container
      Normal  Started                23m   kubelet, minikube  Started container
    

    Simulate a failing deployment that fails a readiness probe

    We will now try to simulate a bad helloworld pod that fails a readiness probe. Run a readiness check on port 90 to simulate a failing scenario.

    helloworld-with-bad-readiness-probe.yaml

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: helloworld-deployment-with-bad-readiness-probe
    spec:
      selector:
        matchLabels:
          app: helloworld
      replicas: 1 # tells deployment to run 1 pods matching the template
      template: # create pods using pod definition in this template
        metadata:
          labels:
            app: helloworld
        spec:
          containers:
          - name: helloworld
            image: karthequian/helloworld:latest
            ports:
            - containerPort: 80
            readinessProbe:
              # length of time to wait for a pod to initialize
              # after pod startup, before applying health checking
              initialDelaySeconds: 10
              # Amount of time to wait before timing out
              initialDelaySeconds: 1
              # Probe for http
              httpGet:
                # Path to probe
                path: /
                # Port to probe
                port: 90
    
    localhost:~ xunyang$ kubectl create -f helloworld-with-bad-readiness-probe.yaml 
    deployment.apps "helloworld-deployment-with-bad-readiness-probe" created
    localhost:~ xunyang$ kubectl get deployment
    NAME                                             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    helloworld-deployment-with-bad-readiness-probe   1         1         1            0           9m
    helloworld-deployment-with-probe                 1         1         1            1           12m
    localhost:~ xunyang$ kubectl get pods
    NAME                                                             READY     STATUS    RESTARTS   AGE
    helloworld-deployment-with-bad-readiness-probe-9447b7b49-p2qwm   0/1       Running   0          10m
    helloworld-deployment-with-probe-7f78698c95-whltr                1/1       Running   0          13m
    localhost:~ xunyang$ kubectl describe pod helloworld-deployment-with-bad-readiness-probe-9447b7b49-p2qwm
    Name:           helloworld-deployment-with-bad-readiness-probe-9447b7b49-p2qwm
    Namespace:      default
    Node:           minikube/10.0.2.15
    Start Time:     Mon, 16 Apr 2018 15:47:15 +0800
    Labels:         app=helloworld
                    pod-template-hash=500363605
    Annotations:    <none>
    Status:         Running
    IP:             172.17.0.5
    Controlled By:  ReplicaSet/helloworld-deployment-with-bad-readiness-probe-9447b7b49
    Containers:
      helloworld:
        Container ID:   docker://a2fdd491e851524fa5f24ea7d20e0306004561a43c7e734b4992ae3da71859a2
        Image:          karthequian/helloworld:latest
        Image ID:       docker-pullable://karthequian/helloworld@sha256:165f87263f1775f0bf91022b48a51265357ba9f36bc3882f5ecdefc7f8ef8f6d
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Mon, 16 Apr 2018 15:47:19 +0800
        Ready:          False
        Restart Count:  0
        Readiness:      http-get http://:90/ delay=1s timeout=1s period=10s #success=1 #failure=3
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from default-token-64pmj (ro)
    Conditions:
      Type           Status
      Initialized    True 
      Ready          False 
      PodScheduled   True 
    Volumes:
      default-token-64pmj:
        Type:        Secret (a volume populated by a Secret)
        SecretName:  default-token-64pmj
        Optional:    false
    QoS Class:       BestEffort
    Node-Selectors:  <none>
    Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                     node.kubernetes.io/unreachable:NoExecute for 300s
    Events:
      Type     Reason                 Age                From               Message
      ----     ------                 ----               ----               -------
      Normal   Scheduled              12m                default-scheduler  Successfully assigned helloworld-deployment-with-bad-readiness-probe-9447b7b49-p2qwm to minikube
      Normal   SuccessfulMountVolume  12m                kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-64pmj"
      Normal   Pulling                12m                kubelet, minikube  pulling image "karthequian/helloworld:latest"
      Normal   Pulled                 12m                kubelet, minikube  Successfully pulled image "karthequian/helloworld:latest"
      Normal   Created                12m                kubelet, minikube  Created container
      Normal   Started                12m                kubelet, minikube  Started container
      Warning  Unhealthy              9m (x20 over 12m)  kubelet, minikube  Readiness probe failed: Get http://172.17.0.5:90/: dial tcp 172.17.0.5:90: getsockopt: connection refused
    

    as the pod is not ready, the pod check will wait till the pod is ready.

    Simulate a failing deployment that fails a liveness probe

    we will simulate a bad helloworld pod that fails a liveness probe. Run a liveness check on port 90 to simulate a failing scenario.

    helloworld-with-bad-liveness-probe.yaml

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: helloworld-deployment-with-bad-liveness-probe
    spec:
      selector:
        matchLabels:
          app: helloworld
      replicas: 1 # tells deployment to run 1 pods matching the template
      template: # create pods using pod definition in this template
        metadata:
          labels:
            app: helloworld
        spec:
          containers:
          - name: helloworld
            image: karthequian/helloworld:latest
            ports:
            - containerPort: 80
            livenessProbe:
              # length of time to wait for a pod to initialize
              # after pod startup, before applying health checking
              initialDelaySeconds: 10
              # How often (in seconds) to perform the probe.
              periodSeconds: 5
              # Amount of time to wait before timing out
              timeoutSeconds: 1
              # Kubernetes will try failureThreshold times before giving up and restarting the Pod
              failureThreshold: 2
              # Probe for http
              httpGet:
                # Path to probe
                path: /
                # Port to probe
                port: 90
    
    localhost:~ xunyang$ kubectl create -f helloworld-with-bad-liveness-probe.yaml
    deployment.apps "helloworld-deployment-with-bad-liveness-probe" created
    ocalhost:~ xunyang$ kubectl get pods
    NAME                                                             READY     STATUS             RESTARTS   AGE
    helloworld-deployment-with-bad-liveness-probe-7fbddcfd4d-8k6t8   0/1       CrashLoopBackOff   6          13m
    helloworld-deployment-with-bad-readiness-probe-9447b7b49-p2qwm   0/1       Running            0          22m
    helloworld-deployment-with-probe-7f78698c95-whltr                1/1       Running            0          25m
    localhost:~ xunyang$ kubectl describe pod helloworld-deployment-with-bad-liveness-probe-7fbddcfd4d-8k6t8
    Name:           helloworld-deployment-with-bad-liveness-probe-7fbddcfd4d-8k6t8
    Namespace:      default
    Node:           minikube/10.0.2.15
    Start Time:     Mon, 16 Apr 2018 15:55:35 +0800
    Labels:         app=helloworld
                    pod-template-hash=3968879808
    Annotations:    <none>
    Status:         Running
    IP:             172.17.0.6
    Controlled By:  ReplicaSet/helloworld-deployment-with-bad-liveness-probe-7fbddcfd4d
    Containers:
      helloworld:
        Container ID:   docker://32d999bbc20c8c00dc0e19a51a351b82a7cbf763f38715a3b0791ec86e7b3f0f
        Image:          karthequian/helloworld:latest
        Image ID:       docker-pullable://karthequian/helloworld@sha256:165f87263f1775f0bf91022b48a51265357ba9f36bc3882f5ecdefc7f8ef8f6d
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Waiting
          Reason:       CrashLoopBackOff
        Last State:     Terminated
          Reason:       Completed
          Exit Code:    0
          Started:      Mon, 16 Apr 2018 15:59:12 +0800
          Finished:     Mon, 16 Apr 2018 15:59:24 +0800
        Ready:          False
        Restart Count:  6
        Liveness:       http-get http://:90/ delay=10s timeout=1s period=5s #success=1 #failure=2
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from default-token-64pmj (ro)
    Conditions:
      Type           Status
      Initialized    True 
      Ready          False 
      PodScheduled   True 
    Volumes:
      default-token-64pmj:
        Type:        Secret (a volume populated by a Secret)
        SecretName:  default-token-64pmj
        Optional:    false
    QoS Class:       BestEffort
    Node-Selectors:  <none>
    Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                     node.kubernetes.io/unreachable:NoExecute for 300s
    Events:
      Type     Reason                 Age                From               Message
      ----     ------                 ----               ----               -------
      Normal   Scheduled              14m                default-scheduler  Successfully assigned helloworld-deployment-with-bad-liveness-probe-7fbddcfd4d-8k6t8 to minikube
      Normal   SuccessfulMountVolume  14m                kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-64pmj"
      Normal   Pulling                13m (x4 over 14m)  kubelet, minikube  pulling image "karthequian/helloworld:latest"
      Normal   Killing                13m (x3 over 14m)  kubelet, minikube  Killing container with id docker://helloworld:Container failed liveness probe.. Container will be killed and recreated.
      Normal   Pulled                 13m (x4 over 14m)  kubelet, minikube  Successfully pulled image "karthequian/helloworld:latest"
      Normal   Created                13m (x4 over 14m)  kubelet, minikube  Created container
      Normal   Started                13m (x4 over 14m)  kubelet, minikube  Started container
      Warning  Unhealthy              13m (x5 over 14m)  kubelet, minikube  Liveness probe failed: Get http://172.17.0.6:90/: dial tcp 172.17.0.6:90: getsockopt: connection refused
    

    as the liveness check failed, the pod will continue recreate even the pod is ready.

    localhost:~ xunyang$ kubectl get pods
    NAME                                                             READY     STATUS    RESTARTS   AGE
    helloworld-deployment-with-bad-liveness-probe-7fbddcfd4d-8k6t8   1/1       Running   5          11m
    helloworld-deployment-with-bad-readiness-probe-9447b7b49-p2qwm   0/1       Running   0          19m
    helloworld-deployment-with-probe-7f78698c95-whltr                1/1       Running   0          22m
    localhost:~ xunyang$ kubectl get pod
    NAME                                                             READY     STATUS             RESTARTS   AGE
    helloworld-deployment-with-bad-liveness-probe-7fbddcfd4d-8k6t8   0/1       CrashLoopBackOff   11         29m
    helloworld-deployment-with-bad-readiness-probe-9447b7b49-p2qwm   0/1       Running            0          37m
    helloworld-deployment-with-probe-7f78698c95-whltr                1/1       Running            0          40m
    localhost:~ xunyang$ kubectl get pod
    

    when check deployment status, the both wrong probe shows not available.

    localhost:~ xunyang$ kubectl get deployment
    NAME                                             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    helloworld-deployment-with-bad-liveness-probe    1         1         1            0           16m
    helloworld-deployment-with-bad-readiness-probe   1         1         1            0           24m
    helloworld-deployment-with-probe                 1         1         1            1           27m
    

    clear data

    相关文章

      网友评论

        本文标题:learning kubernetes by minikube,

        本文链接:https://www.haomeiwen.com/subject/ajaskftx.html