1.编辑 mysql.yaml

(deployment, service)

apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-rc
spec:
replicas: 1
selector:
matchLabels:
app1: mysql
template:
metadata:
labels:
app1: mysql
spec:
#添加标签调度
#nodeSelector:
# app1: mysql

#根据 NodeName 调度
nodeName: kube01
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: ALLOW_EMPTY_PASSWORD
value: "yes"
- name: MYSQL_ROOT_USER
value: root
- name: MYSQL_ROOT_PASSWORD
value: Mrtoo902li
- name: MYSQL_CHARACTER_SET
value: utf8
- name: MYSQL_COLLATE
value: utf8_general_ci
- name: MYSQL_DATABASE
value: face_privatization
volumeMounts:
- name: mysql
mountPath: /var/lib/mysql
volumes:
- name: mysql
hostPath:
path: /root/test/data2/mysql
restartPolicy: Always



---
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
labels:
app1: mysql
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 32306
selector:
app1: mysql

说明

挂载的卷 path: /root/test/data2/mysql 不用配置数据文件,
启动之后,数据就有数据了


2.创建

kubectl apply -f  mysql.yaml

3.查看pod

kubectl get pods -o wide

mysql-rc-6f74fc486-l52cv 1/1 Running 0 11s 10.211.65.17 kube01 <none> <none>

4.查看service

kubectl get svc

5.测试mysql连接

kubectl exec -it mysql-rc-6f74fc486-l52cv -- /bin/bash


#测试连接

mysql -u root -p'Mrtoo902li' -h 192.20.5.75 -P 32306


mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>