k8s部署mysql:
1.创建数据目录文件夹:
mkdir /var/lib/mysql
#创建配置目录:
mkdir cicd
eg:/home/kui/ccmysql/cicd
2.创建secret对象,存储mysql账号密码:
Secret对象:mysql-auth
#命令:
kubectl create secret generic mysql-auth --from-literal=username=root --from-literal=password=123456
#创建完成后查看mysql-auth对象
kubectl get secret
#查看secret对象yaml文件
kubectl get secret mysql-auth -o yaml
3.创建mysql deployment并完成pod部署:
创建deploy:deployment-cicd-mysql.yaml
vim deployment-cicd-mysql.yaml
yaml文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: cicd-mysql
spec:
replicas: 1
selector:
matchLabels:
app: cicd-mysql
template:
metadata:
labels:
app: cicd-mysql
spec:
containers:
- name: cicd-mysql
image: drupalci/mysql-5.7
imagePullPolicy: IfNotPresent #镜像拉取策略:如果不存在就拉取镜像
args:
- "--ignore-db-dir=lost+found"
ports:
- containerPort: 3306
volumeMounts: #挂载数据卷
- name: mysql-data
mountPath: "/var/lib/mysql" #挂载到容器内的目录
env: #环境变量
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-auth
key: password #引用mysql-auth中的password值
volumes: #数据卷
- name: mysql-data
hostPath: #宿主机路径
path: /var/lib/mysql #宿主机的目录
type: Directory
备注:以下配置文档中有,但视频没用
tolerations:
- key: "mysql"
operator: "Equal"
value: "true"
effect: "NoSchedule"
#创建mysql deployment:
#启动成功
kubectl apply -f deployment-cicd-mysql.yaml
4.创建mysql的service:
vim service-cicd-mysql.yaml
apiVersion: v1
kind: Service
metadata: #源数据
name: service-cicd-mysql
spec: #规格
selector: #选择器
app: cicd-mysql #与deployment配置中的container-name相对应
ports: #将pod的 3306端口代理到宿主机的3306端口
- protocol: TCP
port: 3306
targetPort: 3306 #虚拟端口->真正的端口要看Service启动后实际分配的
type: NodePort #不写就不能访问到pod