learning kubernetes by minikube, 5

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容