目录


前言

Deployments 和 ReplicaSets 是为无状态服务而设计,StatefulSet 是为有状态服务而设计。

应用场景:

    稳定的持久化存储,即Pod重新调度后还是能访问到相同的持久化数据,基于PVC来实现

    稳定的网络标志,即Pod重新调度后其PodName和HostName不变,基于Headless Service(即没有Cluster IP的Service)来实现

    有序部署,有序扩展,即Pod是有顺序的,在部署或者扩展的时候要依据定义的顺序依次依次进行(即从0到N-1,在下一个Pod运行之前所有之前的Pod必须都是Running和Ready状态),基于init containers来实现

    有序收缩,有序删除(即从N-1到0)


参考之前的文章,首先安装nfs和storageclass。

创建statefulset :

1、nginx-statefulset-test.yaml

[root@k8s-master k8s]# cat nginx-statefulset-test.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-storageclass-service
  labels:
    app: nginx-service-test
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: nginx-service-test
  selector:
    app: nginx-service-test
  type: ClusterIP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx-statefuleset01
  labels:
    app: nginx-storageclass-test
spec:
  serviceName: "nginx-storageclass-service"
  replicas: 2
  selector:
    matchLabels:
      app: nginx-service-test
  template:
    metadata:
      labels:
        app: nginx-service-test
    spec:
      containers:
      - name: nginx-service-test
        image: nginx:latest
        ports:
        - containerPort: 80
          name: nginx-s-c
        volumeMounts:
        - name: web
          mountPath: /usr/share/nginx/html
      volumes:
      - name: web
        persistentVolumeClaim:
          claimName: pvc-test-01

2、创建

[root@k8s-master k8s]# kubectl create -f nginx-statefulset-test.yaml
service/nginx-storageclass-service created
statefulset.apps/nginx-statefuleset01 created

3、查看

[root@k8s-master k8s]# kubectl get svc
NAME                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes                   ClusterIP   10.96.0.1        <none>        443/TCP        7d1h
nginx-storageclass-service   ClusterIP   10.103.227.6     <none>        80/TCP         3m50s
[root@k8s-master k8s]# kubectl get statefulset
NAME                   READY   AGE
nginx-statefuleset01   2/2     4m19s
[root@k8s-master k8s]# kubectl get rs
NAME                                     DESIRED   CURRENT   READY   AGE
nfs-client-provisioner-test-59bf4c676f   1         1         1       13h
[root@k8s-master k8s]# kubectl get pods
NAME                                           READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-test-59bf4c676f-5wdpf   1/1     Running   0          13h
nginx-statefuleset01-0                         1/1     Running   0          13h
nginx-statefuleset01-1                         1/1     Running   0          13h

4、在nfs共享目录下创建文件

 cd /data/nfsdata/default-pvc-test-01-pvc-4cdc0549-c6ce-4f77-8816-a49df5627d98/

创建 index.html

[root@k8s-master default-pvc-test-01-pvc-4cdc0549-c6ce-4f77-8816-a49df5627d98]# cat index.html
<h1>hello,nfs storageclass !</h1>

5、访问service

可以看出,访问到的是共享目录下index.html文件

[root@k8s-master k8s]# curl 10.103.227.6:80
<h1>hello,nfs storageclass !</h1>

6、查看 statefulset 详细信息

[root@k8s-master k8s]# kubectl describe statefulset nginx-statefuleset01
Name:               nginx-statefuleset01
Namespace:          default
CreationTimestamp:  Sat, 25 Dec 2021 10:59:56 -0500
Selector:           app=nginx-service-test
Labels:             app=nginx-storageclass-test
Annotations:        <none>
Replicas:           2 desired | 2 total
Update Strategy:    RollingUpdate
  Partition:        0
Pods Status:        2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx-service-test
  Containers:
   nginx-service-test:
    Image:        nginx:latest
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:
      /usr/share/nginx/html from web (rw)
  Volumes:
   web:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  pvc-test-01
    ReadOnly:   false
Volume Claims:  <none>
Events:         <none>
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐