k8s动态存储(SC&PVC)

这里以NFS文件存储测试

相关搭建可以参考: NFS安装与配置

查看nfs信息:

# 每个node节点需要安装nfs工具集
yum install nfs-utils

showmount -e 192.168.104.26

192.168.104.26:/data

安装nfs插件

https://github.com/kubernetes-retired/external-storage/tree/master/nfs-client/deploy

配置rbac权限rbac.yaml

kubectl apply -f rbac.yaml

部署nfs-client-provisionerdeployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: quay.io/external_storage/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs
            - name: NFS_SERVER
              value: 192.168.104.26
            - name: NFS_PATH
              value: /data/k8s-storage
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.104.26
            path: /data/k8s-storage

# 部署
kubectl apply -f deployment.yaml
  • NFS_SERVER: 换成自己的nfs服务器地址
  • NFS_PATH: 换成nfs文件路径

部署StorageClass(sc)class.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"
  
# 部署
kubectl apply -f class.yaml

部署PersistentVolumeClaim(pvc)文件test-claim.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-02
  annotations:
    volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
      
# 部署
kubectl apply -f test-claim.yaml
  • annotations: 使用的StorageClass
  • storage: 申请的空间大小

pvc会根据sc模板, 从nfs文件中创建空间为1G的文件目录

pod使用

创建部署文件

vim my-nginx
###################################
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      run: my-nginx
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: "/opt"
          name: logs
      volumes:
      - name: logs
        persistentVolumeClaim:
          claimName: pvc-02
  • mountPath: pod绑定的存储路径
  • claimName: 声明的存储名称

执行

kubectl apply -f my-nginx.yaml

检验

确保容器已启动

kubectl get po | grep my-nginx

每台node主机一定要安装nfs-utils, 且确保nfs服务器存在/data/k8s-storage

新增文件测试

# 往其中一个pod写入文件并在另一个pod中查看

# 查看nfs文件目录

展示评论