一,前言
上一篇,介紹基于 k8s 項目部署流程設(shè)計;
本篇,介紹 MySQL 服務(wù)的部署;
二,部署 MySQL 服務(wù)
部署 MySQL
- 可以為指定 node 添加污點,專門用于 mysql 部署(當前只有一個節(jié)點,不考慮);
- 為了保證mysql容器重啟時數(shù)據(jù)不會丟失:創(chuàng)建 mysql 數(shù)據(jù)目錄,用于存儲 mysql 數(shù)據(jù),實現(xiàn) MySQL 數(shù)據(jù)的持久化;
- 創(chuàng)建 Secret 對象,向為 mysql 容器提供用戶名、密碼信息;
- 創(chuàng)建 mysql Deployment 配置文件,并創(chuàng)建 deploy 完成 pod 部署;
- 創(chuàng)建 mysql Service 配置文件,并創(chuàng)建 service 解決 ip 漂移問題,對外提供 pod 訪問;
- 為 k8s-master 安裝 mysql,使 k8s-master 能夠使用 mysql 命令,測試數(shù)據(jù)庫使用;
三,MySQL 數(shù)據(jù)的持久化
容器中的數(shù)據(jù)是需要保留的,否則容器重啟就沒了,需要創(chuàng)建數(shù)據(jù)目錄,存儲 MySQL 數(shù)據(jù);
在本地創(chuàng)建 MySQL 數(shù)據(jù)文件夾,然后掛載到 MySQL 容器,實現(xiàn) MySQL 數(shù)據(jù)的可以持久化
在 k8s-node 節(jié)點創(chuàng)建 MySQL 數(shù)據(jù)文件夾(此文件夾必須為空,否則導(dǎo)致 MySQL 啟動失??;
// 創(chuàng)建數(shù)據(jù)目錄文件夾(mysql 運行時的數(shù)據(jù)目錄,約定好的)
[root@k8s-master ~]# mkdir /var/lib/mysql
[root@k8s-master ~]# ll /var/lib/mysql
總用量 0
創(chuàng)建一個新的文件夾:cicd
將所有的配置文件都放在這個cicd目錄下
[root@k8s-master ~]# mkdir cicd
[root@k8s-master ~]# cd cicd/
[root@k8s-master cicd]#
四,創(chuàng)建 Secret 對象
通過 Secret 對象,將 MySQL 的用戶名、密碼傳遞到鏡像中使用;
創(chuàng)建 Secret 對象:mysql-auth
// 創(chuàng)建 secret,generic:基于普通文本格式,--from-literal:從字面量創(chuàng)建
[root@k8s-master cicd]# kubectl create secret generic mysql-auth --from-literal=username=root --from-literal=password=123456
secret/mysql-auth created
[root@k8s-master cicd]# kubectl get secret mysql-auth
NAME TYPE DATA AGE
mysql-auth Opaque 2 17s
[root@k8s-master cicd]# kubectl get secret mysql-auth -o yaml
apiVersion: v1
data:
password: MTIzNDU2
username: cm9vdA==
kind: Secret
metadata:
creationTimestamp: "2022-01-06T07:14:25Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:password: {}
f:username: {}
f:type: {}
manager: kubectl-create
operation: Update
time: "2022-01-06T07:14:25Z"
name: mysql-auth
namespace: default
resourceVersion: "1934676"
uid: c4a219d2-3754-4133-8484-9b4caa5a54cd
type: Opaque
[root@k8s-master cicd]# echo MTIzNDU2 | base64 -d
123456
[root@k8s-master cicd]# echo cm9vdA== | base64 -d
root
五,創(chuàng)建 Deployment
創(chuàng)建 mysql Deployment 配置文件,并創(chuàng)建 deploy 完成 pod 部署;
創(chuàng)建 deploy:deployment-cicd-mysql.yaml
[root@k8s-master cicd]# vi deployment-cicd-mysql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cicd-mysql
spec:
replicas: 1
selector:
matchLabels:
app: cicd-mysql
template:
metadata:
labels:
app: cicd-mysql
spec:
containers:
- name: cicd-mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent #鏡像拉取策略:如果不存在就拉取鏡像
args:
- "--ignore-db-dir=lost+found"
ports:
- containerPort: 3306
volumeMounts: #掛載數(shù)據(jù)卷
- name: mysql-data
mountPath: "/var/lib/mysql" #掛載到容器內(nèi)的目錄
env: #環(huán)境變量
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-auth
key: password #引用mysql-auth中的password值
volumes: #數(shù)據(jù)卷
- name: mysql-data
hostPath: #宿主機路徑
path: /var/lib/mysql #宿主機的目錄
type: Directory
備注:以下配置文檔中有,但視頻沒用
tolerations:
- key: "mysql"
operator: "Equal"
value: "true"
effect: "NoSchedule"
備注:
mysql 自帶 root 用戶,只設(shè)置 root 用戶密碼即可使用;
環(huán)境變量 env MYSQL_ROOT_PASSWORD 是在 mysql:5.7 的鏡像中約定的;
mysql容器啟動成功后,會讀取到 MYSQL_ROOT_PASSWORD 并設(shè)置為密碼;
如:docker run mysql:5.7 --env PASSWORD=123
生效配置
[root@k8s-master cicd]# kubectl apply -f deployment-cicd-mysql.yaml
deployment.apps/cicd-mysql created
[root@k8s-master cicd]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cicd-mysql-745975859b-kstnh 0/1 Pending 0 65s
pay-v1-6db6455b8-np2hw 1/1 Running 0 6h38m
user-v1-9f4d589cc-rdmnz 1/1 Running 0 24h
v4-57b4cf7fd9-zcl45 0/1 ImagePullBackOff 0 7d
v4-fb4cd75f5-bf2pf 0/1 ImagePullBackOff 0 2d17h
清理一下資源,刪除 pay-v1、user-v1、v4 的 deploy
[root@k8s-master cicd]# kubectl delete deploy pay-v1 user-v1 v4
deployment.apps "pay-v1" deleted
deployment.apps "user-v1" deleted
deployment.apps "v4" deleted
[root@k8s-master cicd]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cicd-mysql-745975859b-kstnh 0/1 Pending 0 4m8s
cicd-mysql-745975859b-kstnh 這個 pod 是 pending 狀態(tài)
[root@k8s-master cicd]# kubectl describe pods cicd-mysql-745975859b-kstnh
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 49s (x7 over 4m51s) default-scheduler 0/2 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 1 node(s) had taint {pay-v1: true}, that the pod didn't tolerate.
kubectl logs cicd-mysql-6cbd4f95-g64hh
這是因為,k8s-node 服務(wù)器之前配置了 pay-v1: true,需要刪除掉污點
// 查看污點
[root@k8s-master cicd]# kubectl describe node k8s-node
Taints: pay-v1=true:NoSchedule
// 刪除污點
[root@k8s-master cicd]# kubectl taint nodes k8s-node pay-v1-
node/k8s-node untainted
// 再次查看,沒有污點了
[root@k8s-master cicd]# kubectl describe node k8s-node
Taints: <none>
再看 pod 信息,已經(jīng)在創(chuàng)建中了
[root@k8s-master cicd]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cicd-mysql-745975859b-kstnh 0/1 ContainerCreating 0 20m
但是有些問題,再看一下容器描述
[root@k8s-master cicd]# kubectl describe pods cicd-mysql-745975859b-kstnh
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 6m58s (x20 over 24m) default-scheduler 0/2 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 1 node(s) had taint {pay-v1: true}, that the pod didn't tolerate.
Normal Scheduled 6m36s default-scheduler Successfully assigned default/cicd-mysql-745975859b-kstnh to k8s-node
Warning FailedMount 2m15s (x2 over 4m33s) kubelet Unable to attach or mount volumes: unmounted volumes=[mysql-data], unattached volumes=[mysql-data default-token-q4qxd]: timed out waiting for the condition
Warning FailedMount 24s (x11 over 6m35s) kubelet MountVolume.SetUp failed for volume "mysql-data" : hostPath type check failed: /var/lib/mysql is not a directory
原因是,pod 在 k8s-node 服務(wù)器上部署,但 k8s-node 服務(wù)器上并沒有/var/lib/mysql目錄
所以,需要在 k8s-node 服務(wù)器上創(chuàng)建/var/lib/mysql目錄
[root@k8s-node ~]# mkdir /var/lib/mysql
刪除 pod,再看新啟動的 pod,已經(jīng)正常 Running
(注意:1,不刪除 pod 過一會兒也會正常 Running;2,刪除 pod后,副本會重新創(chuàng)建)
[root@k8s-master deployment]# kubectl delete pod cicd-mysql-745975859b-kstnh
pod "cicd-mysql-745975859b-kstnh" deleted
[root@k8s-master deployment]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cicd-mysql-745975859b-gpwzh 1/1 Running 0 10s
[root@k8s-master deployment]# kubectl describe pods cicd-mysql-745975859b-gpwzh
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 29s default-scheduler Successfully assigned default/cicd-mysql-745975859b-gpwzh to k8s-node
Normal Pulled 25s kubelet Container image "mysql:5.7" already present on machine
Normal Created 25s kubelet Created container cicd-mysql
Normal Started 25s kubelet Started container cicd-mysql
查看日志:
[root@k8s-master deployment]# kubectl logs cicd-mysql-745975859b-gpwzh
2022-01-07T01:39:13.425378Z 0 [Note] mysqld: ready for connections.
Version: '5.7.36' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
mysql 已經(jīng)啟動成功,等待連接
六,k8s-master 安裝 mysql 命令
為 k8s-master 安裝 mysql,使 k8s-master 能夠使用 mysql 命令,測試數(shù)據(jù)庫使用;
由于 k8s-maste 沒有安裝 mysql 所以不能使用 mysql 命令
1,k8s-maste 安裝 mysql
2,可以進入容器中操作
[root@k8s-master deployment]# kubectl exec -it cicd-mysql-745975859b-gpwzh -- bash
root@cicd-mysql-745975859b-gpwzh:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_cicd |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)
mysql> select * from users;
Empty set (0.00 sec)
\
創(chuàng)建 cicd 數(shù)據(jù)庫,創(chuàng)建表
mysql> create database cicd;
Query OK, 1 row affected (0.04 sec)
mysql> use cicd;
Database changed
mysql> CREATE TABLE `users` (
-> `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
-> `name` varchar(255) NOT NULL COMMENT '',
-> `age` int(11) NOT NULL COMMENT '',
-> `sex` varchar(255) NOT NULL COMMENT '1 2',
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.03 sec)
退出 mysql,退出pod,回到 k8s-master
mysql> exit;
Bye
root@cicd-mysql-745975859b-gpwzh:/# exit
exit
給 k8s-master 安裝 mysql:
// 下載MySQL源安裝包
wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
// 安裝MySQL源
yum -y install mysql57-community-release-el7-11.noarch.rpm
// 安裝 mysql
yum install mysql-community-server -y
k8s-master 訪問 MySQL
[root@k8s-master ~]# cat /etc/hosts
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
172.17.178.106 k8s-node
172.17.178.105 k8s-master
[root@k8s-master cicd]# mysql -h172.17.178.105 -P3306 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.178.105' (111)
還不能訪問,因為沒有服務(wù)
七,創(chuàng)建 Service
由于 pod 的 ip 是會漂移的, 需要通過 Service 訪問 pod,需要創(chuàng)建服務(wù)
創(chuàng)建 service:service-cicd-mysql.yaml
[root@k8s-master cicd]# vi service-cicd-mysql.yaml
apiVersion: v1
kind: Service
metadata: #源數(shù)據(jù)
name: service-cicd-mysql
spec: #規(guī)格
selector: #選擇器
app: cicd-mysql #與deployment配置中的container-name相對應(yīng)
ports: #將pod的 3306端口代理到宿主機的3306端口
- protocol: TCP
port: 3306
targetPort: 3306 #虛擬端口->真正的端口要看Service啟動后實際分配的
type: NodePort #不寫就不能訪問到pod
生效配置,服務(wù)啟動
[root@k8s-master cicd]# kubectl apply -f service-cicd-mysql.yaml
service/service-cicd-mysql created
[root@k8s-master cicd]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16d
service-cicd-mysql NodePort 10.108.224.96 <none> 3306:30509/TCP 11s
service-pay-v1 NodePort 10.97.250.199 <none> 80:30114/TCP 2d19h
service-user-v1 NodePort 10.104.13.40 <none> 80:31071/TCP 14d
3306:30509 服務(wù)的內(nèi)部端口/外部端口(外部訪問 Service 中的 mysql 服務(wù)使用的端口號)
數(shù)據(jù)庫端口:訪問 Service 的 30509 端口
[root@k8s-master cicd]# mysql -h172.17.178.105 -P30509 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use cicd;
Database changed
mysql> show tables;
+----------------+
| Tables_in_cicd |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)
mysql> exit;
Bye
數(shù)據(jù)庫配置完成;
八,結(jié)尾
本篇,完成了 mysql 服務(wù)的創(chuàng)建和測試;文章來源:http://www.zghlxwxcb.cn/news/detail-773966.html
下一篇,介紹后端服務(wù)的部署;文章來源地址http://www.zghlxwxcb.cn/news/detail-773966.html
到了這里,關(guān)于【Kubernetes】第二十二篇 - k8s 部署 MySQL 服務(wù)(secret、deployment、service)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!