[英]How to connect MySQL running on Kubernetes
I have deployed my application on Google gcloud
container engine.我已经在 Google gcloud
容器引擎上部署了我的应用程序。 My application required MySQL
.我的申请需要MySQL
。 Application is running fine and connecting to MySQL
correctly.应用程序运行正常并正确连接到MySQL
。 But I want to connect MySQL
database from my local machine using MySQL
Client ( Workbench
, or command line), Can some one help me how to expose this to local machine?但是我想使用MySQL
客户端( Workbench
或命令行)从我的本地计算机连接MySQL
数据库,有人可以帮助我如何将其公开给本地计算机吗? and how can I open MySQL
command line on gcloud
shell?以及如何在gcloud
shell 上打开MySQL
命令行?
I have run below command but external ip is not there:我已经在命令下运行,但外部 ip 不存在:
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
app-mysql 1 1 1 1 2m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
app-mysql-3323704556-nce3w 1/1 Running 0 2m
$ kubectl get service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
app-mysql 11.2.145.79 <none> 3306/TCP 23h
EDIT编辑
I am using below yml
file:我正在使用下面的yml
文件:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: app-mysql
spec:
replicas: 1
template:
metadata:
labels:
app: app-mysql
spec:
volumes:
- name: data
emptyDir: {}
containers:
- name: mysql
image: mysql:5.6.22
env:
- name: MYSQL_USER
value: root
- name: MYSQL_DATABASE
value: appdb
ports:
- containerPort: 3306
volumeMounts:
- name: data
mountPath: /var/lib/mysql/
---
apiVersion: v1
kind: Service
metadata:
name: app-mysql
spec:
selector:
app: app-mysql
ports:
- port: 3306
Try the kubectl port-forward
command.试试kubectl port-forward
命令。
In your case;在你的情况下; kubectl port-forward app-mysql-3323704556-nce3w 3306:3306
See The documentation for all available options.有关所有可用选项,请参阅文档。
There are 2 steps involved:涉及两个步骤:
1 ) You first perform port forwarding from localhost to your pod: 1 ) 您首先执行从本地主机到您的 pod 的端口转发:
kubectl port-forward <your-mysql-pod-name> 3306:3306 -n <your-namespace>
2 ) Connect to database: 2)连接数据库:
mysql -u root -h 127.0.0.1 -p <your-password>
Notice that you might need to change 127.0.0.1
to localhost
- depends on your setup.请注意,您可能需要将127.0.0.1
更改为localhost
- 取决于您的设置。
If host is set to:如果主机设置为:
localhost
- then a socket or pipe is used. localhost
- 然后使用套接字或 pipe。
127.0.0.1
- then the client is forced to use TCP/IP. 127.0.0.1
- 然后客户端被迫使用 TCP/IP。
You can check if your database is listening for TCP connections with netstat -nlp
.您可以使用netstat -nlp
检查您的数据库是否正在侦听 TCP 连接。
Read more in:阅读更多:
Cant connect to local mysql server through socket tmp mysql sock 无法通过套接字 tmp mysql sock 连接到本地 mysql 服务器
To add to the above answer, when you add --address 0.0.0.0 kubectl should open 3306 port to the INTERNET too (not only localhost)!要添加到上述答案中,当您添加--address 0.0.0.0 kubectl 时,还应该打开 3306 到 INTERNET 的端口(不仅是本地主机)!
kubectl port-forward POD_NAME 3306:3306 --address 0.0.0.0
Use it with caution for short debugging sessions only, on development systems at most.仅在短期调试会话中谨慎使用它,最多在开发系统上使用。 I used it in the following situation:我在以下情况下使用它:
You need to add a service to your deployment.您需要向部署添加服务。 The service will add a load balancer with a public ip in front of your pod, so it can be accessed over the public internet.该服务将在您的 pod 前添加一个带有公共 ip 的负载均衡器,以便可以通过公共互联网访问它。
See the documentation on how to add a service to a Kubernetes deployment .请参阅有关如何向 Kubernetes 部署添加服务的文档。 Use the following code to add a service to your app-mysql deployment:使用以下代码向您的 app-mysql 部署添加服务:
kubectl expose deployment/app-mysql
You may also need to configure your MySQL service so it allows remote connections.您可能还需要配置 MySQL 服务以允许远程连接。 See this link on how to enable remote access on MySQL server :请参阅有关如何在 MySQL 服务器上启用远程访问的链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.