一、创建命名空间
# cat mysql8-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:name: mysql8labels: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: mysql8name: mysql8-config
data:#my.cnf代表mysql8的配置文件名称my.cnf: |[mysql]default-character-set=utf8mb4[mysqld]datadir=/var/lib/mysqlmax_connections=3000innodb_lock_wait_timeout=500character-set-server=utf8mb4collation-server=utf8mb4_general_cidefault-storage-engine=INNODBsort_buffer_size=512MBlower_case_table_names=1default-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: mysql8name: mysql8
spec:type: NodePortports:- port: 3306targetPort: 3306nodePort: 32306selector:app: mysql8
---
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:namespace: mysql8name: mysql8labels:app: mysql8
spec:replicas: 1selector:matchLabels:app: mysql8template:metadata:name: mysql8labels:app: mysql8spec:containers:- name: mysql8# 容器对应的Docker Image,即镜像名image: docker.io/library/mysql:8.2.0# Always:总是拉取;IfNotPresent:默认值,本地有则不拉取;Never:只用本地镜像,从不拉取;imagePullPolicy: IfNotPresentports:- containerPort: 3306env:- name: MYSQL_ROOT_PASSWORDvalue: "YudB6vw4Fw#"livenessProbe:initialDelaySeconds: 30periodSeconds: 10timeoutSeconds: 5successThreshold: 1failureThreshold: 3exec:# 在容器中执行此命令,如果命令返回状态码为0,则认为探测成功command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]# 判断容器是否能进入ready状态,探针失败则进入noready状态,并从service的endpoints中剔除此容器readinessProbe:initialDelaySeconds: 10periodSeconds: 10timeoutSeconds: 5successThreshold: 1failureThreshold: 3exec:# 在容器中执行此命令,如果命令返回状态码为0,则认为探测成功command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]volumeMounts:- name: mysql-datamountPath: /var/lib/mysql- name: mysql-config# mysql的配置文件my.cnfmountPath: /etc/mysql/conf.d/my.cnf subPath: my.cnf- name: localtimereadOnly: truemountPath: /etc/localtimevolumes:- name: mysql-datanfs:server: 192.168.110.38path: /nfs/mysql- name: mysql-configconfigMap:name: mysql8-config- name: localtimehostPath:type: Filepath: /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