一、创建命名空间
# cat mysql8-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: mysql8
labels:
name: mysql8
# kubectl apply -f mysql8-namespace.yaml
namespace/mysql8 created
# kubectl get ns|grep mysql8
mysql8 Active 8s
二、创建mysql配置文件参数
# cat mysq8-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
namespace: mysql8
name: mysql8-config
data:
#my.cnf代表mysql8的配置文件名称
my.cnf: |
[mysql]
default-character-set=utf8mb4
[mysqld]
datadir=/var/lib/mysql
max_connections=3000
innodb_lock_wait_timeout=500
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
default-storage-engine=INNODB
sort_buffer_size=512MB
lower_case_table_names=1
default-time-zone='+8:00'
# kubectl apply -f mysq8-configmap.yaml
configmap/mysql8-config created
三、部署mysql
# cat mysql8.yaml
# Service
apiVersion: v1
kind: Service
metadata:
namespace: mysql8
name: mysql8
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
nodePort: 32306
selector:
app: mysql8
---
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: mysql8
name: mysql8
labels:
app: mysql8
spec:
replicas: 1
selector:
matchLabels:
app: mysql8
template:
metadata:
name: mysql8
labels:
app: mysql8
spec:
containers:
- name: mysql8
# 容器对应的Docker Image,即镜像名
image: docker.io/library/mysql:8.2.0
# Always:总是拉取;IfNotPresent:默认值,本地有则不拉取;Never:只用本地镜像,从不拉取;
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "YudB6vw4Fw#"
livenessProbe:
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
exec:
# 在容器中执行此命令,如果命令返回状态码为0,则认为探测成功
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
# 判断容器是否能进入ready状态,探针失败则进入noready状态,并从service的endpoints中剔除此容器
readinessProbe:
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
exec:
# 在容器中执行此命令,如果命令返回状态码为0,则认为探测成功
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
# mysql的配置文件my.cnf
mountPath: /etc/mysql/conf.d/my.cnf
subPath: my.cnf
- name: localtime
readOnly: true
mountPath: /etc/localtime
volumes:
- name: mysql-data
nfs:
server: 192.168.110.38
path: /nfs/mysql
- name: mysql-config
configMap:
name: mysql8-config
- name: localtime
hostPath:
type: File
path: /etc/localtime
默认镜像无法下载,可以使用下面地址下载,然后重新做tag即可:
# docker pull dhub.kubesre.xyz/library/mysql:8.2.0
# docker tag dhub.kubesre.xyz/library/mysql:8.2.0 docker.io/library/mysql:8.2.0