配置K8S的 NFS-Storageclass共享存储

目录

一.安装nfs

二.配置动态供应的默认存储类(nfs)

三.测试


本次我使用的系统是Ubuntu16.04版本

一.安装nfs

在nfs主节点安装

apt install nfs-kernrl-server rpcbind
或者
aptitude install nfs-kernel-server rpcbind

在nfs-node节点安装

apt install nfs-common rpcbind

检测是否安装

dpkg -s nfs-kernel-server
dpkg -s rpcbind
dpks -s nfs-common

nfs主节点

vim /etc/exports

末尾添加

$需要共享的目录 *(insecure,rw,sync,no_root_squash)

mkdir $需要共享的目录

重启一下服务

systemctl enable rpcbind
systemctl enable nfs-server
systemctl start rpcbind
systemctl start nfs-server

使nfs配置生效

exportfs -rv

node节点将主节点的nfs目录挂载到本机

showmount -e $主节点ip
mkdir -p 要挂载的目录
mount -t nfs $主节点ip:$主节点共享的目录 $node节点上要挂载的目录

二.配置动态供应的默认存储类(nfs)

vim nfs-storageclass.yaml

## 创建了一个存储类
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
parameters:
  archiveOnDelete: "true"  ## 删除pv的时候,pv的内容是否要备份

---
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: registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2
          # resources:
          #    limits:
          #      cpu: 10m
          #    requests:
          #      cpu: 10m
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: k8s-sigs.io/nfs-subdir-external-provisioner
            - name: NFS_SERVER
              value: $主节点ip ## 指定自己nfs服务器地址
            - name: NFS_PATH  
              value: $目录  ## nfs服务器共享的目录
      volumes:
        - name: nfs-client-root
          nfs:
            server: $主节点ip
            path: $目录
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "list", "watch"]
  - 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: default
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: default
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: default
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io

kubectl apply -f nfs-storageclass.yaml

查看配置是否生效

kubectl get sc

三.测试

上测试脚本

vim tset.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: testnfs
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 200Mi
  storageClassName: #不指定会调用默认StorageClass

查看是否创建成功

kubcectl get pv

kubcectl get pvc

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5b285bK45ZCW,size_20,color_FFFFFF,t_70,g_se,x_16

你可能感兴趣的:(K8S,云原生,kubernetes,容器,kubeadm,devops)