k8s安装mysql
# 大概步骤
创建一个新的namespace
在该namespace下创建一个deployment
deployment自动部署好replicaSet和pod
创建对应服务
验证是否成功
# 1.创建一个新的namespace
[root@master kmysql]# kubectl create namespace summer
namespace/ summer created
1
2
2
# 2.在该namespace下创建一个deployment
(env中设置了mysql的root用户的密码)
[root@master kmysql]# cat mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
namespace: summer
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
args:
- "--ignore-db-dir=lost+found"
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "hadoop"
# kubectl create -f mysql-deployment.yaml --record
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- deployment自动部署好replicaSet和pod
[root@master kmysql]# kubectl get rs -n summer
NAME DESIRED CURRENT READY AGE
mysql-deployment-66bf8d5bf4 1 1 1 11s
[root@master kmysql]# kubectl get pod -n summer
NAME READY STATUS RESTARTS AGE
mysql-deployment-66bf8d5bf4-4rbq7 1/1 Running 0 33s
1
2
3
4
5
6
2
3
4
5
6
# 4.创建对应服务
(注意定义type=NodePort,并对应的nodeport端口号,以便集群外访问该服务)
[root@master kmysql]# cat mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
namespace: summer
labels:
name: mysql-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 30306
selector:
app: mysql
# kubectl create -f mysql-svc.yaml --record
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 5.验证是否成功
在远程客户端上下载mysql客户端Navicat,进行验证
主机:service对应的pod所在的node的ip
端口:上面service中的nodeport端口号
密码:deployment文件env中设置的root用户的密码)
这里有三个node节点都可以访问 验证: