iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 21
1
Kubernetes

15 分鐘學習系列 - 第一次學 Kubernetes 就上手系列 第 21

Day 21 - 使用與設定 liveness probes

本篇筆記將介紹探測 Container 的功能, 分為兩種:

  • Liveness 偵測 - 主要偵測 containter 狀態是否進入死結(deadlock), 或應用程式沒有繼續執行, 需要重新啟動
  • Readiness 偵測 - 目的是偵測 Container 是否已經準備好可以接受 Traffic.

Liveness 偵測

  1. 以下的 yaml 設定檔定義 liveness probe 格式如下:
    livenessProbe:
    httpGet:
    path: /status # 偵測的路徑
    port: 8080 # 偵測的 Port
    initialDelaySeconds: 5 # delay 的時間
    timeoutSeconds: 1 # 逾時
# Wishlist deployment yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wishlist-deployment
labels:
app: wishlist
spec:
replicas: 3 #We always want more than 1 replica for HA
selector:
matchLabels:
app: wishlist
template:
metadata:
labels:
app: wishlist
spec:
containers:
- name: wishlist #1st container
image: karthequian/wishlist:1.0 #Dockerhub image
ports:
- containerPort: 8080 #Exposes the port 8080 of the container
name: wishlist-port
env:
- name: PORT #Environment variable key passed to container that is read by application
value: "8080" # Value of the env port.
livenessProbe:
httpGet:
path: /status
port: 8080 #Name or port number
initialDelaySeconds: 5
timeoutSeconds: 1
- name: catalog #2nd container
image: karthequian/wishlist-catalog:1.0
ports:
- containerPort: 8081
name: catalog-port
env:
- name: PORT
value: "8081"
livenessProbe:
httpGet:
path: /status
port: 8081 #Name or port number
initialDelaySeconds: 5
timeoutSeconds: 1
- name: auth #3rd container
image: karthequian/wishlist-auth:1.0
ports:
- containerPort: 8082
name: auth-port
env:
- name: PORT
value: "8082"
livenessProbe:
httpGet:
path: /status
port: 8082 #Name or port number
initialDelaySeconds: 5
timeoutSeconds: 1
---
kind: Service
apiVersion: v1
metadata:
name: wishlist-service
namespace: default
spec:
type: NodePort
selector:
app: wishlist
ports:
- name: wishlist-port
protocol: TCP
port: 8080
targetPort: 8080
- name: wishlist-auth-port
protocol: TCP
port: 8081
targetPort: 8081
- name: wishlist-catalog-port
protocol: TCP
port: 8082
targetPort: 8082
  1. 使用命令 kubectl apply -f .\wishlist-deployment-liveness.yaml 將 liveness probe 部署
PS C:\k8s> kubectl apply -f .\wishlist-deployment-liveness.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps "wishlist-deployment" configured
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
service "wishlist-service" configured
  1. 使用命令 kubectl describe deployment wishlist-deployment 確認 deployment
    其中有 3 個 Liveness probe:
    Liveness: http-get http://:8080/status delay=5s timeout=1s period=10s #success=1
    Liveness: http-get http://:8081/status delay=5s timeout=1s period=10s #success=1
    Liveness: http-get http://:8082/status delay=5s timeout=1s period=10s #success=1
Name:                   wishlist-deployment
Namespace:              default
CreationTimestamp:      Wed, 24 Oct 2018 01:16:03 +0800
Labels:                 app=wishlist
Annotations:            deployment.kubernetes.io/revision=2
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"wishlist"},"name":"wishlist-deployment","namespace":"default"...
Selector:               app=wishlist
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=wishlist
  Containers:
   wishlist:
    Image:      karthequian/wishlist:1.0
    Port:       8080/TCP
    Host Port:  0/TCP
    Liveness:   http-get http://:8080/status delay=5s timeout=1s period=10s #success=1 #failure=3
    Environment:
      PORT:  8080
    Mounts:  <none>
   catalog:
    Image:      karthequian/wishlist-catalog:1.0
    Port:       8081/TCP
    Host Port:  0/TCP
    Liveness:   http-get http://:8081/status delay=5s timeout=1s period=10s #success=1 #failure=3
    Environment:
      PORT:  8081
    Mounts:  <none>
   auth:
    Image:      karthequian/wishlist-auth:1.0
    Port:       8082/TCP
    Host Port:  0/TCP
    Liveness:   http-get http://:8082/status delay=5s timeout=1s period=10s #success=1 #failure=3
    Environment:
      PORT:  8082
    Mounts:  <none>
  Volumes:   <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   wishlist-deployment-bf8f7946c (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  3m    deployment-controller  Scaled up replica set wishlist-deployment-bf8f7946c to 1
  Normal  ScalingReplicaSet  3m    deployment-controller  Scaled down replica set wishlist-deployment-6cb7f6c4c9 to 2
  Normal  ScalingReplicaSet  3m    deployment-controller  Scaled up replica set wishlist-deployment-bf8f7946c to 2
  Normal  ScalingReplicaSet  3m    deployment-controller  Scaled down replica set wishlist-deployment-6cb7f6c4c9 to 1
  Normal  ScalingReplicaSet  3m    deployment-controller  Scaled up replica set wishlist-deployment-bf8f7946c to 3
  Normal  ScalingReplicaSet  3m    deployment-controller  Scaled down replica set wishlist-deployment-6cb7f6c4c9 to 0

以上便設定好 liveness probe 了.


參考資料:


上一篇
Day 20 - 在 Kubernetes 中執行 Job
下一篇
Day 22 - 使用永久性儲存 Persistent Volume
系列文
15 分鐘學習系列 - 第一次學 Kubernetes 就上手30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
cwchiu
iT邦新手 3 級 ‧ 2019-08-22 14:14:28

yaml 排版亂了

我要留言

立即登入留言