简体   繁体   English

Kubernetes服务DNS解析返回错误的IP

[英]Kubernetes service dns resolution returning wrong IP

I have a simple MYSQL pod sitting behind a MYSQL service. 我在MYSQL服务后面有一个简单的MYSQL Pod。

Additionally I have another pod that is running a python process that is trying to connect to the MYSQL pod. 另外,我还有另一个正在运行python进程的Pod,该进程试图连接到MYSQL Pod。

If I try connecting to the IP address of the MYSQL pod manually from the python pod, everything is A-OK. 如果我尝试从python容器手动连接到MYSQL容器的IP地址,则一切正常。 However if I try connecting to the MYSQL service then I get an error that I can't connect to MYSQL. 但是,如果尝试连接到MYSQL 服务,则会收到无法连接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. 如果我连接到我的python容器并对mysqlservice进行nslookup,那么我实际上得到了错误的 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. 如您所见,mysqllook的IP为172.17.0.4,而nslookup mysqlservice的IP为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. 我对kubernetes相当陌生,但是在这个问题上,我已经投入了数小时的时间,而且我似乎无法理解我做错了什么。

So this was the exact correct behavior but I just misconfigured my pods. 所以这是完全正确的行为,但是我只是配置了错误的Pod。

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. kubernetes服务中定义的选择器必须与您要服务的Pod的标签匹配。 IE) In my MySqlService.yaml file I have the name selector for "mysqlpod": IE)在我的MySqlService.yaml文件中,我具有“ 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. 因此,在MySqlPod.yaml文件中,我需要一个完全匹配的标签。

kind: Pod
apiVersion: v1
metadata:
  name: mysqlpod
  labels:
    name: mysqlpod
spec:
  ...

For anyone coming again here, please check @gnicholas answer, but also make sure that clusterIP: None is correctly set. 对于再次来到这里的人,请检查@gnicholas答案,但还要确保clusterIP: None正确设置。

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. 我碰巧缩进了clusterIP: None .yml文件中clusterIP: None太多缩进,并且.yml忽略了该命令,因此clusterIP被错误地分配,从而导致了错误的IP问题。

Be aware that the validation won't throw any error, but will silently ignore it. 请注意,验证不会引发任何错误,但会默默地忽略它。

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

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