阿汤博客-承接Linux服务器维护、服务器配置与管理,有意者可以联系博主!

Kubernetes使用StorageClass动态生成NFS类型的PV

运维文档 1545℃ 0评论

官方文档:https://github.com/kubernetes-incubator/external-storage/tree/master/nfs

以自己单独安装NFS系统为例

1、安装nfs设置共享(生产环境建议使用云服务商提供的云共享存储如:阿里云的NAS,AWS的EBS)

#yum install nfs-utils  #每个节点安装

#systemctl enable nfs

#cat /etc/exports

/data/data/nfs 192.168.1.0/24(rw,async)

2、安装rbac,不安装nfs-provisioner会报错没有权限

#cat nfs-rbac.yml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: atang
---
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
    # replace with namespace where provisioner is deployed
    namespace: atang
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
  # replace with namespace where provisioner is deployed
  namespace: atang
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
  # replace with namespace where provisioner is deployed
  namespace: atang
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: atang
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io

3、安装nfs-client-provisioner

#cat nfs-provisioner.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: atang
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: www.amd5.cn/nfs
            - name: NFS_SERVER
              value: 192.168.1.222
            - name: NFS_PATH
              value: /data/data/nfs
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.1.222
            path: /data/data/nfs

4、创建storageclass

#cat nfs-class.yml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: atang-nfs
  namespace: atang
provisioner: www.amd5.cn/nfs
mountOptions:
  - vers=4.1
parameters:
  archiveOnDelete: "false"

5、创建PVC测试

#cat nfs-pvc.yml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-nfs
  annotations:
    volume.beta.kubernetes.io/storage-class: "atang-nfs"
  namespace: atang
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Mi

6、查看运行状态

#kubectl get pvc,sc,sa -n atang

Kubernetes使用StorageClass动态生成NFS类型的PV

转载请注明:阿汤博客 » Kubernetes使用StorageClass动态生成NFS类型的PV

喜欢 (0)or分享 (0)