K8s使用storageclass动态创建PV(NFS存储)

部署nfs-server

  • yum 安装nfs
    yum install rpcbind nfs-utils -y
    systemctl enable rpcbind
    systemctl enable nfs
    systemctl start rpcbind
    systemctl start nfs
    
    
  • 配置nfs
mkdir -p /data/nfs
echo '/data/nfs  <客户端节点 ip或访问网段1>/24(rw,sync,no_subtree_check,no_root_squash) <客户端节点 ip或访问网段2>/24(rw,sync,no_subtree_check,no_root_squash)
'  >>/etc/exports
systemctl restart nfs
  • 验证nfs
    exportfs   -v  
    showmount -e <服务器 ip>
    
    

k8s 集群部署nfs-client-provisioner

rabc.yaml
kind: ServiceAccount
apiVersion: v1
metadata:
 name: nfs-client-provisioner
 namespace: kube-system
---
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:
 namespace: kube-system
 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:
 namespace: kube-system
 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
deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
 name: nfs-client-provisioner
 namespace: kube-system
spec:
 replicas: 2
 strategy:
   type: Recreate
 selector:
   matchLabels:
     app: nfs-client-provisioner
 template:
   metadata:
     labels:
       app: nfs-client-provisioner
   spec:
     serviceAccountName: nfs-client-provisioner
     affinity:
       podAntiAffinity:
         preferredDuringSchedulingIgnoredDuringExecution:
         - weight: 100
           podAffinityTerm:
             topologyKey: kubernetes.io/hostname
             labelSelector:
               matchLabels:
                 app: nfs-client-provisioner
       # nodeAffinity:
         # requiredDuringSchedulingIgnoredDuringExecution:
           # nodeSelectorTerms:
           # - matchExpressions:
             # - key: dedicated
               # operator: In
               # values:
               # - "cmp"
     containers:
       - name: nfs-client-provisioner
         image: quay.io/external_storage/nfs-client-provisioner:v3.1.0-k8s1.11
         volumeMounts:
           - name: nfs-client-root
             mountPath: /persistentvolumes
         env:
           - name: PROVISIONER_NAME
             value: nfs-client-provisioner
           - name: NFS_SERVER
             value: 10.127.40.182  #nfs服务器的地址
           - name: NFS_PATH
             value: /data/nfs #nfs的export 的目录
     volumes:
       - name: nfs-client-root
         nfs:
           server: 10.127.40.182   #nfs服务器的地址
           path: /data/nfs #nfs的export 的目录
storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"storage.k8s.io/v1","kind":"StorageClass","metadata":{"annotations":{},"name":"nfs"},"provisioner":"nfs-client-provisioner","reclaimPolicy":"Delete"}
    storageclass.beta.kubernetes.io/is-default-class: "true"
    storageclass.kubernetes.io/is-default-class: "true"
  name: nfs
provisioner: nfs-client-provisioner
reclaimPolicy: Delete #有两种选择, Delete是PV删除以后,文件也从NFS里面删除;Retain是保留文件
test-nginx-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
    volumeMounts:
      - name: www
        mountPath: /usr/share/nginx/html
  volumes:
    - name: www
      persistentVolumeClaim:
        claimName: nginx
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx
spec:
  storageClassName: "nfs"
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

FAQ

  • Kubernetes v1.20.0 报"unexpected error getting claim reference: selfLink was empty, can’t make reference"
1. kubeadm 部署的方式
当前的解决方法是编辑/etc/kubernetes/manifests/kube-apiserver.yaml
在这里:
spec:
 containers:
 - command:
   - kube-apiserver
添加这一行:
- --feature-gates=RemoveSelfLink=false
2. 用rancher 容器的部署的方式
 kube-api:
     always_pull_images: false
     extra_args:
       feature-gates=RemoveSelfLink: 'false' 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值