gpt4 book ai didi

mysql - kubernetes 无法连接 mysql

转载 作者:行者123 更新时间:2023-11-29 06:27:50 26 4
gpt4 key购买 nike

我已经在 kubernetes 中部署了一个 mysql 数据库并通过服务公开。当我的应用程序尝试连接到该数据库时,它一直被拒绝。当我尝试在本地访问它时,我也会得到同样的结果。 Kubernetes 节点在 minikube 中运行。

apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
type: NodePort
selector:
app: mysql
ports:
- port: 3306
protocol: TCP
targetPort: 3306
---
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_db
imagePullPolicy: Never
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: "/var/lib/mysql"
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim

这是我用于持久存储的 yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/Users/Work/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi

在此之后,我通过运行 minikube service list 得到这个:

default              | mysql-service             | http://192.168.99.101:31613 

但是,我无法从我的应用程序或本地计算机访问数据库。我缺少什么或者我配置错误吗?

编辑:我在这里没有定义任何环境,因为 docker 运行的镜像已经是一个正在运行的 mysql 数据库,并且一些脚本也在 docker 镜像中运行。

最佳答案

Mysql肯定没有启动,通过检查日志确认。 kubectl get pods | grep mysql ; kubectl logs -f $POD_ID 。请记住,您必须指定环境变量 MYSQL_DATABASEMYSQL_ROOT_PASSWORD 才能启动 mysql。如果您不想为 root 设置密码,请指定相应的命令。这里我给你一个mysql 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_db
imagePullPolicy: Never
env:
- name: MYSQL_DATABASE
value: main_db
- name: MYSQL_ROOT_PASSWORD
value: s4cur4p4ss
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: "/var/lib/mysql"
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim

关于mysql - kubernetes 无法连接 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58468212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com