---
apiVersion: v1
data:
mysqld.cnf: |-
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
bind-address = 0.0.0.0
server_id = 1918
default-time-zone='+08:00'
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_connections=1024
default_storage_engine=innodb
skip_external_locking
lower_case_table_names=1
skip_host_cache
skip_name_resolve
character_set_server=utf8mb4
max_allowed_packet = 12M
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
# ---------- binlog & errorlog & slowlog ---------- #
log_bin = mysql-bin
binlog_format = ROW
max_binlog_size = 100m
binlog_cache_size = 4m
max_binlog_cache_size = 512m
expire_logs_days = 7
log-error = /var/lib/mysql/error.log
slow-query-log = on
slow_query_log_file = /var/lib/mysql/slow.log
long_query_time = 1
[client]
default-character-set=utf8mb4
kind: ConfigMap
metadata:
annotations:
labels:
app: mysql
name: mysql-cm
namespace: bigdata
---
apiVersion: v1
kind: Service
metadata:
annotations:
labels:
app: mysql
name: mysql-svc
namespace: bigdata
spec:
ports:
- name: mysql
port: 3306
protocol: TCP
targetPort: 3306
selector:
app: mysql
mysql 的数据目录使用的 hostpath 的方式,通过亲和性来绑定节点,需要提前给对应的节点打上
mysql=
这样的标签
kubectl label node <nodename> mysql=
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
labels:
app: mysql
name: mysql
namespace: bigdata
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: mysql
operator: Exists
containers:
- name: mysql
env:
- name: APP_NAME
value: mysql
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MYSQL_ROOT_USER
value: root
- name: MYSQL_ROOT_PASSWORD
value: YWJjMTIzCg==
- name: TZ
value: Asia/Shanghai
- name: LANG
value: en_US.UTF-8
image: mysql:5.7.31
imagePullPolicy: IfNotPresent
livenessProbe:
exec:
command:
- /bin/bash
- -c
- mysql -u${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD} -e 'SELECT 1'
initialDelaySeconds: 30
periodSeconds: 30
timeoutSeconds: 10
ports:
- containerPort: 3306
name: mysql
protocol: TCP
readinessProbe:
exec:
command:
- /bin/bash
- -c
- mysql -u${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD} -e 'SELECT 1'
initialDelaySeconds: 30
periodSeconds: 30
timeoutSeconds: 10
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-data-dir
- mountPath: /etc/mysql/conf.d/
name: mysql-config
dnsPolicy: ClusterFirst
restartPolicy: Always
securityContext: {}
terminationGracePeriodSeconds: 10
volumes:
- name: mysql-data-dir
hostPath:
path: /data/k8s_data/mysql
type: DirectoryOrCreate
- name: mysql-config
configMap:
name: mysql-cm
- 和 deployment 的一样,需要给节点打上
mysql-back=
的标签- 注意
schedule
的时间是和controller-manager
组件的时区一样的,如果是 kubeadm 部署的,就要注意容器内的时间是不是和本地相差八小时
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: mysql-backup
namespace: bigdata
spec:
schedule: "0 1 * * *"
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 3
jobTemplate:
spec:
template:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: mysql-back
operator: Exists
containers:
- name: mysql-backup
imagePullPolicy: IfNotPresent
image: mysql:5.7.31
env:
- name: MYSQL_ROOT_USER
value: root
- name: MYSQL_ROOT_PASSWORD
value: YWJjMTIzCg==
- name: MYSQL_HOST
value: mysql-svc.bigdata.svc.cluster.local
- name: TZ
value: Asia/Shanghai
- name: LANG
value: en_US.UTF-8
command:
- /bin/sh
- -c
- |
set -ex
mysqldump --host=${MYSQL_HOST} --user=${MYSQL_ROOT_USER} \
--password=${MYSQL_ROOT_PASSWORD} \
--routines --all-databases --single-transaction \
> /mysql-backup/mysql-$(date +"%Y%m%dT%H_%M_%S").sql && find /mysql-backup/ -type f -mtime +3 -exec rm -rf {} \;
volumeMounts:
- name: mysql-backup
mountPath: /mysql-backup
restartPolicy: OnFailure
volumes:
- name: mysql-backup
hostPath:
path: /data/k8s_data/mysql-back
type: DirectoryOrCreate
- phpmyadmin-github
- phpmyadmin-docker-github
- 这个是 mysql 的图形化界面
- 如果有 ingress,可以配置成 clusterip 的模式
- 如果没有 ingress,可以配置成 nodeport 的模式
- 下面两个都配置了,可以各取所需
- 下面的 nodeport 配置了 30003 端口,需要检查一下自己本地有没有被占用,也可以自己随意修改,端口范围值一般是 30000-32767,具体要看 apiserver 的配置
---
apiVersion: v1
kind: Service
metadata:
namespace: bigdata
name: phpmyadmin
labels:
app: phpmyadmin
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
selector:
app: phpmyadmin
---
apiVersion: v1
kind: Service
metadata:
namespace: bigdata
name: phpmyadmin-np
labels:
app: phpmyadmin
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http-np
nodePort: 30003
selector:
app: phpmyadmin
- 下面的 PMA_HOST 需要替换成自己的 mysql 地址
- 下面的 PMA_PORT 需要替换成自己的 mysql 端口
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
labels:
app: phpmyadmin
name: phpmyadmin
namespace: bigdata
spec:
replicas: 1
selector:
matchLabels:
app: phpmyadmin
template:
metadata:
annotations:
labels:
app: phpmyadmin
spec:
containers:
- name: phpmyadmin
image: phpmyadmin/phpmyadmin:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
env:
- name: PMA_HOST
value: mysql-svc.bigdata.svc.cluster.local
- name: PMA_PORT
value: "3306"
pod 变成 running 之后,就可以通过 ip 加 nodeport 的方式来访问了,使用 mysql 数据库内的用户名和密码登录,可以自己先提前在 mysql 数据库里面创建好