1、有状态部署,无头servic
#创建yaml,应用sts
[root@k8s-master ~]# cat sts.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx-statefulset
namespace: default
spec:
serviceName: nginx
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.14
name: nginx
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl apply -f sts.yaml
service/nginx created
statefulset.apps/nginx-statefulset created
#pod名称唯一性
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 1/1 Running 0 88s 172.28.82.210 k8s-master <none> <none>
nginx-statefulset-0 1/1 Running 0 14m 172.28.82.205 k8s-master <none> <none>
nginx-statefulset-1 1/1 Running 0 14m 172.26.139.63 k8s-node <none> <none>
nginx-statefulset-2 1/1 Running 0 14m 172.28.82.206 k8s-master <none> <none>
#无头service
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d22h
nginx ClusterIP None <none> 80/TCP 2m3s
2、扩容
[root@k8s-master yaml]# kubectl scale statefulset nginx-statefulset --replicas=5
statefulset.apps/nginx-statefulset scaled
[root@k8s-master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 1026 (23m ago) 42d
nginx-deployment-ff6774dc6-gr4gq 1/1 Running 0 29d
nginx-statefulset-0 1/1 Running 0 80s
nginx-statefulset-1 1/1 Running 0 69s
nginx-statefulset-2 1/1 Running 0 68s
nginx-statefulset-3 1/1 Running 0 3s
nginx-statefulset-4 1/1 Running 0 1s
3、缩容
[root@k8s-master yaml]# kubectl patch sts nginx-statefulset -p '{"spec":{"replicas":3}}'
statefulset.apps/nginx-statefulset patched
[root@k8s-master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 1026 (25m ago) 42d
nginx-deployment-ff6774dc6-gr4gq 1/1 Running 0 29d
nginx-statefulset-0 1/1 Running 0 3m18s
nginx-statefulset-1 1/1 Running 0 3m7s
nginx-statefulset-2 1/1 Running 0 3m6s
4、唯一域名标识(根据主机名+一定规则域名)
主机名称.service名称.名称空间.svc.cluster.local
eg:nginx-statefulset-0.nginx.default.svc.cluster.local
[root@k8s-master ~]# kubectl exec busybox -n default -- nslookup nginx-statefulset-0.nginx.default
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: nginx-statefulset-0.nginx.default
Address 1: 172.28.82.205 nginx-statefulset-0.nginx.default.svc.cluster.local