参考配置1
[root@k8s-master1 ~]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mysql #为该Deployment设置key为app,value为mysql的标签
name: mysql
namespace: test
spec:
replicas: 1 #副本数量
selector: #标签选择器,与上面的标签共同作用
matchLabels: #选择包含标签app:nginx的资源
app: mysql
template: #这是选择或创建的Pod的模板
metadata: #Pod的元数据
labels: #Pod的标签,上面的selector即选择包含标签app:nginx的Pod
app: mysql
spec: #期望Pod实现的功能(即在pod中部署)
containers: #生成container,与docker中的container是同一种
- name: mysql
image: mysql:5.7 #使用镜像mysql: 创建container,该container默认3306端口可访问
ports:
- containerPort: 3306 # 开启本容器的3306端口可访问
env:
- name: MYSQL_ROOT_PASSWORD
value: hjj123456
volumeMounts: #挂载持久存储卷
- name: mysql-data #挂载设备的名字,与volumes[*].name 需要对应
mountPath: /var/lib/mysql #挂载到容器的某个路径下
- name: mysql-dev-conf
mountPath: /etc/mysql
volumes:
- name: mysql-data #和上面保持一致 这是本地的文件路径,上面是容器内部的路径
nfs:
server: 10.0.19.129 #nfs的ip地址
path: /opt/data/mysql/data #此路径需要实现创建
- name: mysql-dev-conf
nfs:
server: 10.0.19.129
name: /opt/data/mysql/cnf #此路径需要实现创建
参考配置2
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
namespace: plugins
labels:
app: mysql
data:
my.cnf: |-
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true
max_connections=2000
secure_file_priv=/var/lib/mysql
bind-address=0.0.0.0
symbolic-links=0
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
---
apiVersion: v1
kind: Service
metadata:
labels:
app: mysql
name: mysql-svc
namespace: plugins
spec:
type: NodePort
ports:
- name: http
port: 3306
nodePort: 30021
protocol: TCP
targetPort: 3306
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: plugins
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- args:
- --datadir
- /var/lib/mysql/datadir
env:
- name: MYSQL_ROOT_PASSWORD
value: root
- name: MYSQL_USER
value: user
- name: MYSQL_PASSWORD
value: user
image: mysql:5.7
name: mysql-container
ports:
- containerPort: 3306
name: dbapi
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-storage
- name: config
mountPath: /etc/mysql/conf.d/my.cnf
subPath: my.cnf
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: nfs-plugins
- name: config
configMap:
name: mysql-config
- name: localtime
hostPath:
type: File
path: /etc/localtime
自己配置 , nodeName: gem-yxyw-t-c02,分配主机的时候需要查看主机名
实际配置1
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mysqldb01-config
labels:
app: gem-sale-t-db01
data:
my.cnf: |-
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true
max_connections=2000
secure_file_priv=/var/lib/mysql
bind-address=0.0.0.0
symbolic-links=0
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
---
apiVersion: v1
kind: Service
metadata:
labels:
app: gem-sale-t-db01
name: gem-sale-t-db01
spec:
type: NodePort
ports:
- name: http
port: 3306
nodePort: 30001
protocol: TCP
targetPort: 3306
selector:
app: gem-sale-t-db01
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: gem-sale-t-db01
name: gem-sale-t-db01
spec:
replicas: 1
selector:
matchLabels:
app: gem-sale-t-db01
template:
metadata:
labels:
app: gem-sale-t-db01
spec:
containers:
- name: gem-sale-t-db01
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: 3!y5cD&%2OWz
livenessProbe:
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
exec:
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
readinessProbe:
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
exec:
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
volumes:
- name: config
configMap:
name: mysqldb01-config
nodeName: gem-yxyw-t-c02
实际配置2
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mysqldb02-config
labels:
app: gem-sale-t-db02
data:
my.cnf: |-
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true
max_connections=2000
secure_file_priv=/var/lib/mysql
bind-address=0.0.0.0
symbolic-links=0
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
---
apiVersion: v1
kind: Service
metadata:
labels:
app: gem-sale-t-db02
name: gem-sale-t-db02
spec:
type: NodePort
ports:
- name: http
port: 3306
nodePort: 30002
protocol: TCP
targetPort: 3306
selector:
app: gem-sale-t-db02
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: gem-sale-t-db02
name: gem-sale-t-db02
spec:
replicas: 1
selector:
matchLabels:
app: gem-sale-t-db02
template:
metadata:
labels:
app: gem-sale-t-db02
spec:
containers:
- name: gem-sale-t-db02
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: 3!y5cD&%2OWz
livenessProbe:
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
exec:
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
readinessProbe:
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
exec:
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
volumes:
- name: config
configMap:
name: mysqldb02-config
nodeName: gem-yxyw-t-c02
mysql8.0部署
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
labels:
app: mysql
data:
my.cnf: |-
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
max_connections = 2000
secure_file_priv=/var/lib/mysql
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
## PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql
labels:
app: mysql #设置 pv 的 label 标签
spec:
capacity:
storage: 20Gi #设置 pv 存储资源大小
accessModes:
- ReadWriteOnce
mountOptions:
- hard
- nfsvers=4.1
nfs: #指定使用 NFS 存储驱动
server: 10.36.21.220 #指定 NFS 服务器 IP 地址
path: /data/mysql/GEM-SALE-T-DB04 #指定 NFS 共享目录的位置,且需提前在该目录中创建 mysql 目录
persistentVolumeReclaimPolicy: Retain
---
## PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql
spec:
resources:
requests:
storage: 20Gi #设置 pvc 存储资源大小
accessModes:
- ReadWriteOnce
selector:
matchLabels:
app: mysql #根据 Label 选择对应 PV
## Service
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
type: NodePort
ports:
- name: mysql
port: 3306
targetPort: 3306
nodePort: 30336
selector:
app: mysql
---
## Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0.19
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD ## 配置Root用户默认密码
value: "123456"
resources:
limits:
cpu: 2000m
memory: 512Mi
requests:
cpu: 2000m
memory: 512Mi
livenessProbe:
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
exec:
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
readinessProbe:
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
exec:
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
volumeMounts:
- name: data
mountPath: /var/lib/mysql
- name: config
mountPath: /etc/mysql/conf.d/my.cnf
subPath: my.cnf
- name: localtime
readOnly: true
mountPath: /etc/localtime
volumes:
- name: data
persistentVolumeClaim:
claimName: mysql
- name: config
configMap:
name: mysql-config
- name: localtime
hostPath:
type: File
path: /etc/localtime
nodeName: gem-yxyw-t-c02
mkdir -p /data/mysql/GEM-SALE-T-DB01/mysql-data
mkdir -p /data/mysql/GEM-SALE-T-DB02/mysql-data
参数简介:
show variables like '%max_connections%'; #查数据库最大连接数确定配置是否生效
-
ports: 配置镜像映射端口。
-
env: 镜像环境变量配置,其中 MYSQL_ROOT_PASSWORD 是 Mysql 镜像用于配置 root 用户默认密码变量。
-
resources: 配置 CPU、Memory 资源限制,可以通过配置该值来配置 Pod 的 QoS 级别。
-
livenessProbe: 配置存活探针,定时检测 Mysql 应用运行状态,如果检测到 Mysql 挂掉将进行重启操作。
-
readinessProbe: 配置就绪探针,定时检测 Mysql 应用启动状态,如果启动成功将允许流量涌入,启动失败将进行重启操作。
-
command: 探针执行探测时执行的探测命令。
-
volumeMounts: 存储卷挂载配置,用于镜像内存储的挂载配置,与 volumes 中对于的 name 进行绑定。
-
volumes: 存储卷配置,可配置使用 pvc、hostPath、emptyDir、nfs 等存储,需要配置 name 值与 VolumeMounts 进行绑定。
-
在镜像内部命令行中输入 mysql 登录命令,测试是否能够正常登录:
$ mysql -h 10.36.21.220 -P 30336 --user=root --password=123456 mysql -h 10.36.21.220 -P 30336 -uroot -p'123456'
导出导入数据库
导出数据库所有库
mysqldump -h 127.0.0.1 -uroot -p"Gd*(53#SALE" -R -E --all-databases >back.sql
导入数据库所有库
mysql -h 10.36.21.220 -P 30004 -uroot -p'3!y5cD&%2OWz' <back.sql
k8s的mysql的pod误删之后数据恢复,
gem-sale-t-db04被删除之后,挂载的数据卷还在/data/mysql/GEM-SALE-T-DB04/mysql-data,我们从新创建一个pod把数据拷贝到新的挂载卷,数据库就自动恢复了
cd /data/mysql/GEM-SALE-T-DB06/mysql-data
\cp -rf /data/mysql/GEM-SALE-T-DB04/mysql-data/* ./
数据恢复
docker容器数据恢复
恢复步骤
1. 查找数据卷位置
数据卷目录在 /var/lib/docker/volumes 下,每个容器都会在该目录下有一个文件夹,如果容器还存在的话,我们可以使用 docker inspect 容器ID 去查看 数据卷位置,这下容器被删除了,可怎么办,只能挨个去找了,一般 MySQL 容器数据卷目录下会有一个 _data 目录,该目录下会显示你每个数据库的文件夹,最终找到了
2. 创建新数据卷
使用 docker volume create 数据卷名字 命令新建一个数据卷,docker volume ls 查看数据卷列表
docker volume create mysqldata
docker volume ls
注意:使用数据卷进行挂载的时候,数据卷必须是一个空的目录,也就是说不能有任何数据。
创建容器
执行创建指令:
docker run -d -p 3306:3306 --name mysql -v mysqldata:/var/lib/mysql -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql:5.7
恢复数据之前需要把刚刚建立的数据卷里面关联的内容删除掉,然后把之前的数据卷内容复制到现在的数据卷进行数据恢复。
cd /var/lib/docker/volumes/mysqldata/_data/
rm -f *
rm -f -R *
复制内容到数据卷
cd /var/lib/docker/volumes/容器id/_data/
cp -R * /var/lib/docker/volumes/mysqldata/_data/
至此数据库数据恢复完成,需要重启下mysql容器,进入恢复的容器查看
docker stop mysql
docker start mysql