本篇將補完上一篇所提到關於StatefulSet的實作部份。
話不多說,直接先把重點說明如下:
#-----------------------------------------------
# 1-1. 準備2個PV: nfs-pv
#-----------------------------------------------
# vim nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /var/nfsshare
server: 10.107.88.9
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv002
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /var/nfsshare
server: 10.107.88.9
#-----------------------------------------------
# 1-2. 準備2個PV: nfs-sc
#-----------------------------------------------
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storage
provisioner: kubernetes.io/no-provisioner
parameters:
type: nfs
server: 10.107.88.9
path: /var/nfsshare
# kubectl create -f nfs-sc.yaml -n example
# kubectl create -f nfs-pv.yaml -n example
# kubectl get pv -n example
接下來要建立好service,Statefulset才能套用,Pod會按照以下格式取得DNS/hostname:pod-specific-string.serviceName.default.svc.cluster.local
其中pod-specific-string由StatefulSet controller管理
#-----------------------------------------------
# 1-3. Claim一個StatefulSet資源清單
#-----------------------------------------------
apiVersion: v1
kind: Service
metadata:
name: nginx-headless
namespace: example
labels:
app: nginx
spec:
ports:
- name: http
port: 80
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
namespace: example
spec:
serviceName: "nginx-headless" #指定一個headless service
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx # 要與上面selector一致
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80 #這個Port不會有任何訪問的功能
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates: # pvc template, 會自動建立一個pvc
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: nfs-storage
resources:
requests:
storage: 1Gi
# kubectl create -f nginx-sts.yaml -n example
# kubectl get pod -o wide -n example
上述做法,以圖來看如下:
#-----------------------------------------------
# 1-4. Statefulset建立Pod副本會升序處理(1->2->3), 更新與刪除會降序處理(3->2->1)
#-----------------------------------------------
S1. 依序建立web-0 -> web-1 POD
S2. 確認hostname
# kubectl exec web-0 -n example -- hostname
# kubectl exec web-1 -n example -- hostname
#-----------------------------------------------
# 1-5. 確認由誰控制(statefulset)
#-----------------------------------------------
# kubectl describe pod web-0 -n example
#-----------------------------------------------
# 1-6. 訪問Pod內容
#-----------------------------------------------
# kubectl get pod -o wide -n example
web-0 1/1 Running 0 3h39m 10.244.84.209 worker01.dc1.k8s.poc <none> <none>
web-1 1/1 Running 0 3h39m 10.244.84.210 worker01.dc1.k8s.poc <none> <none>
# curl 10.244.84.209
-> 出現403 error
※ 排除403問題
Cause: volumeClaimTemplates是以掛載點(mountpoint)的方式,不是volume
=> 實際上是將PV的volume掛進容器中,所以會蓋掉容器內的資料。
=> 此處直接到PV內部建立index.html確保容器的正常訪問(或進到容器裡面建立)
# for i in 0 1; do kubectl exec web-$i -n example -- sh -c 'echo hello $(hostname) > /usr/share/nginx/html/index.html'; done
# kubectl get po -o wide
目的:在容器中用DNS方式訪問headless service
#-----------------------------------------------
# 2-1.建立busybox容器
#-----------------------------------------------
# kubectl run -it --image busybox:1.28.3 test -n example \
--restart=Never --rm /bin/sh
/ #
#-----------------------------------------------
# 2-2. 使用nslookup解析headless service
#-----------------------------------------------
/ # nslookup nginx-headless
/ # ping nginx-headless
※ 使用headless不會隨機輪詢背後的pod,而是固定在某個pod,所以不能取代一般的Service
#-----------------------------------------------
# 2-3. 測試刪除Pod之後,看Pod生成狀態
#-----------------------------------------------
# kubectl get pod -o wide -n example --show-labels
# kubectl delete pod -l app=nginx
# kubectl run -it --image busybox:1.28.3 test -n example --restart=Never --rm /bin/sh
/ # nslookup web-0.nginx
/ # nslookup web-1.nginx
※ 確認由StatefulSet controller建立2個pod,ID沒變,IP變化但URL沒變, 一樣Bound之前的PVC,資料一樣存在