-
创建mysql的yaml文件
cat << eof > mysql.yaml apiVersion: apps/v1 # 通过kubectl explain deployment命令查看版本 kind: Deployment # 资源类型 metadata:name: mysql-deployment # 资源名称namespace: mysql # 指定命名空间 spec:replicas: 1 # 控制器下的pod数selector:matchLabels:app: mysql # 这是控制器用来控制pod的标签template:metadata:labels:app: mysql # pod的标签spec:containers:- name: mysql # 容器名image: mysql:8.0.20 # 容器镜像env: # mysql环境参数- name: MYSQL_ROOT_PASSWORD # 指定ROOT用户密码value: "@Qv110119"ports: # 容器暴露的端口- name: mysqlcontainerPort: 3306 # volumeMounts: # 容器内的被挂载目录 # - name: mysql-data # mountPath: /var/lib/mysql # - name: mysql-conf # mountPath: /etc/my.cnf # volumes: # 声明volume(宿主机目录), name为logs-volume # - name: mysql-data # nfs: # server: 192.168.174.30 # nfs服务器地址 # path: /root/nfs/mysql/data # 共享文件路径(服务器地址) --- apiVersion: v1 kind: Service metadata:name: mysql-nodeportnamespace: mysql spec:selector:app: mysql # 服务通过此标签连接deployment资源来暴露端口type: NodePort # 服务类型:NodePortports: # 不指定NodePort端口就会自动分配,(默认的取值范围是:30000-32767)- protocol: TCPport: 3306targetPort: 3306 eof
-
创建mysq应用:
kubectl apply -f mysql.yaml
-
查看pod:
[root@master mysql]# kubectl get deployment,pod,svc -n mysql NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/mysql-deployment 1/1 1 1 7m57s deployment.apps/nginx 1/1 1 1 15d NAME READY STATUS RESTARTS AGE pod/mysql-deployment-b68b5b8bc-jps5c 1/1 Running 0 7m57s pod/nginx-6867cdf567-phn5l 1/1 Running 1 148m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d service/mysql-nodeport NodePort 10.98.118.107 <none> 3306:30160/TCP 7m57s service/nginx NodePort 10.103.194.224 <none> 80:30105/TCP 15d
-
远程连接:
[root@localhost ~]# mysql -h192.168.174.30 -P30160 -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.20 MySQL Community Server - GPL Copyright (c) 2000, 2023, 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>