使用Python连接到MySQL的Pod
引言
这篇文章更多地作为我的备忘录。之前在网上查询并尝试了各种方法都不成功,后来我尝试了一些其他方法,偶然间找到了一种能行的设置,并在这里分享出来。对于那些了解的人来说,可能会觉得“当然是这样啦”,但是因为在网上查找不到太多相关信息,所以我决定把它发布在这里。
经过
在Kubernetes上创建了一个MySQL的Pod。出于各种原因,我想使用这个Pod来编辑一个表,需要使用Python的mysql.connector。于是我需要输入连接信息来连接MySQL。要输入的信息有以下5个。
-
- host
-
- port
-
- user
-
- password
- database
问题发生
这个的用户、密码、数据库很容易知道。我判断端口应该是普通的3306。但是,我不知道主机。我设定的主机被拒绝了。
最开始我设置了MySQL的服务名。但是它不起作用。我原以为是localhost(127.0.0.1),但也没有效果。那么为什么呢?
MySQL的配置
以下是MySQL的配置参考。服务的Yaml文件。
apiVersion: v1
kind: Service
metadata:
name: mysql-test
labels:
app: mysql
spec:
type: ClusterIP
ports:
- port: 3306
targetPort: 3306
selector:
app: mysql
部署的Yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-test
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
nodeSelector:
kubernetes.io/hostname: worker
containers:
- image: mysql:5.7
name: mysql-test
env:
- name: MYSQL_ROOT_PASSWORD
value: ROOT_PASSWORD
- name: MYSQL_DATABASE
value: DB
- name: MYSQL_USER
value: USER
- name: MYSQL_PASSWORD
value: USER_PASSWORD
ports:
- containerPort: 3306
name: mysql-test
volumeMounts:
- name: mysql-storage-test
mountPath: /var/lib/mysql
volumes:
- name: mysql-storage-test
persistentVolumeClaim:
claimName: mysql-pvc-test
dnsPolicy: ClusterFirstWithHostNet
MySQL Pod的详细信息
Name: mysql-test-8fd9f44fb-5tcx9
Namespace: default
Priority: 0
Service Account: default
Node: worker/192.168.100.169
Start Time: Tue, 24 Oct 2023 05:50:11 +0000
Labels: app=mysql
pod-template-hash=8fd9f44fb
Annotations: <none>
Status: Running
IP: 10.42.1.8
IPs:
IP: 10.42.1.8
Controlled By: ReplicaSet/mysql-test-8fd9f44fb
Containers:
mysql-test:
Container ID: containerd://1accde85c6b6bdfd7655f0a121b62e272d988c4cc96f1791584673b2437caae0
Image: mysql:5.7
Image ID: docker.io/library/mysql@sha256:4f9bfb0f7dd97739ceedb546b381534bb11e9b4abf013d6ad9ae6473fed66099
Port: 3306/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 24 Oct 2023 05:50:13 +0000
Ready: True
Restart Count: 0
Environment:
MYSQL_ROOT_PASSWORD: ROOT_PASSWORD
MYSQL_DATABASE: DB
MYSQL_USER: USER
MYSQL_PASSWORD: USER_PASSWORD
Mounts:
/var/lib/mysql from mysql-storage-test (rw)
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-qrj9b (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
mysql-storage-test:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: mysql-pvc-test
ReadOnly: false
kube-api-access-qrj9b:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: kubernetes.io/hostname=c0a20092-worker
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
解决编码问题
根据结果来看,将这个MySQL的Pod的IP地址设置为主机后,运行良好。
Pod的IP地址可以在描述中的“IP”中找到。本次是“10.42.1.8”。
在虚拟机或本地PC中,“localhost”与Kubernetes的Pod中的“localhost”可能不同。