Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing kube-dns outside of kubernetes cluster

Tags:

Similar question: How to expose kube-dns service for queries outside cluster?

I have a PerconaDB instance in a VM in Google compute engine. Next to it is running Kubernetes cluster where services connect to the PerconaDB.

When I login with MySQL client and do show processlist;, I see following:

| 175 | user       | 10.12.142.24:46124 | user | Sleep   |   14 |                                       | NULL                                                                                                 |         0 |             0 |
| 176 | user       | 10.12.142.24:46126 | user | Sleep   |   14 |                                       | NULL                                                                                                 |         0 |             0 |
| 177 | user       | 10.12.122.42:60806 | user | Sleep   |    2 |                                       | NULL                                                                                                 |         0 |             0 |
| 178 | user       | 10.12.122.43:55164 | user | Sleep   |   14 |                                       | NULL                                                                                                 |         1 |             0 |
| 179 | user       | 10.12.122.43:55166 | user | Sleep   |    4 |                                       | NULL                                                                                                 |         1 |             0 |
| 180 | user       | 10.12.141.11:35944 | user | Sleep   |   14 |                                       | NULL                                                                                                 |         1 |             0 |

Notice the number of different IPs for which I have no idea what they belong to. These are the pods inside the Kubernetes cluster and I would like to know their names so instead of 10.12.142.24:46124 I could see myservice-0dkd0:46124.

I thought the solution would be to somehow link the kube-dns service to the PerconaDB VM, but I have no idea, how to do that correctly. Also this is now running in production, so I don't want to experiment too much.

like image 994
Vojtěch Avatar asked Aug 20 '18 20:08

Vojtěch


People also ask

How do you access the service outside of Kubernetes cluster?

You have several options for connecting to nodes, pods and services from outside the cluster: Access services through public IPs. Use a service with type NodePort or LoadBalancer to make the service reachable outside the cluster. See the services and kubectl expose documentation.

Is kube-DNS deprecated?

Previously, the kube-dns project was used. This kube-dns project is now deprecated.

How external DNS works in Kubernetes?

Kubernetes ExternalDNS provides a solution. It sets up DNS records at DNS providers external to Kubernetes such that Kubernetes services are discoverable via the external DNS providers, and allows the controlling of DNS records to be done dynamically, in a DNS provider agnostic way.

What command can be used to access a pod from outside of a Kubernetes cluster?

Pod/Container Port Not Exposed Use kubectl exec -it POD_NAME /bin/SHELL (where shell is often bash if it is installed in the container, or sh otherwise) to get a shell running inside the pod. The shell actually runs inside one of its containers.


1 Answers

At this moment, the reverse DNS lookup or PTR type lookup for a POD IP is possible only in case of pods that are part of a headless service (details: https://github.com/kubernetes/dns/pull/25) but even that has it's limitations. Furthermore, kubernetes has no default per POD dns name at all, even inside kubernetes cluster you are not able to say curl http://<pod_name>. You have services for that. What you are asking for is not really achievable with kubernetes and DNS as it is now. Please do remember that PTR record (IP->name) should go hand in hand with regular record for resolving name (name->IP) which also makes things complicated, and means you can not have just myservice-0dkd0 in there.

That said, you can achieve what you want in a non-dns way here. Assuming you run on linux, you can use /etc/hosts to maintain a name-to-ip and ip-to-name list that is exclusive to that particular system, and does not need to adhere to all the limitations of real DNS.

If, on your mysql host you run something like following say from cron every 1 min, you should get correctly mapped names in your /etc/hosts almost all the time :

NAMESPACE=default
sed -i "/^[0-9\.]*\t[a-zA-Z0-9-]*\.$NAMESPACE/d" /etc/hosts
kubectl get pod --namespace default --field-selector=status.phase==Running -o jsonpath='{range .items[*]}{.status.podIP}{"\t"}{.metadata.name}.{.metadata.namespace}{"\n"}{end}' >> /etc/hosts
like image 51
Radek 'Goblin' Pieczonka Avatar answered Oct 20 '22 08:10

Radek 'Goblin' Pieczonka