文章目录
- 1、安装
- [2、完整卸载 mysql(pod/deployment/service/pvc)](#2、完整卸载 mysql(pod/deployment/service/pvc))
1、安装
1.1、环境
docker 部署 minikube,minikube 中部署 mysql
Mac,注意芯片类型 和 workbench 版本适配(workbench最新版 8.0.40,对于image mysql:version, version 的值 M2 版本 8.0.30, Intel 芯片 8.0)
1.2、workbench 崩溃问题
在 MySQL Workbench 中,点击 Help 菜单,然后选择 About MySQL Workbench 查看版本
kubectl exec -it -- mysql --version 查看k8s 中 mysql 版本
1.1、deployment.yaml 文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0 # M2 版本 8.0.30,目前最新 workbench 最高到 8 #非 arm 架构芯片可以写 image: mysql:8.0
env:
- name: MYSQL_ROOT_PASSWORD
value: rootadmin # 设置 root 用户密码
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-storage
mountPath: /var/lib/mysql # MySQL 数据存储路径
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc # 使用 PVC
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi # 请求的存储大小
storageClassName: standard # 确保与您的 PV 的存储类匹配
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
type: NodePort # 设置为 NodePort 类型
selector:
app: mysql # 选择与 Deployment 中相同的标签
ports:
- port: 3306 # Service 的端口
targetPort: 3306 # Pod 中的端口
nodePort: 30002 # 自定义 NodePort(可选,范围是 30000-32767)
1.2、运行
kubectl apply -f mysql-deployment.yaml
1.3、启动隧道,从宿主机直接访问 k8s 中的mysql
minikube get services
minikube service mysql-service
2、完整卸载 mysql(pod/deployment/service/pvc)
➜ simpled git:(security) ✗ kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-deployment-848886474f-6q94t 1/1 Running 0 3m54s
redis-559d94f9bc-mgfxt 1/1 Running 1 (32m ago) 4h57m
➜ simpled git:(security) ✗ kubectl delete pod mysql-deployment-848886474f-6q94t
pod "mysql-deployment-848886474f-6q94t" deleted
➜ simpled git:(security) ✗ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
mysql-deployment 0/1 1 0 4m19s
redis 1/1 1 1 4h57m
➜ simpled git:(security) ✗ kubectl delete deployment mysql-deployment
deployment.apps "mysql-deployment" deleted
➜ simpled git:(security) ✗ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h1m
mysql-service NodePort 10.106.158.26 <none> 3306:30002/TCP 4m44s
redis-service NodePort 10.108.203.83 <none> 6379:30001/TCP 4h58m
➜ simpled git:(security) ✗ kubectl delete service mysql-service
service "mysql-service" deleted
➜ simpled git:(security) ✗ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
mysql-pvc Bound pvc-59ff2870-64c4-46ca-a74d-b36380ceca0c 1Gi RWO standard <unset> 5m20s
➜ simpled git:(security) ✗ kubectl delete mysql-pvc
error: the server doesn't have a resource type "mysql-pvc"