关于kubernetes:KubernetesStatefulSetPVPVC

55次阅读

共计 2881 个字符,预计需要花费 8 分钟才能阅读完成。

StatefulSet

适应场景:

  • [x] 稳固且惟一的网络标识符
  • [x] 稳固且长久的存储
  • [x] 有序,优雅的部署和扩大
  • [x] 有序,优雅的删除和终止
  • [x] 有序而主动的滚动更新

通用的 StatefulSet 形成:

  • [x] Headless Service
  • [x] StatefulSet
  • [x] volumeClaimTemplate

构建 NFS 文件系统

装置 NFS 文件系统

$ yum install -y nfs-common nfs-utils rpcbind
$ mkdir /nfsdata /nfs1
$ chmod 666 /nfsdata
$ chown nfsnobody /nfsdata
$ cat /etc/exports
/data/nfs1 *(rw,no_root_squash,no_all_squash,sync)
/data/nfs2 *(rw,no_root_squash,no_all_squash,sync)


$ systemctl start rpcbind
$ systemctl start nfs
#  在 kubernetes 节点或者是 minikube 主机上用工具挂载测试
$ sudo mount -t nfs 172.16.56.138:/data/nfs1 /data
$ ls /data
index.html

StatefulSet/PV/PVC 实战

创立 PV

$ cat nfs-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfspv1
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /data/nfs1
    server: 172.16.56.138
    
$ kubectl apply -f nfs-pv.yaml
$ kubectl get pv
NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
nfspv1   10Gi       RWO            Retain           Available           nfs                     5s

编排 StatefulSet/PV 资源

$cat statefulset-demo.yaml
apiVersion: v1
kind: Service
metadata:
  name: myapp-svc
  labels:
    app: myapp-svc
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: myapp-pod
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myapp
spec:
  serviceName: myapp-svc
  replicas: 2
  selector:
    matchLabels:
      app: myapp-pod
  template:
    metadata:
      labels:
        app: myapp-pod
    spec:
      containers:
      - name: myapp
        image: nginx:latest
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: myappdata
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: myappdata
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "nfs"            # 动静 pvc
      resources:
        requests:
          storage: 2Gi

#    volumes:
#      - name: myappdata
#        persistentVolumeClaim:              #指定 pvc
#          claimName: nfspv1
$ kubectl apply -f statefulset-demo.yaml
service/myapp-svc created
statefulset.apps/myapp created

$ kubectl get pv
NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                       STORAGECLASS   REASON   AGE
nfspv1   10Gi       RWO            Retain           Bound    default/myappdata-myapp-0   nfs                     31s
       
$ kubectl get pvc
NAME                STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
myappdata-myapp-0   Bound     nfspv1   10Gi       RWO            nfs            50s
myappdata-myapp-1   Pending                                      nfs            38s

$ kubectl get statefulset
NAME    READY   AGE
myapp   1/2     60s

$ kubectl get pod
NAME      READY   STATUS    RESTARTS   AGE
myapp-0   1/1     Running   0          75s
myapp-1   0/1     Pending   0          63s
$ kubectl describe pod myapp-1
...
Events:
  Type     Reason            Age        From               Message
  ----     ------            ----       ----               -------
  Warning  FailedScheduling  <unknown>  default-scheduler  error while running "VolumeBinding" filter plugin for pod "myapp-1": pod has unbound immediate PersistentVolumeClaims
  Warning  FailedScheduling  <unknown>  default-scheduler  error while running "VolumeBinding" filter plugin for pod "myapp-1": pod has unbound immediate PersistentVolumeClaims

# 测试 statefulset 是否会随着 pod 删除而完结
$ kubectl exec -ti myapp-0 -- sh
# date >/usr/share/nginx/html/index.html
# more /usr/share/nginx/html/index.html
Sun Apr 19 17:34:25 UTC 2020

$ kubectl delete pod myapp-0

# 再次检测,还是存在,未被删除
$ kubectl exec -ti myapp-0 -- sh
$ more /usr/share/nginx/html/index.html
Sun Apr 19 17:34:25 UTC 2020

# 登陆 nfs 服务器,查看挂载目录
nfs-server$ cat /data/nfs1/index.html 
Sun Apr 19 17:34:25 UTC 2020

能够发现写的数据是存在 nfs 文件系统上,当删除一个用 statefulset 创立的 pod 时候,主动重建后的数据仍然存在,阐明长久化是胜利的

【完结】

正文完
 0