前言:
本文從一個簡單示例開始介紹statefulSet的創(chuàng)建和基本用法橱野,最后提供一個mongo集群的生產(chǎn)示例。
1. 概念
1.1 功能:
- 維護(hù)体斩、管理pod的生命周期
- 運行的pod都是有狀態(tài)的剿吻,切有啟動順序
1.2 一般應(yīng)用
Mysql、Mongo 等有狀態(tài)的服務(wù)
1.3 比較
對象 | 是否有狀態(tài) | POD數(shù)量 |
---|---|---|
Deployment | 無 | 按副本數(shù) |
DaemonSet | 無 | (適合節(jié)點)每節(jié)點一個 |
StatefulSet | 有 | 按副本數(shù) |
相關(guān)文檔
《K8S-Deployment》
《K8S-DaemonSet》
2. 簡單示例基本用法
說明:nginx本身是沒有狀態(tài)的讯檐,這里用nginx僅是便于后邊的伸縮演示,對實際生產(chǎn)并沒有意義染服。
- 已有條件:
集群中已經(jīng)創(chuàng)建了默認(rèn)StorageClass别洪。(pv會默認(rèn)自動創(chuàng)建) - 需要創(chuàng)建:
HeadLessService
StatefulSet
2.1 yml文件
- HeadLessService
創(chuàng)建一個test.yml文件譬重,內(nèi)容如下:
apiVersion: v1
kind: Service
metadata:
name: headless-nginx
namespace: test
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
說明:headless和普通service的區(qū)別是設(shè)置 clusterIP: None舶得,不讓其獲取ClusterIP , DNS解析的時候直接走pod的ip地址婉支。
- 在test.yml文件添加如下內(nèi)容(和前邊service用--- 分開)
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
namespace: test
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: harborcto.xxx.com.cn/public/nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
說明:
volumeClaimTemplates 是創(chuàng)建 pvc 的樣例秉颗。和pod的樣例 template 是平級的痢毒。
2.2 啟動和查看
[root@DoM01 statfulset]# kubectl create -f test.yml
service/headless-nginx created
statefulset.apps/web created
[root@DoM01 statfulset]# kubectl get all -n test
NAME READY STATUS RESTARTS AGE
pod/web-0 1/1 Running 0 28s
pod/web-1 1/1 Running 0 22s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/headless-nginx ClusterIP None <none> 80/TCP 25s
NAME READY AGE
statefulset.apps/web 2/2 25s
可以看到:
1)沒有使用Deployment時的 replicaset來管理pod。
2)有web-0 和web-1 兩個pod啟動了蚕甥。
3)headless-service 沒有分配ip地址哪替。
2.3 伸縮
- 增加到5個pod
[root@DoM01 ~]# kubectl scale statefulset -n test --replicas=5 web
statefulset.apps/web scaled
[root@DoM01 ~]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 20m
web-1 1/1 Running 0 20m
web-2 1/1 Running 0 18s
web-3 1/1 Running 0 13s
web-4 1/1 Running 0 7s
從時間可見,pod在按序號依次啟動菇怀。
- 減少到兩個
[root@DoM01 ~]# kubectl scale statefulset -n test --replicas=2 web
statefulset.apps/web scaled
[root@DoM01 ~]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 26m
web-1 1/1 Running 0 26m
web-2 1/1 Running 0 6m53s
web-3 1/1 Running 0 6m48s
web-4 0/1 Terminating 0 6m42s
[root@DoM01 ~]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 27m
web-1 1/1 Running 0 26m
如上可見凭舶,pod按序號從后往前依次關(guān)閉。
2.4 刪除
[root@DoM01 ~]# kubectl delete -n test statefulsets web
statefulset.apps "web" deleted
[root@DoM01 ~]# kubectl get pod -n test
No resources found.
[root@DoM01 ~]# kubectl get pvc -n test
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound pvc-9dd04363-b482-45c6-a789-cfad170c1372 1Gi RWO nfs-client 37m
www-web-1 Bound pvc-ef7a4f64-1279-4640-813d-4a2707d61258 1Gi RWO nfs-client 37m
www-web-2 Bound pvc-0b6a3249-d2aa-4d8a-82a6-1fe78ce86999 1Gi RWO nfs-client 17m
www-web-3 Bound pvc-bdbf66e9-0a86-4e29-807c-44c634ad23f7 1Gi RWO nfs-client 17m
www-web-4 Bound pvc-9bb3fc9f-923e-46f9-b82c-66ec7414ea04 1Gi RWO nfs-client 17m
如上可見:
刪除statefulset后爱沟,默認(rèn)配置下pod會被刪除帅霜。但是pvc是不會被刪除的,需要我們手動刪除呼伸。
3. mongdb示例
3.1 HeadLessService
- yml文件
創(chuàng)建service.yml文件如下:
apiVersion: v1
kind: Service
metadata:
name: headless-mongo
namespace: test
labels:
name: mongo
spec:
selector:
role: mongo
ports:
- port: 27017
targetPort: 27017
clusterIP: None
- 創(chuàng)建service
[root@DoM01 statfulset]# kubectl create -f service.yml
[root@DoM01 statfulset]# kubectl get service -n test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
headless-mongo ClusterIP None <none> 27017/TCP 32m
3.2 StatefulSet
- yml文件
創(chuàng)建 statefulset.yml文件如下:
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongo
namespace: test
spec:
serviceName: "mongo"
replicas: 3
template:
metadata:
labels:
role: mongo
environment: test
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongo
image: harbocto.boe.com.cn/public/mongo:3.4.4
command:
- mongod
- "--replSet"
- rs0
#- "--smallfiles"
#- "--noprelloc"
resources:
limits:
cpu: "1"
memory: 2Gi
requests:
cpu: "0.2"
memory: 100Mi
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
- name: mongo-sidecar
image: harbocto.boe.com.cn/public/mongo-k8s-sidecar:latest
resources:
limits:
cpu: "1"
memory: 2Gi
requests:
cpu: "0.2"
memory: 100Mi
env:
- name: MONGO_SIDECAR_POD_LABELS
value: "role=mongo,environment=test"
- name: KUBERNETES_MONGO_SERVICE_NAME
value: "mongo"
volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
- 啟動
[root@DoM01 statfulset]# kubectl create -f statefulset.yml
[root@DoM01 statfulset]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
mongo-0 2/2 Running 0 68s
mongo-1 2/2 Running 0 62s
mongo-2 2/2 Running 0 39s
如上身冀,可見3個節(jié)點依次啟動。