10. kubernetes statefulset控制器
[TOC]
本文基于馬哥的docker和k8s視頻總結(jié), 在此致謝馬哥.
管理有狀態(tài)應(yīng)用, 非常復(fù)雜.
PetSet -> StatefulSet
- 穩(wěn)定且唯一的網(wǎng)絡(luò)標(biāo)識(shí)符
- 穩(wěn)定且持久的存儲(chǔ)
- 有序, 平滑的部署和擴(kuò)展
- 有序, 平滑地終止和刪除
- 有序的滾動(dòng)更新 (逆序, 先更新從節(jié)點(diǎn), 再更新主節(jié)點(diǎn))
三個(gè)組件: headless service
该镣、StatefulSet
啃沪、volumeClaimTemplate
volumeClaimTemplate
作用:
- 為每一個(gè)pod定義volume
- 在pod所在名稱空間自動(dòng)創(chuàng)建pvc
apiVersion: v1
kind: Service
metadata:
name: myapp
labels:
app: myapp
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: myapp-pod
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: myapp
spec:
serviceName: myapp
replicas: 3
selector:
matchLabels:
app: myapp-pod
template:
metadata:
labels:
app: myapp-pod
spec:
containers:
- name: myapp
image: nginx/myapp:v1
ports:
- containerPort: 80
name: web
volumeMounts:
- name: myappdata
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: myappdata
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 2Gi
要解析pod時(shí), pod必須跟上無頭服務(wù)的名稱: pod_name.service_name.ns_name.svc.cluster.local
例如: myapp-0.myapp.default.svc.cluster.local
支持動(dòng)態(tài)擴(kuò)展和縮減, 金絲雀發(fā)布:
kubectl scale sts myapp --replicas=5
# 或
kubectl patch sts myapp -p '{"spec":{"replicas":"5"}}'
kubectl scale sts myapp --replicas=2
# 或
kubectl patch sts myapp -p '{"spec":{"replicas":"2"}}'
# pod計(jì)數(shù)從0開始(即pod-0, pod-1, ...), 設(shè)定分區(qū)為4時(shí), pod-N, N>=4的那些pod會(huì)被更新
kubectl patch sts myapp -p '{"spec":{"updataStrategy":{"rollingUpdate":{"partition":4}}}}'
kubectl set image sts/myapp myapp=nginx/myapp:v2 # 更新鏡像
# 然后測(cè)試, 如果通過驗(yàn)證則將partition的值改為0, 讓所有pod都更新
kubectl patch sts myapp -p '{"spec":{"updataStrategy":{"rollingUpdate":{"partition":0}}}}'