一、背景:
因为业务需求,需要在kubernetes集群下部署一个mysql数据库 8.0.20版本的单机服务。
具体实施过程如下:
二、实施部署mysql数据库:
mysql 8.0.20的镜像:
dockerhub.jiang.com/jiang-public/mysql:8.0.20-stjh
这里选择的namespace是:dev
1、创建pv、pvc存储:
注:这里的kubernetes集群环境存在storageclass组建,所以这里只要是pvc组建的配置就可以。
pvc:mysql-db-pvc 的yml配置内容
apiVersion: storage.k8s.io/v1
kind: PersistentVolumeClaim
metadata:
name: db-mysql-pvc
namespace: dev
finalizers:
- kubernetes.io/pvc-protection
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: hpe-san
volumeMode: Filesystem
应用该pvc资源
# kubectl apply -f mysql-db-pvc.yaml
查看该pv资源
# kubectl get pvc -n dev
若需要进行删除pvc
方式一:
# kubectl delete pvc mysql-db-pvc -n dev
方式二:
# kubectl delete -f mysql-db-pvc.yaml
2、创建ConfigMap组建:
ConfigMap 是kuberbetes中的一种 API对象,用来将非机密性的数据保存到键值对中。使用时,Pods 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。ConfigMap 的主要作用就是为了让镜像和配置文件解耦,以便实现镜像的可移植性和可复用性。
使用 ConfigMap 的限制条件:
1、ConfigMap 需要在 Pod 启动前创建出来;
2、并且只有当 ConfigMap 和 Pod 处于同一命名空间时,才可以被 Pod 引用;
3、当 Pod 挂载 ConfigMap 绑定的目录时,目录下的目录并不会挂载到 Pod 内,只有目录下的文件会被挂载。
mysql-db-cm的yaml配置内容:
apiVersion: v1
kind: ConfigMap
metadata:
annotations:
configlabels: ""
name: db-mysql-cm
namespace: dev
data:
my.cnf: |
[mysql]
default-character-set=utf8
[mysqld]
datadir=/var/lib/mysql
max_connections=1000
innodb_lock_wait_timeout=500
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
default-storage-engine=INNODB
sort_buffer_size=256MB
lower_case_table_names=1
default-time-zone='+8:00'
mysql的参数解释:
# mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 数据库文件位置
datadir=/var/lib/mysql
# 允许最大连接数
max_connections=1000
# innodb的dml操作的行级锁的等待时间
innodb_lock_wait_timeout=500
# 设置mysql服务端默认字符集
character-set-server=utf8mb4
# 默认创建新数据的新建排序规则
collation-server=utf8mb4_general_ci
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 缓存大小
sort_buffer_size=256MB
# 大小写敏感配置项0为敏感,1为不敏感
lower_case_table_names=1
# 选择正8区
default-time-zone='+8:00'
应用configmap资源:
# kubectl apply -f mysql-db-cm.yaml
查看configmap:
# kubectl get cm -n dev
3、部署deployment的mysql服务:
mysql-deploy.yaml配置内容:
kind: Deployment
metadata:
name: mysql-db-okbny
namespace: dev
labels:
app: mysql-db-okbny
name: mysql-db
version: v8.0.2
annotations:
deployment.kubernetes.io/revision: '6'
sidecar.istio.io/inject: 'false'
spec:
replicas: 1
selector:
matchLabels:
app: mysql-db-okbny
name: mysql-db
template:
metadata:
labels:
app: mysql-db-okbny
name: mysql-db
version: v8.0.2
annotations:
cni.projectcalico.org/ipv4pools: '["172.25.0.0/16"]'
sidecar.istio.io/inject: 'false'
system/container-registry-map: '{"mysql-db":"default"}'
system/registry: default
v1.multus-cni.io/default-network: kube-system/calico@eth0
spec:
volumes:
- name: db-mysql-cm
configMap:
name: db-mysql-cm
defaultMode: 420
- name: db-mysql-pvc
persistentVolumeClaim:
claimName: db-mysql-pvc
containers:
- name: mysql-db
image: 'dockerhub.jiang.com/jiang-public/mysql:8.0.20-stjh'
ports:
- containerPort: 3306
protocol: TCP
env:
- name: MYSQL_ROOT_PASSWORD
value: foura@1234
resources:
limits:
cpu: '2'
memory: 2Gi
requests:
cpu: '1'
memory: 1Gi
volumeMounts:
- name: db-mysql-cm
mountPath: /etc/my.cnf
subPath: my.cnf
- name: db-mysql-pvc
mountPath: /var/lib/mysql
livenessProbe:
exec:
command:
- sh
- '-c'
- 'mysqladmin -uroot -p${MYSQL_ROOT_PASSWORD} ping'
initialDelaySeconds: 30
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
securityContext:
privileged: false
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
serviceAccountName: default
serviceAccount: default
securityContext: {}
strategy:
type: Recreate
minReadySeconds: 10
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
应用deploy资源:
# kubectl apply -f mysql-deploy.yaml
查看deploy的mysql服务:
# kubectl get -f deploy
4、部署service的mysql服务:
mysql-db-service的服务配置内容:
kind: Service
metadata:
name: mysql-db
namespace: dev
labels:
name: mysql-db
system/appName: dev
annotations:
binding_domains: ''
system/https: 'false'
system/lbgroup: group-dgdxw
system/ruleComment: ''
system/ruleName: db-mysql
spec:
ports:
- name: tcp-port-0
protocol: TCP
port: 3306
targetPort: 3306
selector:
name: mysql-db
type: ClusterIP
sessionAffinity: None
应用service资源:
# kubectl apply -f mysql-db-service.yaml
查看service资源:
# kubectl get svc