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.
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
Looks like you need to make changes at your mysql server level. Checkout this documentation link for detailed info for skipping name resolution to IP addresses.
By default, MySQL makes a DNS lookup to resolve the client hostnames. You can disable this lookup with the option skip_name_resolve.
mysql> show variables like "skip_name_resolve"; // will show the current state of the variable.