mysql 5.7部署到k8s并持久化到NFS
2018-10-31 本文已影响1412人
九遐
参考网上的yaml文件内容练习在K8S里的部署。
非持久化部署
mysql-deployment.yaml的内容
root@bob-k8s3:/home/bob# cat mysql-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
fabric8.io/iconUrl: https://raw.githubusercontent.com/docker-library/docs/c408469abbac35ad1e4a50a6618836420eb9502e/mysql/logo.png
labels:
app: mk-mysql
provider: monkeyk
version: "5.7"
group: com.monkeyk
name: mk-mysql
namespace: hyf
spec:
replicas: 1
selector:
matchLabels:
app: mk-mysql
provider: monkeyk
group: com.monkeyk
template:
metadata:
annotations:
fabric8.io/iconUrl: https://raw.githubusercontent.com/docker-library/docs/c408469abbac35ad1e4a50a6618836420eb9502e/mysql/logo.png
labels:
app: mk-mysql
provider: monkeyk
group: com.monkeyk
spec:
containers:
- env:
- name: MYSQL_ROOT_PASSWORD
value: "asdewq"
- name: MYSQL_USER
value: "monkeyk"
- name: MYSQL_PASSWORD
value: "asdewq"
image: "mysql:5.7"
imagePullPolicy: IfNotPresent
name: mk-mysql
ports:
- containerPort: 3306
protocol: TCP
name: http
mysql-svc.yaml的内容
root@bob-k8s3:/home/bob# cat mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
fabric8.io/iconUrl: >-
https://raw.githubusercontent.com/docker-library/docs/c408469abbac35ad1e4a50a6618836420eb9502e/mysql/logo.png
labels:
expose: "true"
app: mk-mysql
group: com.monkeyk
provider: monkeyk
version: '5.7'
name: mk-mysql
namespace: hyf
spec:
type: NodePort
ports:
- name: http
port: 80
nodePort: 30802
protocol: TCP
targetPort: 3306
selector:
app: mk-mysql
group: com.monkeyk
provider: monkeyk
部署:
root@bob-k8s3:/home/bob# kubectl create -f mysql-deployment.yaml
root@bob-k8s3:/home/bob# kubectl create -f mysql-svc.yaml
查看pods, 因为K8S里部署了反向代理,外部系统并不能通过POD的IP访问mysql.
root@bob-k8s3:/home/bob# kubectl get pods -n hyf -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mk-mysql-69fc68f9fc-mwhsv 1/1 Running 0 54m 172.20.157.210 10.200.0.30
查看服务,K8S集群内的其他程序可以通过mk-mysql.hyf:80来访问:
image.png
持久化部署
做数据持久化之前,先查看MYSQL数据的存放地址:
root@bob-k8s3:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
mk-mysql-558c495848-nbsqb 1/1 Running 0 23m
root@bob-k8s3:~# kubectl exec -it mk-mysql-558c495848-nbsqb bash
root@mk-mysql-558c495848-nbsqb:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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> select@@datadir;
+-----------------+
| @@datadir |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)
mysql>
更改上面的mysql-deployment.yaml,把路径/var/lib/mysql/ 添加到volumeMounts,重新部署,添加nfs支持,持久化到nfs服务器:
root@bob-k8s3:/home/bob# cat mysql-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
fabric8.io/iconUrl: https://raw.githubusercontent.com/docker-library/docs/c408469abbac35ad1e4a50a6618836420eb9502e/mysql/logo.png
labels:
app: mk-mysql
provider: monkeyk
version: "5.7"
group: com.monkeyk
name: mk-mysql
namespace: hyf
spec:
replicas: 1
selector:
matchLabels:
app: mk-mysql
provider: monkeyk
group: com.monkeyk
template:
metadata:
annotations:
fabric8.io/iconUrl: https://raw.githubusercontent.com/docker-library/docs/c408469abbac35ad1e4a50a6618836420eb9502e/mysql/logo.png
labels:
app: mk-mysql
provider: monkeyk
group: com.monkeyk
spec:
containers:
- env:
- name: MYSQL_ROOT_PASSWORD
value: "asdewq"
- name: MYSQL_USER
value: "monkeyk"
- name: MYSQL_PASSWORD
value: "asdewq"
image: "mysql:5.7"
imagePullPolicy: IfNotPresent
name: mk-mysql
ports:
- containerPort: 3306
protocol: TCP
name: http
volumeMounts:
- name: mysqldata
mountPath: "/var/lib/mysql"
readOnly: false
subPath: hyf-mysql
volumes:
- name: mysqldata
nfs:
server: 10.108.52.135
path: "/nfs"
查看NFS服务器上的持久化数据
root@bob-Virtual-Machine:/nfs# pwd
/nfs
root@bob-Virtual-Machine:/nfs# ls hyf-mysql/
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
root@bob-Virtual-Machine:/nfs#