Kubernetes 创建nfs存储类

Kubernetes 创建 nfs 存储类

首先你需要在别的终端上创建 nfs 服务并能提供 nfs 访问
Kubernetes 不包含内部 NFS 驱动。你需要使用外部驱动为 NFS 创建 StorageClass。
https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
安装 nfs 驱动

安装 nfs 驱动

1#安装nfs客户端
2apt-get install nfs-common
3git clone https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner.git
4cd nfs-subdir-external-provisioner/deploy
5k3s kubectl create -f rbac.yaml
6vim deployment.yaml
  1. 编辑 deployment.yaml
 1apiVersion: apps/v1
 2kind: Deployment
 3metadata:
 4  name: nfs-client-provisioner
 5  labels:
 6    app: nfs-client-provisioner
 7  # replace with namespace where provisioner is deployed
 8  namespace: default
 9spec:
10  replicas: 1
11  strategy:
12    type: Recreate
13  selector:
14    matchLabels:
15      app: nfs-client-provisioner
16  template:
17    metadata:
18      labels:
19        app: nfs-client-provisioner
20    spec:
21      serviceAccountName: nfs-client-provisioner
22      containers:
23        - name: nfs-client-provisioner
24          image: k8s.gcr.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
25          volumeMounts:
26            - name: nfs-client-root
27              mountPath: /persistentvolumes
28          env:
29            - name: PROVISIONER_NAME
30              value: k8s-sigs.io/nfs-subdir-external-provisioner
31            - name: NFS_SERVER
32              value: 192.168.99.235
33            - name: NFS_PATH
34              value: /volume2/nfs-k8s
35      volumes:
36        - name: nfs-client-root
37          nfs:
38            server: 192.168.99.235
39            path: /volume2/nfs-k8s
  1. 定义存储类
1k3s kubectl create -f deployment.yaml
2k3s kubectl create -f class.yaml

测试存储是否正常

1k3s kubectl create -f test-claim.yaml -f test-pod.yaml
2k3s kubectl delete -f test-claim.yaml -f test-pod.yaml

创建有状态 pods(mysql)

创建 mysql-deployment.yaml

 1apiVersion: v1
 2kind: Service
 3metadata:
 4  name: mysql
 5spec:
 6  ports:
 7    - port: 3306
 8      targetPort: 3306
 9  selector:
10    app: mysql
11  clusterIP: None
12  type: LoadBalancer
13---
14apiVersion: apps/v1
15kind: Deployment
16metadata:
17  name: mysql
18spec:
19  selector:
20    matchLabels:
21      app: mysql
22  strategy:
23    type: Recreate
24  template:
25    metadata:
26      labels:
27        app: mysql
28    spec:
29      containers:
30        - image: mysql:5.7
31          name: mysql
32          env:
33            # Use secret in real usage
34            - name: MYSQL_ROOT_PASSWORD
35              value: password
36          ports:
37            - containerPort: 3306
38              name: mysql
39          volumeMounts:
40            - name: mysql-persistent-storage
41              mountPath: /var/lib/mysql
42      volumes:
43        - name: mysql-persistent-storage
44          persistentVolumeClaim:
45            claimName: mysql-pv-claim

创建 mysql-pv.yaml

 1apiVersion: v1
 2kind: PersistentVolume
 3metadata:
 4  name: mysql-pv-volume
 5spec:
 6  storageClassName: managed-nfs-storage
 7  capacity:
 8    storage: 20Gi
 9  accessModes:
10    - ReadWriteOnce
11  nfs:
12    server: 192.168.99.235
13    path: "/volume2/nfs-k8s"
14---
15apiVersion: v1
16kind: PersistentVolumeClaim
17metadata:
18  name: mysql-pv-claim
19spec:
20  storageClassName: managed-nfs-storage
21  accessModes:
22    - ReadWriteMany
23  resources:
24    requests:
25      storage: 20Gi

部署 mysql

1k3s kubectl apply -f mysql-pv.yaml
2k3s kubectl apply -f mysql-deployment.yaml
3k3s kubectl describe deployment mysql