pv和StatefulSet

持久化演示说明-NFS

NFS 是Network File System的缩写,即网络文件系统。功能是通过网络让不同的机器、不同的操作系统能够彼此分享文件,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在类Unix系统间实现磁盘文件共享的一种方法。

1.安装nfs服务器

node1节点(ip:192.168.10.207):(如果不安装nfs-common 也能成功)

 yum install -y nfs-common nfs-utils rpcbind
[root@node01 /]# mkdir /nfs{1..3}
[root@node01 /]# ls

bin  boot  data  dev  etc  home  lib  lib64  media  mnt  nfs  nfs1  nfs2  nfs3  opt  proc  qj  root  run  sbin  srv  sys  tmp  usr  var
[root@node01 /]# chmod 777 nfs1/ nfs2/ nfs3/
[root@node01 /]# chown nfsnobody nfs1/ nfs2/ nfs3/

[root@node01 /]# ls
bin  boot  data  dev  etc  home  lib  lib64  media  mnt  nfs  nfs1  nfs2  nfs3  opt  proc  qj  root  run  sbin  srv  sys  tmp  usr  var

[root@node01 nfs2]# cat /etc/exports
/nfs *(insecure,rw,async,no_root_squash)
/nfs1 *(insecure,rw,async,no_root_squash)
/nfs2 *(insecure,rw,async,no_root_squash)
/nfs3 *(insecure,rw,async,no_root_squash)

[root@node01 /]# systemctl restart rpcbind
[root@node01 /]# systemctl restart nfs

检查
master节点:(如果不安装nfs-common 也能成功)

yum install -y nfs-common nfs-utils rpcbind
mkdir /test
systemctl restart rpcbind
systemctl restart nfs
mount -t nfs 192.168.10.207:nfs1 /test/

不会报错即mount成功

cd /test
echo "111" > 111.html

到node1节点的nfs1目录下查看可看到新增文件111.html

master节点解除挂载:

umount /test

2.部署pvc

pv资源:

[root@apiserver pv]# cat pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /nfs1
    server: 192.168.10.207

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0002
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /nfs2
    server: 192.168.10.207

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /nfs3
    server: 192.168.10.207
kubectl create -f pv.yaml 

资源说明:
访问方式为

ReadWriteOnce–该卷可以通过单个节点以读写方式安装
ReadOnlyMany –该卷可以被许多节点只读安装
ReadWriteMany –该卷可以被许多节点读写安装
在CLI中,访问模式缩写为:

RWO-ReadWriteOnce
ROX-ReadOnlyMany
RWX-ReadWriteMany

Reclaim Policy
目前的回收政策是

Retain – manual reclamation
Recycle – basic scrub (“rm -rf /thevolume/*”)
Delete – associated storage asset such as AWS EBS, GCE PD, Azure Disk, or OpenStack Cinder volume is deleted
目前,只有NFS和HostPath支持回收。 AWS EBS,GCE PD,Azure Disk和Cinder卷支持删除

Phase
卷将处于以下阶段之一

Available – a free resource that is not yet bound to a claim
Bound– the volume is bound to a claim
Released– the claim has been deleted, but the resource is not yet reclaimed by the cluster
Failed– the volume has failed its automatic reclamation

statfulset资源:
[root@apiserver pv]# cat stateful.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"  #声明它属于哪个Headless Service.
  replicas: 3 # by default is 1
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 3
      containers:
      - name: nginx
        image: nginx:v1
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:   #可看作pvc的模板
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "nfs"  #存储类名,改为集群中已存在的
      resources:
        requests:
          storage: 5Gi
kubectl create -f stateful.yaml 

这时pod会一次运行根据 accessModes: [ “ReadWriteOnce” ] \storageClassName: “nfs” 还有必须比存储资源 storage: 5Gi大,(优先选择小的) 匹配pv.

中文官网: https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-persistent-volume-storage/

https://kubernetes.io/zh/docs/concepts/storage/persistent-volumes/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值