09-kubernetes StatefulSet

目录

  • StatefulSet
  • 简单测试 使用 StatefulSet
    • 创建基础的PV
    • StatefulSet 清单

StatefulSet

有状态应用副本集

无状态的, 更关注的是群体

有状态的, 更关注的是个体

有状态应用集的特点:

  1. 稳定且需要唯一的网络标识符;
  2. 稳定且持久的存储;
  3. 要求有序, 平滑的部署和扩展;
  4. 要求有序, 平滑的删除和终止;
  5. 有序的滚动更新;

需要有三个组件组成:

  1. headless service
  2. StatefulSet 控制器
  3. volumeClaimTemplate 存储卷申请模板

简单测试 使用 StatefulSet

创建基础的PV

[root@master configmap]# cat ../volume/pv-demo.yaml 
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv001
  labels:
    name: pv001
spec:
  nfs:
    path: /data/volumes/v1
    server: 10.0.20.20
  accessModes: ["ReadWriteMany", "ReadWriteOnce"]
  capacity:
    storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv002
  labels:
    name: pv002
spec:
  nfs:
    path: /data/volumes/v2
    server: 10.0.20.20
  accessModes: ["ReadWriteOnce"]
  capacity:
    storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv003
  labels:
    name: pv003
spec:
  nfs:
    path: /data/volumes/v3
    server: 10.0.20.20
  accessModes: ["ReadWriteMany", "ReadWriteOnce"]
  capacity:
    storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv004
  labels:
    name: pv004
spec:
  nfs:
    path: /data/volumes/v4
    server: 10.0.20.20
  accessModes: ["ReadWriteMany", "ReadWriteOnce"]
  capacity:
    storage: 10Gi
---
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv005
  labels:
    name: pv005
spec:
  nfs:
    path: /data/volumes/v5
    server: 10.0.20.20
  accessModes: ["ReadWriteMany", "ReadWriteOnce"]
  capacity:
    storage: 10Gi
[root@master volume]# kubectl apply -f pv-demo.yaml 
persistentvolume/pv001 created
persistentvolume/pv002 created
persistentvolume/pv003 created
persistentvolume/pv004 created
persistentvolume/pv005 created
[root@master volume]# kubectl get pv -o wide
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE   VOLUMEMODE
pv001   5Gi        RWO,RWX        Retain           Available                                   5s    Filesystem
pv002   5Gi        RWO            Retain           Available                                   5s    Filesystem
pv003   5Gi        RWO,RWX        Retain           Available                                   5s    Filesystem
pv004   10Gi       RWO,RWX        Retain           Available                                   5s    Filesystem
pv005   10Gi       RWO,RWX        Retain           Available                                   5s    Filesystem

StatefulSet 清单

[root@master manifests]# cat statefulset-demo.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myapp-svc
  namespace: default
  labels:
    app: myapp                  # service 名称
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None               # 配置headless service
  selector:
    app: myapp-pod              # 匹配Pod 标签
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myapp
spec:
  serviceName: myapp            # 名称
  replicas: 3                   # 三个副本
  selector:
    matchLabels:
      app: myapp-pod            # 匹配Pod 
  template:
    metadata:
      labels:
        app: myapp-pod
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: myappdata
          mountPath: /usr/shar/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: myappdata                   # pvc名称
    spec:
      accessModes: ["ReadWriteOnce"]    # 权限
      resources:
        requests:
          storage: 5Gi                  # pv 大小

创建

[root@master manifests]# kubectl apply -f statefulset-demo.yaml 
service/myapp-svc unchanged
statefulset.apps/myapp created
[root@master manifests]# kubectl get sts
NAME    READY   AGE
myapp   3/3     5s
[root@master manifests]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
myapp-0   1/1     Running   0          92s
myapp-1   1/1     Running   0          91s
myapp-2   1/1     Running   0          32s

StatefulSet 会自动创建pvc, 然后去绑定对应符合要求的PV

[root@master manifests]# kubectl get pvc
NAME                STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
myappdata-myapp-0   Bound    pv002    5Gi        RWO                           2m42s
myappdata-myapp-1   Bound    pv003    5Gi        RWO,RWX                       66s
myappdata-myapp-2   Bound    pv001    5Gi        RWO,RWX                       7s
[root@master manifests]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                       STORAGECLASS   REASON   AGE
pv001   5Gi        RWO,RWX        Retain           Bound       default/myappdata-myapp-2                           20m
pv002   5Gi        RWO            Retain           Bound       default/myappdata-myapp-0                           20m
pv003   5Gi        RWO,RWX        Retain           Bound       default/myappdata-myapp-1                           20m
pv004   10Gi       RWO,RWX        Retain           Available                                                       20m
pv005   10Gi       RWO,RWX        Retain           Available                                                       20m

最后, 声明式创建的, 可以使用 kubectl patch 命令 或者 kubectl set image 等命令对StatefulSet进行修改.

转载于:https://www.cnblogs.com/winstom/p/11316616.html

你可能感兴趣的:(运维)