k8s笔记之StatefulSet

StatefulSet是为了解决有状态服务的问题(对应Deployments和ReplicaSets是为无状态服务而设计),其应用场景包括
1、稳定的持久化存储,即Pod重新调度后还是能访问到相同的持久化数据,基于PVC来实现
2、稳定的网络标志,即Pod重新调度后其PodName和HostName不变,基于Headless Service(即没有Cluster IP的Service)来实现
3、有序部署,有序扩展,即Pod是有顺序的,在部署或者扩展的时候要依据定义的顺序依次依次进行(即从0到N-1,在下一个Pod运行之前所有之前的Pod必须都是Running和Ready状态),基于init containers来实现有序收缩,有序删除(即从N-1到0)

1.(提前准备好nfs存储,之前有提过)
创建四个目录作为pv,分别对应于pv01、pv02、pv03、pv04

[root@master-01 statefulset]# ls /data/

 pv-volume pv-volume2 pv-volume-test1 pv-volume-test2

2.创建pv

[root@master-01 statefulset]# cat pv.yaml 
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv01
  labels:
    nfs-label: pv01
    name: pv01
spec:
  accessModes: ["ReadWriteMany"]
  capacity:
    storage: 2Gi

  nfs:
    server: 10.1.1.10    
    path:  /data/pv-volume
  storageClassName: local-storage
---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv02
  labels:
    nfs-label: pv02
    name: pv02
spec:
  accessModes: ["ReadWriteMany"]
  capacity:
    storage: 2Gi

  nfs:
    server: 10.1.1.10
    path:  /data/pv-volume2
  storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv03
  labels:
    nfs-label: pv03
    name: pv03
spec:
  accessModes: ["ReadWriteMany"]
  capacity:
    storage: 2Gi

  nfs:
    server: 10.1.1.10
    path:  /data/pv-volume-test1
  storageClassName: local-storage
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv04
  labels:
    nfs-label: pv04
    name: pv04
spec:
  accessModes: ["ReadWriteMany"]
  capacity:
    storage: 2Gi

  nfs:
    server: 10.1.1.10
    path:  /data/pv-volume-test2
  storageClassName: local-storage

[root@master-01 statefulset]# kubectl apply -f pv.yaml 
persistentvolume/pv01 created
persistentvolume/pv02 created
persistentvolume/pv03 created
persistentvolume/pv04 created
[root@master-01 statefulset]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS    REASON   AGE
pv01   2Gi        RWX            Retain           Available           local-storage            5s
pv02   2Gi        RWX            Retain           Available           local-storage            5s
pv03   2Gi        RWX            Retain           Available           local-storage            5s
pv04   2Gi        RWX            Retain           Available           local-storage            5s

3.创建statefulset绑定pv

[root@master-01 statefulset]# cat myapp-stateful.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: myapp
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myapp-statefulset-test
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      labels:
        app: myapp
        release: canary
    spec:
      imagePullSecrets:
      - name: regsecret
      containers:
      - name: myapp
        image: "harbor-ali.abc.com/k8s_img/myapp:v1"
        imagePullPolicy: Always
        ports:
        - name: http
          containerPort: 80
        volumeMounts:
        - name: myappdata
          mountPath: /usr/share/nginx/html
  serviceName: myapp
  selector:
    matchLabels:
      app: myapp
  volumeClaimTemplates:
  - metadata:
      name: myappdata
    spec:
      storageClassName: local-storage #存储卷名与pv的存储卷名对应
      accessModes: ["ReadWriteMany"]  
      resources:
        requests:
          storage: 2Gi
[root@master-01 statefulset]# kubectl apply -f myapp-stateful.yaml 
statefulset.apps/myapp-statefulset-test created
[root@master-01 statefulset]# kubectl get sts
NAME                     READY   AGE
myapp-statefulset-test   2/2     11s
[root@master-01 statefulset]# kubectl get pvc
NAME                                 STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS    AGE
myappdata-myapp-statefulset-test-0   Bound    pv04     2Gi        RWX            local-storage   27s
myappdata-myapp-statefulset-test-1   Bound    pv01     2Gi        RWX            local-storage   23s
[root@master-01 statefulset]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                                        STORAGECLASS    REASON   AGE
pv01   2Gi        RWX            Retain           Bound       default/myappdata-myapp-statefulset-test-1   local-storage            6m31s
pv02   2Gi        RWX            Retain           Available                                                local-storage            6m31s
pv03   2Gi        RWX            Retain           Available                                                local-storage            6m31s
pv04   2Gi        RWX            Retain           Bound       default/myappdata-myapp-statefulset-test-0   local-storage            6m31s

从结果可以看出statfulset会自动创建pvc并与相应的pod绑定

查看创建的Pod,他们都是有序的

[root@master-01 statefulset]# kubectl get pod
NAME                                   READY   STATUS    RESTARTS   AGE
myapp-statefulset-test-0               1/1     Running   0          3m17s
myapp-statefulset-test-1               1/1     Running   0          3m13s

可以pv的结果可以看到pv02/pv03是空闲未被绑定的
更新pod个数把副本数改为3,stateful会自动选择合适的pvc与之绑定

[root@master-01 statefulset]# kubectl patch statefulset myapp-statefulset-test -p '{"spec":{"replicas":3}}'

statefulset.apps/myapp-statefulset-test patched
[root@master-01 statefulset]# kubectl get pod
NAME                                   READY   STATUS    RESTARTS   AGE
myapp-statefulset-test-0               1/1     Running   0          6m23s
myapp-statefulset-test-1               1/1     Running   0          6m19s
myapp-statefulset-test-2               1/1     Running   0          92s

[root@master-01 statefulset]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                                        STORAGECLASS    REASON   AGE
pv01   2Gi        RWX            Retain           Bound       default/myappdata-myapp-statefulset-test-1   local-storage            11m
pv02   2Gi        RWX            Retain           Bound       default/myappdata-myapp-statefulset-test-2   local-storage            11m
pv03   2Gi        RWX            Retain           Available                                                local-storage            11m
pv04   2Gi        RWX            Retain           Bound       default/myappdata-myapp-statefulset-test-0   local-storage            11m

pv02已绑定到新的pod

你可能感兴趣的:(k8s笔记之StatefulSet)