1. 程式人生 > >k8s部署mysql資料持久化

k8s部署mysql資料持久化

在這裡我部署mysql的目的是為了後面將上一篇部落格docker打包的el-admin映象部署到k8s上,所以本文主要是部署mysql並實現持久化。 1.將我們的應用都部署到 el-admin 這個名稱空間下面,建立`eladmin-namespace.yaml` 檔案 ```yaml apiVersion: v1 kind: Namespace metadata: name: el-admin ``` 2.建立儲存檔案路徑 ```shell [root@m ~]# mkdir -p /nfsdata/mysql # 授權 [root@m ~]# chmod -R 777 /nfsdata/mysql # m節點上修改檔案 [root@m ~]# vi /etc/exports /nfsdata *(rw,sync,no_root_squash) # m節點上重新掛載 [root@m mysql]# exportfs -r # m節點上啟動 [root@m ~]# systemctl start rpcbind && systemctl enable rpcbind [root@m ~]# systemctl start nfs && systemctl enable nfs # 其他節點上啟動 [root@w1 ~]# systemctl start nfs # m節點上檢視 [root@m ~]# showmount -e Export list for m: /nfsdata * ``` 3.編寫el-admin-mysql.yaml ```yaml apiVersion: v1 kind: ReplicationController metadata: name: el-admin-mysql-rc namespace: el-admin labels: name: el-admin-mysql-rc spec: replicas: 1 selector: name: el-admin-mysql-rc template: metadata: labels: name: el-admin-mysql-rc spec: containers: - name: mysql image: mysql imagePullPolicy: IfNotPresent ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "root" volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql #MySQL容器的資料都是存在這個目錄的,要對這個目錄做資料持久化 volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: el-admin-mysql-pvc #指定pvc的名稱 --- apiVersion: v1 kind: Service metadata: name: el-admin-mysql-svc namespace: el-admin labels: name: el-admin-mysql-svc spec: type: NodePort ports: - port: 3306 protocol: TCP targetPort: 3306 name: http nodePort: 3306 selector: name: el-admin-mysql-rc --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: el-admin-mysql-ingress namespace: el-admin spec: rules: - host: eladmin.charon.com http: paths: - path: / backend: serviceName: el-admin-mysql-svc servicePort: 3306 --- apiVersion: v1 kind: PersistentVolume metadata: name: el-admin-mysql-pv namespace: el-admin spec: capacity: storage: 2Gi accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Recycle storageClassName: nfs nfs: path: /nfsdata/mysql server: 192.168.189.153 --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: el-admin-mysql-pvc namespace: el-admin spec: accessModes: - ReadWriteMany storageClassName: nfs resources: requests: storage: 2Gi ``` 4.建立mysql的服務 ```yaml [root@m el-admin]# kubectl create -f el-admin-mysql.yaml ``` 5.檢視pod,因為是在el-admin的名稱空間下,所以查詢pod的時候需要指定名稱空間 ```shell [root@m ~]# kubectl get pods -n el-admin -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES el-admin-mysql-rc-9p7wf 1/1 Running 1 151m 192.168.190.124 w1 ``` 從上圖可以看到,mysql的pod是在w1節點上,即192.168.189.155這個節點。這個時候我們使用navicat連線mysql,肯定是連線不成功的,如下圖所示: ![](https://img2020.cnblogs.com/blog/1459011/202102/1459011-20210218144601745-674730311.png) 6.進入容器 ```shell [root@m el-admin]# kubectl exec -it el-admin-mysql-rc-9p7wf /bin/bash # 登入mysql root@el-admin-mysql-rc-9p7wf:/# mysql -u root -p # 輸入密碼,密碼為上門pod中配置的root ``` 查詢mysql的使用者, ```sql mysql> select host,user,plugin,authentication_string from mysql.user; ``` ![](https://img2020.cnblogs.com/blog/1459011/202102/1459011-20210218144625637-910992293.png) 為用埠為'%'使用者為root的使用者密碼設定為root。 ```sql mysql> alter user 'root'@'%' identified with mysql_native_password by'root'; ``` 設定完成後,重新連線,即可連線成功。 ![](https://img2020.cnblogs.com/blog/1459011/202102/1459011-20210218144642375-975168137.png) 上門的文件裡配置了ingress,ingress可以配置提供外部可訪問的URL。 ```shell # 修改windows的host檔案,目錄:C:\Windows\System32\drivers\etc # 新增內容 192.168.189.155 eladmin.charon.com ``` 使用`eladmin.charon.com`這個域名也可以連線成功 ![](https://img2020.cnblogs.com/blog/1459011/202102/1459011-20210218144703106-339485844.png) 連線成功後,在navicat裡面建立一個eladmin的資料庫,在k8s上的mysql掛載目錄下檢視新建的eladmin的資料庫目錄。 ![](https://img2020.cnblogs.com/blog/1459011/202102/1459011-20210218144719009-1560552968.png) 到這裡,k8s部署mysql,並將資料持久化到宿主機上就完成了。 參考檔案: kubernetes部署mysql:https://www.cnblogs.com/zoulixiang/p/9910