创建无头服务
[kubeadm@server2 statefulset]$ cat service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
开启一个nginx服务
[kubeadm@server2 statefulset]$ kubectl apply -f service.yaml
service/nginx created
[kubeadm@server2 statefulset]$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d
nginx ClusterIP None <none> 80/TCP 7s
[kubeadm@server2 statefulset]$ cat deployment.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
[kubeadm@server2 statefulset]$ kubectl apply -f deployment.yaml
statefulset.apps/web created
[kubeadm@server2 statefulset]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-55d87b5996-pjcg8 1/1 Running 1 6h13m
web-0 1/1 Running 0 6m1s
web-1 1/1 Running 0 5m49s
[kubeadm@server2 statefulset]$ kubectl describe service nginx
Name: nginx
Namespace: default
Labels: app=nginx
Annotations: Selector: app=nginx
Type: ClusterIP
IP: None
Port: web 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.108:80,10.244.2.97:80
Session Affinity: None
Events: <none>
将我们的deployment.yaml中的replicas: 0改为0时,statefulset会自动回收pod,将pod重建之后名称不变(还是web0和web1)但是ip变了。
StatefulSet将应用状态抽象成了两种情况:
拓扑状态:应用实例必须按照某种顺序启动。新创建的Pod必须和原来Pod的网络标识一样
存储状态:应用的多个实例分别绑定了不同存储数据。
StatefulSet给所有的Pod进行了编号,编号规则是: ( s t a t e f u l s e t 名 称 ) − (statefulset名称)- (statefulset名称)−(序号),从0开始。
Pod被删除后重建,重建Pod的网络标识也不会改变,Pod的拓扑状态按照Pod的“名字+编号”的方式固定下来,并且为每个Pod提供了一个固定且唯一的访问入口,即Pod对应的DNS记录。
[kubeadm@server2 statefulset]$ kubectl run test --image=busyboxplus -itv
/ # nslookup web-0.nginx.default.svc.cluster.local # 访问web0
(pod名称+服务名称+默认命名空间+默认域)
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: web-0.nginx.default.svc.cluster.local
Address 1: 10.244.1.111 web-0.nginx.default.svc.cluster.local
/ # nslookup web-1.nginx.default.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: web-1.nginx.default.svc.cluster.local
Address 1: 10.244.2.105 web-1.nginx.default.svc.cluster.local
/ # curl web-0.nginx # 可以解析
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[kubeadm@server2 statefulset]$ kubectl delete -f deployment.yaml
[kubeadm@server2 statefulset]$ cat deployment.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
storageClassName: managed-nfs-storage
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100M
[kubeadm@server2 statefulset]$ kubectl apply -f deployment.yaml
nslookup用于查询DNS的记录,查询域名解析是否正常,在网络故障时用来诊断网络问题
[kubeadm@server2 statefulset]$ kubectl attach test -it
Defaulting container name to test.
Use 'kubectl describe pod/test -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
/ # nslookup web-0.nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: web-0.nginx
Address 1: 10.244.1.117 web-0.nginx.default.svc.cluster.local
/ # nslookup web-1.nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: web-1.nginx
Address 1: 10.244.2.108 web-1.nginx.default.svc.cluster.local
在web-0和web-1的挂载卷中(挂载卷在172.25.60.1上)写入index.html文件
[root@server1 nfs]# pwd
/nfs
[root@server1 nfs]# ls
default-test-claim-pvc-d5f14819-6661-43a4-b5ce-49e821c28a78 default-www-web-1-pvc-c2de0fa7-cb0b-49e2-b7f8-4dc1c41d950b
default-www-web-0-pvc-c603df89-c538-4dc6-92d0-9c30c3a4f72c
[root@server1 nfs]# cat default-www-web-0-pvc-c603df89-c538-4dc6-92d0-9c30c3a4f72c/index.html
web-0
[root@server1 nfs]# cat default-www-web-1-pvc-c2de0fa7-cb0b-49e2-b7f8-4dc1c41d950b/index.html
web-1
[kubeadm@server2 statefulset]$ kubectl attach test -it
Defaulting container name to test.
Use 'kubectl describe pod/test -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
/ # nslookup nginx # 可以直接访问nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: nginx
Address 1: 10.244.2.108 web-1.nginx.default.svc.cluster.local
Address 2: 10.244.1.117 web-0.nginx.default.svc.cluster.local
/ # nslookup nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: nginx
Address 1: 10.244.1.117 web-0.nginx.default.svc.cluster.local
Address 2: 10.244.2.108 web-1.nginx.default.svc.cluster.local
# curl是一个命令行工具,通过指定的URL来上传或下载数据,并将数据展示出来。curl中的c表示client,而URL,就是URL。
/curl nginx
web-0
/ # curl nginx
web-1
注意:在删除StatefulSet类型的文件时,将replicas设置为0,会自动回收pod
[kubeadm@server2 statefulset]$ cat deployment.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 0
[kubeadm@server2 statefulset]$ kubectl delete -f deployment.yaml
statefulset.apps "web" deleted
Pod的创建也是严格按照编号顺序进行的。比如在web-0进入到running状态,并且Conditions为Ready之前,web-1一直会处于pending状态。
StatefulSet还会为每一个Pod分配并创建一个同样编号的PVC。这样,kubernetes就可以通过Persistent Volume机制为这个PVC绑定对应的PV,从而保证每一个Pod都拥有一个独立的Volume。
首先,想要弹缩的StatefulSet. 需先清楚是否能弹缩该应用.
[kubeadm@server2 statefulset]$ kubectl get statefulset
NAME READY AGE
web 0/0 59m
$ kubectl scale statefulsets <stateful-set-name> --replicas=<new-replicas>
如果StatefulSet开始由 kubectl apply 或 kubectl create --save-config 创建,更新StatefulSet manifests中的 .spec.replicas(更改replicas的数目), 然后执行命令 kubectl apply:
$ kubectl apply -f <stateful-set-file-updated>
kubectl edit statefulsets <stateful-set-name>
$ kubectl patch statefulsets <stateful-set-name> -p '{"spec":{"replicas":}}'