部署 MySQL 到 k8s 集群
命名空间准备
创建项目专用的 namespace
1
kubectl create ns tms-test
MySQL 相关资源清单文件
创建 mysql 服务使用的 configmap,
tms-mysql-configmap.yaml
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
namespace: tms-test
data:
mysqld.cnf: |
[mysql]
prompt = "MySQL [\d]> "
no-auto-rehash
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
slow_query_log_file = /var/log/mysql/mysql-slow.log
bind-address = 0.0.0.0
server-id = 1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
skip_external_locking
lower_case_table_names=1
skip_host_cache
skip_name_resolve
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
back_log = 300
max_connections = 2660
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 4096
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 256M
thread_cache_size = 64
query_cache_type = 0
query_cache_size = 0
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
slow_query_log = 1
long_query_time = 1
performance_schema = 1
explicit_defaults_for_timestamp
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 1024M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 85M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_buffer_pool_instances = 2
innodb_page_cleaners = 2
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 8M
read_buffer = 4M创建 MySQL 初始密码 secret,tms-mysql-secret.yaml
1
2
3
4
5
6
7
8
9
10apiVersion: v1
kind: Secret
metadata:
name: tms-mysql-secrets
namespace: tms-test
type: Opaque
data:
mysql-root-pwd: cW5sPjcxa2ZDUW1h
mysql-app-user-pwd: TjVtRHMkN2Q=
mysql-test-user-pwd: TjVtRHMkN2Q=创建 MySQL 数据持久化 pvc 配置文件,tms-mysql-pvc.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tms-mysql-test-pvc
labels:
app: tms
namespace: tms-test
spec:
storageClassName: rook-ceph-block
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Gi创建 MySQL Deployment 配置文件,tms-mysql-deployment.yaml
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109apiVersion: apps/v1
kind: Deployment
metadata:
name: tms-mysql
labels:
app: tms
tier: mysql
namespace: tms-test
spec:
selector:
matchLabels:
app: tms
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: tms
tier: mysql
spec:
containers:
- name: tms-mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command:
- "sh"
- "-c"
- "rm -rf /etc/mysql/mysql.conf.d/mysqld.cnf && ln -s /mnt/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf"
livenessProbe:
exec:
command:
- "sh"
- "-c"
- MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"
- mysql -h 127.0.0.1 -u root -e "SELECT 1"
initialDelaySeconds: 30
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
exec:
command:
- "sh"
- "-c"
- MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"
- mysql -h 127.0.0.1 -u root -e "SELECT 1"
initialDelaySeconds: 30
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
args:
- --default_authentication_plugin=mysql_native_password
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: tms-mysql-secrets
key: mysql-root-pwd
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: tms-mysql-secrets
key: mysql-app-user-pwd
- name: MYSQL_DATABASE
value: tms_dev
- name: MYSQL_USER
value: tms
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: tms-mysql-persistent-storage
mountPath: /var/lib/mysql
subPath: "mysql"
- name: mysql-config
mountPath: /mnt/
- name: mysql-initdb
mountPath: /docker-entrypoint-initdb.d
volumes:
- name: tms-mysql-persistent-storage
persistentVolumeClaim:
claimName: tms-mysql-test-pvc
- name: mysql-config
configMap:
name: mysql-config
- name: mysql-initdb
nfs:
server: 193.168.55.17
path: /data/nfs/tms/init-sql
apiVersion: v1
kind: Service
metadata:
name: tms-mysql
labels:
app: tms
namespace: tms-test
spec:
ports:
- port: 3306
selector:
app: tms
tier: mysql创建 Cronjob 备份定时任务 job
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
30
31
32
33
34
35
36
37
38
39apiVersion: batch/v1
kind: CronJob
metadata:
name: mysqldump
namespace: tms-test
spec:
schedule: "00 12 * * *"
jobTemplate:
spec:
completions: 1
template:
spec:
restartPolicy: Never
containers:
- name: mysqldump-container
image: nacos/nacos-mysql-master:latest
volumeMounts:
- name: mysql-master-script
mountPath: /var/db/script
- name: local-time
mountPath: /etc/localtime
- name: mysql-master-backup
mountPath: /var/db/backup
command:
- "sh"
- "-c"
- "/var/db/script/mysqldump.sh"
nodeSelector:
kubernetes.io/hostname: k8s-master-01
volumes:
- name: mysql-master-script
hostPath:
path: /root/projects/tms/mysql/shell/prod
- name: mysql-master-backup
hostPath:
path: /root/projects/tms/mysql/backup/prod
- name: local-time
hostPath:
path: /etc/localtimeMySQL 备份脚本
/root/projects/tms/mysql/shell/prod/mysqldump.sh
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Author: 周斌
# Date: 2021-05-11
# Descript: MySQL 数据库全量备份脚本, 每周日晚上12点 30 开始备份.
# 检查当前用户
if [ $UID -ne 0 ];then
echo "Please run the script as root user!!"
exit
fi
# 当天日期
date=$(date +"%Y-%m-%d")
# 数据备份存放目录
backup_dir=/var/db/backup
# 备份记录日志文件
log_file=${backup_dir}/backup.log
# 备份数据库文件
dumpfile=${backup_dir}/all_database.sql
# 压缩文件名
gzdumpfile=${dumpfile}_${date}.tar.gz
# tar 命令绝对路径
tar=`which tar`
# mysqldump 命令绝对路径
mysqldump=`which mysqldump`
# 检查备份目录是否存在
if [ ! -d $backup_dir ];then
mkdir -p $backup_dir && cd $backup_dir
else
cd $backup_dir
fi
# 开始备份所有数据库
echo "*************************************** Begin Starting ******************************************" >> $log_file
echo "" >> $log_file
echo "$(date +'%Y-%m-%d %H:%M:%S') Starting Backup All Database to file $dumpfile ." >> $log_file
mysqldump -uroot -p'xxxxxxx' -h tms-mysql-prod --quick --events --all-databases --flush-logs --master-data=2 --routines --single-transaction > $dumpfile
if [[ $? -eq 0 ]];then
echo "$(date +'%Y-%m-%d %H:%M:%S') All Database is backup successfully !" >> $log_file
# 压缩备份文件
echo "$(date +'%Y-%m-%d %H:%M:%S') Compress file..." >> $log_file
$tar zcPf $gzdumpfile $dumpfile
# 删除未压缩文件
echo "$(date +'%Y-%m-%d %H:%M:%S') Delete sql file..." >> $log_file
rm -rf $dumpfile
echo "$(date +'%Y-%m-%d %H:%M:%S') All Database is backup successfully !!" >> $log_file
else
echo "$(date +'%Y-%m-%d %H:%M:%S') All Database is backup failed !" >> $log_file
fi
echo "" >> $log_file
echo "******************************************** End *************************************************" >> $log_file
echo "" >> $log_file