[root@k8s-master01 yaml]# cat mysql-service.yaml apiVersion: v1 kind: Service metadata: name: mysql namespace: mysql spec: type: NodePort ports: - port: 3306 protocol: TCP targetPort: 3306 name: http nodePort: 30060 selector: app: mysql
要想讓客戶端遠程連接mysql,service設置為nodeport模式
[root@k8s-master01 ~]# mkdir -p /mysql/{data,yaml} [root@k8s-master01 ~]# cd /mysql/yaml/ [root@k8s-master01 yaml]# vim mysql-service.yaml [root@k8s-master01 yaml]# kubectl apply -f /mysql/yaml/mysql-service.yaml service/mysql created [root@k8s-master01 yaml]# vim mysql-deployment.yaml [root@k8s-master01 yaml]# kubectl apply -f mysql-deployment.yaml deployment.apps/mysql created
[root@k8s-master01 yaml]# vim persistence-volume.yaml [root@k8s-master01 yaml]# kubectl apply -f /mysql/yaml/persistence-volume.yaml persistentvolume/mysql-pv-volume created
[root@k8s-master01 yaml]# vim pvClaim.yaml [root@k8s-master01 yaml]# kubectl apply -f /mysql/yaml/pvClaim.yaml persistentvolumeclaim/mysql-pv-claim created
測試連接數據庫:
kubectl run -it --image=mysql:5.7 --restart=Never mysql-client -- mysql -h mysql -ppassword
后面連接數據庫:
kubectl exec -it pod的id號碼 /bin/bash
進入容器:
mysql -uroot -ppassword 進入數據庫
遠程連接mysql 1、然后在mysql中先更改加密方式,指令如下: mysql>ALTER USER 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; 2、然后再更改密碼,由於加密規則更改,所以需要重新設置密碼; mysql>ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; 3、給與root用戶全部權限; mysql> grant all privileges on *.* to root; 4、最后在刷新一下數據庫; mysql>FLUSH PRIVILEGES;
[root@k8s-master01 yaml]# cat mysql-service.yaml apiVersion: v1 kind: Service metadata: name: mysql spec: ports: - port: 3306 selector: app: mysql clusterIP: None [root@k8s-master01 yaml]# cat mysql-deployment.yaml apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: mysql spec: selector: matchLabels: app: mysql strategy: type: Recreate template: metadata: labels: app: mysql spec: containers: - image: mysql:5.7 name: mysql env: - name: MYSQL_ROOT_PASSWORD value: password ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pv-claim [root@k8s-master01 yaml]# cat persistence-volume.yaml apiVersion: v1 kind: PersistentVolume metadata: name: mysql-pv-volume labels: type: local spec: storageClassName: manual capacity: storage: 30Gi accessModes: - ReadWriteOnce hostPath: path: "/mysql/data" [root@k8s-master01 yaml]# cat pvClaim.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-pv-claim spec: storageClassName: manual accessModes: - ReadWriteOnce resources: requests: storage: 30Gi
數據庫文件導入:
[root@k8s-master01 ~]# kubectl cp /mysql/data/t100w.sql mysql-6cbd798f65-7s54p:/root/ /bin/bash
外部mysql數據文件通過kubectl cp 命令導入到pod容器里面root目錄下,然后在容器里面通過命令登陸數據庫,source 命令導入數據文件
mysql> source /root/t100w.sql; Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec)