一、創建命名空間
# 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