在這裏我部署mysql的目的是爲了後面將上一篇博客docker打包的el-admin鏡像部署到k8s上,因此本文主要是部署mysql並實現持久化。html
1.將咱們的應用都部署到 el-admin 這個命名空間下面,建立eladmin-namespace.yaml
文件node
apiVersion: v1 kind: Namespace metadata: name: el-admin
2.建立存儲文件路徑mysql
[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.yamlsql
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的服務docker
[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 <none> <none>
從上圖能夠看到,mysql的pod是在w1節點上,即192.168.189.155這個節點。這個時候咱們使用navicat鏈接mysql,確定是鏈接不成功的,以下圖所示:數據庫
6.進入容器windows
[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的用戶,api
mysql> select host,user,plugin,authentication_string from mysql.user;
爲用端口爲'%'用戶爲root的用戶密碼設置爲root。bash
mysql> alter user 'root'@'%' identified with mysql_native_password by'root';
設置完成後,從新鏈接,便可鏈接成功。
上門的文檔裏配置了ingress,ingress能夠配置提供外部可訪問的URL。
# 修改windows的host文件,目錄:C:\Windows\System32\drivers\etc # 添加內容 192.168.189.155 eladmin.charon.com
使用eladmin.charon.com
這個域名也能夠鏈接成功
鏈接成功後,在navicat裏面建立一個eladmin的數據庫,在k8s上的mysql掛載目錄下查看新建的eladmin的數據庫目錄。
到這裏,k8s部署mysql,並將數據持久化到宿主機上就完成了。
參考文件:
kubernetes部署mysql:http://www.javashuo.com/article/p-emkhwjql-vd.html