k8s 配置使用 NFS 动态存储

部署 nfs 服务

1、主机安装 nfs 服务器

sudo apt-get install nfs-kernel-server
sudo apt-get install nfs-common

2、主机共享 NFS 文件夹

编辑 /etc/exports 文件

sudo vi /etc/exports
在文件末尾添加(nfs 共享目录,它可以作为开发板的根文件系统通过 nfs 挂载。):

/data1/k3snfs                *(insecure,rw,sync,no_root_squash)

修改完成后,保存。

该目录必须存在
然后启动或重启 NFS 服务。

在终端中执行如下命令,可以启动 NFS 服务:

$ sudo /etc/init.d/nfs-kernel-server start

执行如下命令则可以重新启动 NFS 服务:

$ sudo /etc/init.d/nfs-kernel-server restart

3、其他设备挂载 NFS 服务器

$ sudo mount -t nfs 192.168.12.123:/data1/k3snfs    /mnt/nfsdata -o nolock  

注意

  • 共享 NFS 文件夹权限必须为 777 ,chmod 777 Share

  • 配置文件 /etc/exports,需要加入 insecure 选项。否则其他机器挂载失败。

    mount.nfs: access denied by server while mounting 10.110.10.10:/mnt/dfs/3/k8snfs

若其他设备挂载 NFS 报错:

mount -t nfs XXX:/data1/k3snfs    /data1/k3snfs -o nolock
mount.nfs: access denied by server while mounting XXX:/data1/k3snfs

则可能需要重启 NFS 服务:

sudo /etc/init.d/nfs-kernel-server restart

新建 PV

apiVersion: v1
kind: PersistentVolume
metadata:
   name: nfs-mysqldata
spec:
   capacity:
      storage: 50Gi
   accessModes:
      - ReadWriteOnce
   persistentVolumeReclaimPolicy: Retain
   storageClassName: mysqldata
   nfs:
      path: /mnt/dfs/3/k8snfs/mysqldata
      server: hostname1

新建 PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysqldata
  namespace: manti-infra
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
  storageClassName: mysqldata

新建 Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
    name: mysql
    namespace: manti-infra
spec:
    replicas: 1
    selector:
        matchLabels:
            app: mysql
    template:
        metadata:
            labels:
                app: mysql
        spec:
            imagePullSecrets:
              - name: harborsecret
            containers:
              - name: mysql
                image: mysql:v1.0.7
                imagePullPolicy: IfNotPresent
                ports:
                  - containerPort: 3306
                env:
                  - name: MYSQL_ROOT_PASSWORD
                    value: Test321
                volumeMounts:
                  - name: mysqldata
                    subPath: mysql
                    mountPath: /var/lib/mysql
            volumes:
              - name: mysqldata
                persistentVolumeClaim:
                    claimName: mysqldata
            initContainers:
              - name: increase-fd-ulimit
                image: qz-nis-harbor.netease.com/ftd-phishing/busybox
                command: ["sh", "-c", "ulimit -n 65536"]
                securityContext:
                    privileged: true

新建

apiVersion: v1
kind: Service
metadata:
  name: mysql-out
  namespace: manti-infra
  labels:
    app: mysql-out
spec:
  type: NodePort
  ports:
    - name: mysql-out
      port: 3306
      nodePort: 30102
      targetPort: 3306
  selector:
    app: mysql
---
kind: Service
apiVersion: v1
metadata:
  name: mysql
  namespace: manti-infra
  labels:
    app: mysql
spec:
  selector:
    app: mysql
  clusterIP: None
  ports:
    - port: 3306
      name: client