在这里我部署mysql的目的是为了后面将上一篇博客docker打包的el-admin镜像部署到k8s上,因此本文主要是部署mysql并实现持久化。html

1.将咱们的应用都部署到 el-admin 这个命名空间下面,建立eladmin-namespace.yaml 文件node

apiVersion: v1

kind: Namespace

metadata:

name: el-admin

2.建立存储文件路径mysql

[root@m ~]# mkdir -p /nfsdata/mysql

# 受权

[root@m ~]# chmod -R 777 /nfsdata/mysql

# m节点上修改文件

[root@m ~]# vi /etc/exports

/nfsdata *(rw,sync,no_root_squash)

# m节点上从新挂载

[root@m mysql]# exportfs -r

# m节点上启动

[root@m ~]# systemctl start rpcbind && systemctl enable rpcbind

[root@m ~]# systemctl start nfs && systemctl enable nfs

# 其余节点上启动

[root@w1 ~]# systemctl start nfs

# m节点上查看

[root@m ~]# showmount -e

Export list for m:

/nfsdata *

3.编写el-admin-mysql.yamlsql

apiVersion: v1

kind: ReplicationController

metadata:

name: el-admin-mysql-rc

namespace: el-admin

labels:

name: el-admin-mysql-rc

spec:

replicas: 1

selector:

name: el-admin-mysql-rc

template:

metadata:

labels:

name: el-admin-mysql-rc

spec:

containers:

- name: mysql

image: mysql

imagePullPolicy: IfNotPresent

ports:

- containerPort: 3306

env:

- name: MYSQL_ROOT_PASSWORD

value: "root"

volumeMounts:

- name: mysql-persistent-storage

mountPath: /var/lib/mysql #MySQL容器的数据都是存在这个目录的,要对这个目录作数据持久化

volumes:

- name: mysql-persistent-storage

persistentVolumeClaim:

claimName: el-admin-mysql-pvc #指定pvc的名称

---

apiVersion: v1

kind: Service

metadata:

name: el-admin-mysql-svc

namespace: el-admin

labels:

name: el-admin-mysql-svc

spec:

type: NodePort

ports:

- port: 3306

protocol: TCP

targetPort: 3306

name: http

nodePort: 3306

selector:

name: el-admin-mysql-rc

---

apiVersion: extensions/v1beta1

kind: Ingress

metadata:

name: el-admin-mysql-ingress

namespace: el-admin

spec:

rules:

- host: eladmin.charon.com

http:

paths:

- path: /

backend:

serviceName: el-admin-mysql-svc

servicePort: 3306

---

apiVersion: v1

kind: PersistentVolume

metadata:

name: el-admin-mysql-pv

namespace: el-admin

spec:

capacity:

storage: 2Gi

accessModes:

- ReadWriteMany

persistentVolumeReclaimPolicy: Recycle

storageClassName: nfs

nfs:

path: /nfsdata/mysql

server: 192.168.189.153

---

kind: PersistentVolumeClaim

apiVersion: v1

metadata:

name: el-admin-mysql-pvc

namespace: el-admin

spec:

accessModes:

- ReadWriteMany

storageClassName: nfs

resources:

requests:

storage: 2Gi

4.建立mysql的服务docker

[root@m el-admin]# kubectl create -f el-admin-mysql.yaml

5.查看pod,由于是在el-admin的命名空间下,因此查询pod的时候须要指定命名空间shell

[root@m ~]# kubectl get pods -n el-admin -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

el-admin-mysql-rc-9p7wf 1/1 Running 1 151m 192.168.190.124 w1

从上图能够看到,mysql的pod是在w1节点上,即192.168.189.155这个节点。这个时候咱们使用navicat链接mysql,确定是链接不成功的,以下图所示:数据库

3bdc31876357515cc09671adc3540eba.png

6.进入容器windows

[root@m el-admin]# kubectl exec -it el-admin-mysql-rc-9p7wf /bin/bash

# 登陆mysql

root@el-admin-mysql-rc-9p7wf:/# mysql -u root -p

# 输入密码,密码为上门pod中配置的root

查询mysql的用户,api

mysql> select host,user,plugin,authentication_string from mysql.user;

a7cd472ce0240a72a20b400461280ae4.png

为用端口为'%'用户为root的用户密码设置为root。bash

mysql> alter user 'root'@'%' identified with mysql_native_password by'root';

设置完成后,从新链接,便可链接成功。

6c8fa51427caaba162dad1a1acecac70.png

上门的文档里配置了ingress,ingress能够配置提供外部可访问的URL。

# 修改windows的host文件,目录:C:\Windows\System32\drivers\etc

# 添加内容

192.168.189.155 eladmin.charon.com

使用eladmin.charon.com这个域名也能够链接成功

60173df63f2426dd13c38cdb361c0c58.png

链接成功后,在navicat里面建立一个eladmin的数据库,在k8s上的mysql挂载目录下查看新建的eladmin的数据库目录。

7869e56f819c04a48aac999c5c791090.png

到这里,k8s部署mysql,并将数据持久化到宿主机上就完成了。

参考文件:

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐