简体   繁体   English

如何在Kubernetes集群中使用脚本语言连接数据库

[英]How to connect to database using scripting language inside kubernetes cluster

Using Google Kubernetes Engine, I have set up a mysql database deployment & service in kubernetes, much like the wordpress example . 使用Google Kubernetes Engine,我在kubernetes中设置了mysql数据库部署和服务,就像wordpress示例一样 I want to access it via R with R which I have in another deployment & service in my kubernetes cluster (r-user-app, w/2 containers). 我想通过R和R来访问它,我在kubernetes集群(r-user-app,w / 2容器)的另一个部署和服务中拥有R。 This worked with networks in docker. 这适用于docker中的网络。

> kubectl get services
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)                         AGE
kubernetes   ClusterIP      10.15.240.1     <none>           443/TCP                         8d
mysql        LoadBalancer   10.15.248.129   35.227.123.232   3306:31410/TCP                  3m
r-user-app   LoadBalancer   10.15.255.135   35.227.33.206    3838:31003/TCP,8787:32395/TCP   1d

Exec into the rstudio container inside the r-user-app deployment, I see the mysql service: 执行到r-user-app部署内的rstudio容器,我看到mysql服务:

root@r-user-app-1788932656-vgczh:/# getent hosts mysql
10.15.248.129   mysql.default.svc.cluster.local

However, when I try to get it via R, I have no such luck: 但是,当我尝试通过R获得它时,我没有这样的运气:

root@r-user-app-1788932656-vgczh:/# R
R version 3.4.2 (2017-09-28) -- "Short Summer"
-----
> require("RMySQL")
Loading required package: RMySQL
Loading required package: DBI
> mydb<-dbConnect(MySQL(), user="root", password="mypassword", host="10.15.248.129")
Error in .local(drv, ...) :
  Failed to connect to database: Error: Can't connect to MySQL server on '10.15.248.129' (107)

Using the EXTERNAL-IP doesn't work either, and putting port=3306 doesn't help. 使用EXTERNAL-IP也不起作用,将port=3306放置也无济于事。

Same thing in python: 在python中也是如此:

> db=MySQLdb.connect("10.15.248.129", "root", "mypassword")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 193, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, 'Can\'t connect to MySQL server on \'10.15.248.129\' (111 "Connection refused")')

Maybe I don't even know the right question to ask! 也许我什至不知道问正确的问题! Please help! 请帮忙! #kubenube #kubenube

You don't need to use the IP. 您不需要使用IP。 You can access the servers directly by name. 您可以直接通过名称访问服务器。 You even don't need to use the full DNS name with "mysql.default.svc.cluster.local", just "mysql" is enough. 您甚至不需要将完整的DNS名称与“ mysql.default.svc.cluster.local”一起使用,只需“ mysql”就足够了。

You should also check the mysql pod, if it is deployed properly. 您还应该检查mysql pod(如果已正确部署)。 Using Type=LoadBalancer is not a good idea for databases, as it opens them to the outer network. 对于数据库来说,使用Type=LoadBalancer并不是一个好主意,因为它将数据库打开到外部网络。

Answering my own question: This was an issue with the yaml. 回答我自己的问题:这是Yaml的问题。 Needed to be sure to match the Service 需要确保与Service匹配

  selector:
    app: my-app
    tier: mysql

and the Deployment Deployment

spec:
  selector:
    matchLabels:
      app: my-app
      tier: mysql

Now we can access the database using "mysql" instead of a host IP address. 现在,我们可以使用“ mysql”而不是主机IP地址访问数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM