• 部署有状态应用
• 解决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