Kubernetes service dns resolution returning wrong IP

1/20/2017

I have a simple MYSQL pod sitting behind a MYSQL service.

Additionally I have another pod that is running a python process that is trying to connect to the MYSQL pod.

If I try connecting to the IP address of the MYSQL pod manually from the python pod, everything is A-OK. However if I try connecting to the MYSQL service then I get an error that I can't connect to MYSQL.

grant@grant-Latitude-E7450:~/k8s/objects$ kubectl describe pod mysqlpod 
Name:       mysqlpod
Namespace:  default
Node:       minikube/192.168.99.100
Start Time: Fri, 20 Jan 2017 11:10:50 -0600
Labels:     <none>
Status:     Running
IP:     172.17.0.4
Controllers:    <none>

grant@grant-Latitude-E7450:~/k8s/objects$ kubectl describe service mysqlservice
Name:           mysqlservice
Namespace:      default
Labels:         <none>
Selector:       db=mysqllike
Type:           ClusterIP
IP:         None
Port:           <unset> 3306/TCP
Endpoints:      172.17.0.5:3306
Session Affinity:   None
No events.

grant@grant-Latitude-E7450:~/k8s/objects$ kubectl describe pod basic-python-model
Name:       basic-python-model
Namespace:  default
Node:       minikube/192.168.99.100
Start Time: Fri, 20 Jan 2017 12:01:50 -0600
Labels:     db=mysqllike
Status:     Running
IP:     172.17.0.5
Controllers:    <none>

If I attach to my python container and do an nslookup of the mysqlservice, then I'm actually getting the wrong IP. As you saw above the IP of the mysqlpod is 172.17.0.4 while nslookup mysqlservice is resolving to 172.17.0.5.

grant@grant-Latitude-E7450:~/k8s/objects$ k8s exec -it basic-python-model bash
[root@basic-python-model /]# nslookup mysqlservice                    
Server:     10.0.0.10
Address:    10.0.0.10#53

Name:   mysqlservice.default.svc.cluster.local
Address: 172.17.0.5

I'm fairly new to kubernetes, but I've been banging my head on this issue for a few hours and I can't seem to understand what I'm doing wrong.

-- gnicholas
dns
docker
kubernetes
mysql
service

2 Answers

1/20/2017

So this was the exact correct behavior but I just misconfigured my pods.

For future people who are stuck:

The selector defined in a kubernetes service must match the label of the pod(s) you wish to serve. IE) In my MySqlService.yaml file I have the name selector for "mysqlpod":

apiVersion: v1
kind: Service
metadata:
  name: mysqlservice
spec:
  clusterIP: None 
  ports:
    - port: 3306
      targetPort: 3306
  selector:
    name: mysqlpod 

Thus in my MySqlPod.yaml file I need an exactly matching label.

kind: Pod
apiVersion: v1
metadata:
  name: mysqlpod
  labels:
    name: mysqlpod
spec:
  ...
-- gnicholas
Source: StackOverflow

2/7/2019

For anyone coming again here, please check @gnicholas answer, but also make sure that clusterIP: None is correctly set.

I happened to indent clusterIP: None too much in the .yml file and the command was ignored by Kubernetes therefore clusterIP was mistakenly assigned causing the wrong IP issue.

Be aware that the validation won't throw any error, but will silently ignore it.

-- a.barbieri
Source: StackOverflow