k8s存储配置-动态分配存储、普通卷挂载

一、动态分配存储

1.nfs制备器

nfs-rbac.yaml

cat >nfs-rbac.yaml<<-EOF
---
kind: ServiceAccount
apiVersion: v1
metadata:
  name: nfs-client-provisioner
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    namespace: kube-system				######注意命名空间
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
rules:
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: kube-system
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io
EOF
provisioner.yaml
cat >nfs-provisioner-deploy.yaml<<-EOF
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-client-provisioner
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-client-provisioner
  strategy:
    type: Recreate  #---设置升级策略为删除再创建(默认为滚动更新)
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          #---由于quay.io仓库国内被墙,所以替换成七牛云的仓库
          #image: quay-mirror.qiniu.com/external_storage/nfs-client-provisioner:latest
          image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: nfs-client  #---nfs-provisioner的名称,以后设置的storageclass要和这个保持一致
            - name: NFS_SERVER
              value: ${NFS_ADDRESS}  #---NFS服务器地址,和 valumes 保持一致
            - name: NFS_PATH
              value: ${NFS_DIR}  #---NFS服务器目录,和 valumes 保持一致
      volumes:
        - name: nfs-client-root
          nfs:
            server: ${NFS_ADDRESS}  #---NFS服务器地址
            path: ${NFS_DIR} #---NFS服务器目录
EOF

2.pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv001
spec:
  capacity:									#容量配置
    storage: 5Gi							#容量大小
  volumeMode: Filesystem					#存储类型
  accessModes:								#访问模式 ReadWriteMany、ReadOnlyMany
    - ReadWriteOnce							#可被单节点读写
  persistentVolumeReclaimPolicy: Recycle	#回收策略   	Delete  Retain
  storageClassName: slow					#创建pv的存储类名,与pvc相同
  mountOptions:								#加载配置
    - hard
    - nfsvers=4.1							#nfs版本
  nfs:										#连接nfs
    path: '存储路径'
    server: nfsip

3.pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: pvc
spec:
  accessModes:								#访问模式 ReadWriteMany、ReadOnlyMany
    - ReadWriteOnce							#与对应的pv相同
  volumeMode: Filesystem
  resources:
    requests:
      storage: 5Gi							#小于等于pv
  storageClassName: slow

4.storageClass.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
provisioner: nfs-client			#外部制备器提供者,提供者名称
parameters:
  archiveOnDelete: "false"			#是否存档
reclaimPolicy: Retain
volumeBindingMode: Immediate		#pvc立即绑定

二、普通卷挂载

volumes.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - image: nginx
    name: nginx-volume
    volumeMounts:
    - mountPath: /test				####容器目录
      name: test-volume				
  volumes:
  - name: test-volume				
  
    hostPath:
      path: /data					####node目录
      type: DirectoryOrCreate		##检查类型
      
    #emptyDir: {}			######应用于两个容器共享目录操作
    
    #nfs:					####nfs共享挂载
      #server: ip			##nfs服务ip
      #path: nfs共享目录
      #readOnly: false
      
    #persistentVolumeClaim:	##关联pvc
      #claimName: pvc_name	##关联哪个pvc	

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值