Mariadb on k8s

在k8s上部署单实例mariadb

前提

  1. k8s 集群
  2. 持久化存储(NFS, Longhorn等)

构建自定义docker镜像

这里设置容器语言以及时区为上海东八区

1
2
3
4
5
6
7
8
9
FROM mariadb:10.1

ENV LANG="en_US.UTF-8" \
LANGUAGE="en_US:en" \
LC_ALL="en_US.UTF-8"

RUN \
echo "Asia/shanghai" > /etc/timezone && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

docker build -f mysql.Dockerfile -t vcancy/mariadb:10.1 .

1
2


服务编排

1
kubectl -n default apply -f mysql.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
## config map
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
cust.cnf: |
[mysqld]
max_connections=1000

---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mysql
name: mysql
spec:
replicas: 1
selector:
matchLabels:
name: mysql
template:
metadata:
labels:
name: mysql
spec:
containers:
- name: mysql
image: vcancy/mariadb:10.1
volumeMounts:
- name: mysql-data
mountPath: "/var/lib/mysql"
- name: mariadb-config
mountPath: "/etc/mysql/conf.d"
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mariadb-config
configMap:
name: mysql-config

##创建Service
---
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
name: mysql
spec:
type: ClusterIP
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: tcp-3306-3306
selector:
name: mysql

遇到的问题

1、自定义conf。使用官方docker镜像对于自定义的配置文件有要求,需要挂载到/etc/mysql/conf.d下以.cnf结尾同时 [mysqld]开头,不然会启动报错,配置重复等问题