阿汤博客-承接中小企业服务器维护和网站维护,有意者可以联系博主!

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

运维文档 190℃ 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

/var/data 192.168.15.0/24(rw,async)


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

#cat nfs-rbac.yml

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-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"]
  - apiGroups: [""]
    resources: ["services", "endpoints"]
    verbs: ["get"]
  - apiGroups: ["extensions"]
    resources: ["podsecuritypolicies"]
    resourceNames: ["nfs-provisioner"]
    verbs: ["use"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-provisioner
     # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-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-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: Role
  name: leader-locking-nfs-provisioner
  apiGroup: rbac.authorization.k8s.io

3、安装nfs-client-provisioner

#cat nfs-provisioner.yml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-provisioner
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: nfs-provisioner
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-provisioner
    spec:
      serviceAccount: nfs-provisioner
      containers:
        - name: nfs-provisioner
          image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner
          volumeMounts:
            - name: nfs-elk-logs
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: nfs-logs
            - name: NFS_SERVER
              value: 192.168.15.100
            - name: NFS_PATH
              value: /var/data
      volumes:
        - name: nfs-elk-logs
          nfs:
            server: 192.168.15.100
            path: /var/data

4、创建storageclass

#cat nfs-class.yml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: nfs-elk
provisioner: nfs-logs


5、创建PVC测试

#cat nfs-pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: elk-pvc
  annotations:  
    volume.beta.kubernetes.io/storage-class: "nfs-elk"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi


6、查看运行状态

#kubectl get pod,pvc,sc

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


#ls /var/data/  

default-elk-pvc-pvc-2b76e59a-f3b3-11e8-bbd5-000c29731f53

#查看共享目录已经生产volume

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

喜欢 (0)or分享 (0)