The environment

The prerequisite for deploying Mysql is K8s or K3S.

[root@cs100 ~]# kubectl get nodes
NAME         STATUS   ROLES                  AGE   VERSION
k3s-master   Ready    control-plane,master   26h   v1.20.4+k3s1
Copy the code

Creating an NFS Service

Installation Command NFS

yum install nfs-utils -y
Copy the code

Set the mount path

vim /etc/exports
/root/data/nfs *(rw,no_root_squash)
Copy the code

Create a mount path

mkdir /root/data/nfs
Copy the code

Start the NFS

systemctl start nfs

ps -ef | grep nfs
Copy the code

Mysql

Persistent Volume and Persistent Volume Claim are storage resource volumes that manage Persistent services. So we’re going to create PV, and then WE’re going to create PVC, and we’re going to bind the two.

mysql-nfs-pv.yaml

/root/data/ NFS /mysql Create the mysql folder in advance

cat > /root/k3s_yaml/mysql57_nfs/mysql-nfs-pv.yaml  <<EOF
kind: PersistentVolume
apiVersion: v1
metadata:
  name: mysql-nfs-pv
  namespace: db-mysql
spec:
  accessModes:
    - ReadWriteOnce      
  capacity:
    storage: 1Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /root/data/nfs/mysql
    server: 192.168.72.100

EOF
Copy the code

mysql-nfs-pvc.yaml

cat > /root/k3s_yaml/mysql57_nfs/mysql-nfs-pvc.yaml <<EOF

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-nfs-pvc
  namespace: db-mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs 

EOF
Copy the code

mysql-nfs-deploy.yaml

cat > /root/k3s_yaml/mysql57_nfs/mysql-nfs-deploy.yaml <<EOF apiVersion: apps/v1 kind: Deployment metadata: name: mysql-nfs-deploy namespace: db-mysql spec: replicas: 1 selector: matchLabels: app: mysql_nfs template: metadata: Labels: app: mysql_NFS spec: containers: -name: mysql57 image: mysql:5.7 imagePullPolicy: IfNotPresent env: -name: labels: app: mysql_nfs spec: containers: -name: mysql57 image: mysql:5.7 imagePullPolicy: IfNotPresent env: MYSQL_ROOT_PASSWORD value: "123456" args: - --lower_case_table_names=1 ports: - containerPort: 3306 volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-nfs-pvc EOFCopy the code

mysql-nfs-svc.yaml

cat > /root/k3s_yaml/mysql57_nfs/mysql-nfs-svc.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: db-mysql
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: 32306
  selector:
    app: mysql_nfs

EOF
Copy the code

Creating a namespace

kubectl create namespace db-mysql
Copy the code

Execute YAML separately

kubectl create -f mysql-nfs-pv.yaml kubectl create -f mysql-nfs-pvc.yaml kubectl create -f mysql-nfs-deploy.yaml kubectl  create -f mysql-nfs-svc.yamlCopy the code

Check the poddb-mysql mysql-nfs-deploy-5f4fc57696-7cjkn

[root@cs100 mysql57_nfs]# kubectl get pods -A -o wide NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES Kube-system helm-install-Traefik-Rb69p 0/1 Completed 0 2d2h 10.42.0.9k3s - Master <none> <none> READINESS GATES Kube-system helm-install-Traefik-Rb69p 0/1 Completed 0 2d2h 10.42.0.9k3s - Master < None > < None > Kube-system metrics-server-86cbb8457f-nkck4 1/1 Running 2 2d2h 10.42.0.36k3s -master <none> <none> kube-system Coredns-854c77959c-mvlqz 1/1 Running 2 2d2h 10.42.0.33k3s -master <none> <none> Flex-system Fleet-agent-55bfc495bd-g7mt8 1/1 Running 2 2d1h 10.42.0.39k3s -master <none> <none> default Seata-server-5ccbbdfb87-rnzp8 1/1 Running 2 47h 10.42.0.38k3s -master <none> <none> kube-system svclb-traefik-8rk4w 2/2 Running 6 2d2h 10.42.0.34k3s -master <none> <none> kube-system traefik-6f9cbd9bd4-ppCMC 1/1 Running 2 2d2h 10.42.0.32 K3s-master <none> <none> kube-system local-path-provisioner- 5FF76FC89D-s698r 1/1 Running 3 2d2h 10.42.0.40k3s-master <none> <none> db-mysql mysql-nfs-deploy-5f4fc57696-7cjKN 1/1 Running 0 20m 10.42.0.41k3s -master <none> <none> Cattle-system cattle-cluster-agent-68b9fbcc5d-ghp2x 0/1 CrashLoopBackOff 228 2d1h 10.42.0.35k3s -master <none> <none> cattle-system cattle-cluster-agent-68b9fbcc5d-ghp2x 0/1 CrashLoopBackOff 228 2d1h 10.42.0.35k3s -masterCopy the code

Mysql > query mysql

[root@cs100 mysql]# pwd
/root/data/nfs/mysql
[root@cs100 mysql]# ls
auto.cnf  ca-key.pem  ca.pem  client-cert.pem  client-key.pem  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  mysql  performance_schema  private_key.pem  public_key.pem  server-cert.pem  server-key.pem  sys

Copy the code