在VirtualBox Centos7 VM上将kubeadm与两节点群集一起使用。
我有一个用R编写的应用程序和一个位于各自容器中的mysql数据库。
我已成功遵循instructions to setup nginx ingress controller,以便可以通过本地计算机在VM外部访问该应用程序。检查:)

但是,现在当应用程序(R)尝试访问mysql服务时,名称无法解析。与从bash ping'mysql'相同。这不再起作用:

mydb<-dbConnect(MySQL(), user = 'root', password ='password',
                dbname = 'prototype', host = 'mysql')


相反,我必须使用工作区的IP。

mydb<-dbConnect(MySQL(), user = 'root', password ='password',
                dbname = 'prototype', host = '10.244.1.233')


但是,这在重启和系统更改后是否会改变?我想要一种更静态的方式来引用mysql数据库。

$ kubectl get endpoints
NAME         ENDPOINTS                             AGE
kubernetes   192.168.56.101:6443                   5h
mysql        10.244.1.233:3306                     41m
r-user-app   10.244.1.232:8787,10.244.1.232:3838   2h

$ kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                         AGE
kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP                         5h
mysql        ClusterIP      10.96.138.132   <none>        3306/TCP                        28m
r-user-app   LoadBalancer   10.100.228.80   <pending>     3838:32467/TCP,8787:31754/TCP   2h

$ kubectl get ing
NAME         HOSTS              ADDRESS   PORTS     AGE
r-user-app   storage.test.com             80, 443   3h

$ kubectl describe service mysql
Name:              mysql
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=neurocore,tier=mysql
Type:              ClusterIP
IP:                10.96.138.132
Port:              <unset>  3306/TCP
TargetPort:        3306/TCP
Endpoints:         10.244.1.236:3306
Session Affinity:  None
Events:            <none>

 ps auxw | grep kube-proxy
root      1914  0.1  0.3  44848 21668 ?        Ssl  11:03   0:20 /usr/local/bin/kube-proxy --config=/var/lib/kube-proxy/config.conf
root     29218  0.0  0.0 112660   980 pts/1    R+   14:23   0:00 grep --color=auto kube-proxy

$iptables-save | grep mysql
-A KUBE-SEP-7P27CEQL6WJZRBQ5 -s 10.244.1.236/32 -m comment --comment "default/mysql:" -j KUBE-MARK-MASQ
-A KUBE-SEP-7P27CEQL6WJZRBQ5 -p tcp -m comment --comment "default/mysql:" -m tcp -j DNAT --to-destination 10.244.1.236:3306
-A KUBE-SERVICES ! -s 10.244.0.0/16 -d 10.96.138.132/32 -p tcp -m comment --comment "default/mysql: cluster IP" -m tcp --dport 3306 -j KUBE-MARK-MASQ
-A KUBE-SERVICES -d 10.96.138.132/32 -p tcp -m comment --comment "default/mysql: cluster IP" -m tcp --dport 3306 -j KUBE-SVC-M7XME3WTB36R42AM
-A KUBE-SVC-M7XME3WTB36R42AM -m comment --comment "default/mysql:" -j KUBE-SEP-7P27CEQL6WJZRBQ5

最佳答案

根据您的svc,您应该能够从群集内访问mysql:3306

您是否尝试过kubectl exec -it r-user-app bash并从R app容器中ping mysql? host mysql应该返回类似“ mysql.cluster.local”的地址为127.21.0.01“(示例)的内容。或者返回任何错误。如果没有错误,则dbConnect()可能不喜欢主机名?

10-08 08:40