• 部署有狀態應用
• 解決Pod獨立生命週期,保持Pod啓動順序和惟一性
1. 穩定,惟一的網絡標識符,持久存儲
2. 有序,優雅的部署和擴展、刪除和終止
3. 有序,滾動更新html
應用場景:數據庫nginx
常規的service
service:一組pod訪問策略,提供負載均衡和服務發現
其餘:service 會分配一個 CLUSTER-IP 虛擬IP 使整個容器進行通訊。web
headless service:無頭服務
headless service:與service相似,不一樣點在於clusterIP爲None數據庫
須要部署一個dns服務器
http://www.javashuo.com/article/p-kddzetdp-ch.htmlvim
建立有狀態應用
一、vim sts.yamlapi
apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports: - port: 80 name: web clusterIP: None selector: app: nginx --- apiVersion: apps/v1beta1 kind: StatefulSet metadata: name: nginx-statefulset namespace: default spec: # 指定使用的service serviceName: nginx replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80
二、建立容器服務器
kubectl create -f sts.yaml
三、查看建立容器以及service、經過dns名稱,保證每一個固定的身份(pod/nginx-statefulset-x)標識。
kubectl get pods,svc網絡
NAME READY STATUS RESTARTS AGE # statefulset-x 爲身份標識 pod/nginx-statefulset-0 1/1 Running 0 16s pod/nginx-statefulset-1 1/1 Running 0 13s pod/nginx-statefulset-2 1/1 Running 0 10s pod/sh-77649dbd59-ppfbx 1/1 Running 0 21m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 5d16h service/nginx ClusterIP None <none> 80/TCP 26s
四、臨時啓動程序測試解析,經過dns解析惟一標識的容器
kubectl run --image=busybox:1.28.4 -it shapp
/ # nslookup nginx-statefulset-0 Server: 10.0.0.2 Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local nslookup: can't resolve 'nginx-statefulset-0'
StatefulSet與Deployment區別:有身份的!
身份三要素:
• 域名
• 主機名
• 存儲(PVC)負載均衡
ClusterIP A記錄格式:<service-name>.<namespace-name>.svc.cluster.localClusterIP=None A記錄格式:<statefulsetName-index>.<service-name>.svc.cluster.local示例:web-0.nginx.default.svc.cluster.local