k8s部署mysql服务(RC部署)

秒速五厘米 2023-02-13 14:48 159阅读 0赞

编写mysql-rc.yaml文件

  1. apiVersion: v1
  2. kind: ReplicationController
  3. metadata:
  4. name: mysql-rc
  5. labels:
  6. name: mysql-rc
  7. spec:
  8. replicas: 1
  9. selector:
  10. name: mysql-pod
  11. template:
  12. metadata:
  13. labels:
  14. name: mysql-pod
  15. spec:
  16. containers:
  17. - name: mysql
  18. image: mysql:5.7
  19. imagePullPolicy: IfNotPresent
  20. ports:
  21. - containerPort: 3306
  22. volumeMounts:
  23. - mountPath: /var/lib/mysql
  24. name: mydata
  25. env:
  26. - name: MYSQL_ROOT_PASSWORD
  27. value: "root"
  28. volumes:
  29. - name: mydata
  30. hostPath:
  31. path: /root/mysql/data

创建mysql-rc pod

  1. [root@master mysql]# kubectl apply -f mysql-rc.yaml -n gridcloud
  2. replicationcontroller/mysql-rc created

编写mysql-service.yaml文件

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: mysql-svc
  5. labels:
  6. name: mysql-svc
  7. spec:
  8. type: NodePort
  9. ports:
  10. - port: 3306
  11. targetPort: 3306
  12. nodePort: 30306
  13. selector:
  14. name: mysql-pod

启动mysql-service pod

  1. [root@master mysql]# kubectl apply -f mysql-service.yaml -n gridcloud
  2. service/mysql-svc created

查看我们的pod情况

  1. [root@master mysql]# kubectl get pods -n gridcloud
  2. NAME READY STATUS RESTARTS AGE
  3. mysql-rc-d2whh 0/1 ContainerCreating 0 59s

显示正在创建并查看该pod信息

  1. [root@master mysql]# kubectl describe pod mysql-rc-d2whh -n gridcloud
  2. Name: mysql-rc-d2whh
  3. Namespace: gridcloud
  4. Priority: 0
  5. Node: node2/192.168.0.153
  6. Start Time: Sat, 30 May 2020 00:28:52 +0800
  7. Labels: name=mysql-pod
  8. Annotations: <none>
  9. Status: Running
  10. IP: 10.244.2.49
  11. IPs:
  12. IP: 10.244.2.49
  13. Controlled By: ReplicationController/mysql-rc
  14. Containers:
  15. mysql:
  16. Container ID: docker://64769dcdc408ecd690707aa4ce56bc6ddfed7b41e46cf3c7f8d99461788944d1
  17. Image: mysql:5.7
  18. Image ID: docker-pullable://mysql@sha256:d16d9ef7a4ecb29efcd1ba46d5a82bda3c28bd18c0f1e3b86ba54816211e1ac4
  19. Port: 3306/TCP
  20. Host Port: 0/TCP
  21. State: Running
  22. Started: Sat, 30 May 2020 00:30:35 +0800
  23. Ready: True
  24. Restart Count: 0
  25. Environment:
  26. MYSQL_ROOT_PASSWORD: root
  27. Mounts:
  28. /var/lib/mysql from mydata (rw)
  29. /var/run/secrets/kubernetes.io/serviceaccount from default-token-sq8nw (ro)
  30. Conditions:
  31. Type Status
  32. Initialized True
  33. Ready True
  34. ContainersReady True
  35. PodScheduled True
  36. Volumes:
  37. mydata:
  38. Type: HostPath (bare host directory volume)
  39. Path: /root/mysql/data
  40. HostPathType:
  41. default-token-sq8nw:
  42. Type: Secret (a volume populated by a Secret)
  43. SecretName: default-token-sq8nw
  44. Optional: false
  45. QoS Class: BestEffort
  46. Node-Selectors: <none>
  47. Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
  48. node.kubernetes.io/unreachable:NoExecute for 300s
  49. Events:
  50. Type Reason Age From Message
  51. ---- ------ ---- ---- -------
  52. Normal Scheduled 2m38s default-scheduler Successfully assigned gridcloud/mysql-rc-d2whh to node2
  53. Normal Pulling 2m38s kubelet, node2 Pulling image "mysql:5.7"
  54. Normal Pulled 57s kubelet, node2 Successfully pulled image "mysql:5.7"
  55. Normal Created 56s kubelet, node2 Created container mysql
  56. Normal Started 56s kubelet, node2 Started container mysql

最后显示创建成功,最后在查询状态

  1. [root@master mysql]# kubectl get pods -n gridcloud
  2. NAME READY STATUS RESTARTS AGE
  3. mysql-rc-d2whh 1/1 Running 0 2m48s

显示running状态,表示创建成功,并查看详细信息显示分配在node2上,如下

  1. [root@master mysql]# kubectl get pods -n gridcloud -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. mysql-rc-d2whh 1/1 Running 0 5m28s 10.244.2.49 node2 <none> <none>

登录数据库,并创建一个库,做测试

  1. [root@manage-host bin]# mysql -uroot -h 192.168.0.155 -P 30306 -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 3
  5. Server version: 5.7.30 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> show databases;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | information_schema |
  16. | mysql |
  17. | performance_schema |
  18. | sys |
  19. +--------------------+
  20. 4 rows in set (0.00 sec)
  21. mysql> create database test;
  22. Query OK, 1 row affected (0.00 sec)
  23. mysql> show databases;
  24. +--------------------+
  25. | Database |
  26. +--------------------+
  27. | information_schema |
  28. | mysql |
  29. | performance_schema |
  30. | sys |
  31. | test |
  32. +--------------------+
  33. 5 rows in set (0.00 sec)
  34. mysql> quit
  35. Bye

最后我们看我们挂载的路径下有我们的数据库数据信息,此时在node2上查看(前后多了一个test)

  1. [root@node2 ~]# ls /root/mysql/data/
  2. auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
  3. ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
  4. [root@node2 ~]# cd /root/mysql/data/
  5. [root@node2 data]# ls
  6. auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
  7. ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem test

接着我们就重启下pod看看我们的数据库信息是否可查

  1. [root@master mysql]# kubectl get pods -n gridcloud
  2. NAME READY STATUS RESTARTS AGE
  3. mysql-rc-d2whh 1/1 Running 0 72m
  4. [root@master mysql]# kubectl delete pod mysql-rc-d2whh -n gridcloud
  5. pod "mysql-rc-d2whh" deleted
  6. [root@master mysql]# kubectl apply -f mysql-service.yaml -n gridcloud
  7. service/mysql-svc unchanged

然后我们重新登录数据库查看刚刚创建的库是否存在

  1. [root@manage-host bin]# mysql -uroot -h 192.168.0.155 -P 30306 -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 2
  5. Server version: 5.7.30 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> show databases;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | information_schema |
  16. | mysql |
  17. | performance_schema |
  18. | sys |
  19. | test |
  20. +--------------------+
  21. 5 rows in set (0.00 sec)

显示我们的数据没丢失,至此数据库部署成功

发表评论

表情:
评论列表 (有 0 条评论,159人围观)

还没有评论,来说两句吧...

相关阅读