Kubernetes:无法在两个服务(不同的Pod,相同的名称空间)内进行通信

时间:2020-07-19 08:10:55

标签: kubernetes minikube kube-dns

我无法在两个服务之间进行通信。

post-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-data-deployment
  labels:
spec:
  replicas: 1
  selector:
   matchLabels:
    app: python-web-selector
    tier: backend
  template:
   metadata:
     labels:
       app: python-web-selector
       tier: backend
   spec:
    containers:
    - name: python-web-pod
      image: sakshiarora2012/python-backend:v10
      ports:
      - containerPort: 5000

post-deployment2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-data-deployment2
  labels:
spec:
  replicas: 1
  selector:
   matchLabels:
    app: python-web-selector2
    tier: backend
  template:
   metadata:
     labels:
       app: python-web-selector2
       tier: backend
   spec:
    containers:
    - name: python-web-pod2
      image: sakshiarora2012/python-backend:v8
      ports:
      - containerPort: 5000

post-service.yml

apiVersion: v1
kind: Service
metadata:
  name: python-data-service
spec:
  selector:
   app: python-web-selector
   tier: backend
  ports:
      - port: 5000
        nodePort: 30400
  type: NodePort

post-service2.yml

apiVersion: v1
kind: Service
metadata:
  name: python-data-service2
spec:
  selector:
   app: python-web-selector2
   tier: backend
  ports:
      - port: 5000
  type: ClusterIP

当我尝试从一个容器向另一个容器ping时,它无法ping

root@python-data-deployment-7bd65dc685-htxmj:/project# ping python-data-service.default.svc.cluster.local
PING python-data-service.default.svc.cluster.local (10.107.11.236) 56(84) bytes of data.
^C
--- python-data-service.default.svc.cluster.local ping statistics ---
7 packets transmitted, 0 received, 100% packet loss, time 139ms

如果我看到dns条目,它就会显示

sakshiarora@Sakshis-MacBook-Pro Student_Registration % kubectl exec -i -t dnsutils -- nslookup  python-data-service 
Server:     10.96.0.10
Address:    10.96.0.10#53

Name:   python-data-service.default.svc.cluster.local
Address: 10.107.11.236

sakshiarora@Sakshis-MacBook-Pro Student_Registration % 
sakshiarora@Sakshis-MacBook-Pro Student_Registration % kubectl exec -i -t dnsutils -- nslookup  python-data-service2
Server:     10.96.0.10
Address:    10.96.0.10#53

Name:   python-data-service2.default.svc.cluster.local
Address: 10.103.97.40


sakshiarora@Sakshis-MacBook-Pro Student_Registration % kubectl get pod -o wide 
NAME                                       READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
dnsutils                                   1/1     Running   0          5m54s   172.17.0.9   minikube   <none>           <none>
python-data-deployment-7bd65dc685-htxmj    1/1     Running   0          47m     172.17.0.6   minikube   <none>           <none>
python-data-deployment2-764744b97d-mc9gm   1/1     Running   0          43m     172.17.0.8   minikube   <none>           <none>
python-db-deployment-d54f6b657-rfs2b       1/1     Running   0          44h     172.17.0.7   minikube   <none>           <none>

sakshiarora@Sakshis-MacBook-Pro Student_Registration % kubectl describe svc python-data-service
Name:                     python-data-service
Namespace:                default
Labels:                   <none>
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"python-data-service","namespace":"default"},"spec":{"ports":[{"no...
Selector:                 app=python-web-selector,tier=backend
Type:                     NodePort
IP:                       10.107.11.236
Port:                     <unset>  5000/TCP
TargetPort:               5000/TCP
NodePort:                 <unset>  30400/TCP
Endpoints:                172.17.0.6:5000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
sakshiarora@Sakshis-MacBook-Pro Student_Registration % kubectl describe svc python-data-service2
Name:              python-data-service2
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"python-data-service2","namespace":"default"},"spec":{"ports":[{"p...
Selector:          app=python-web-selector2,tier=backend
Type:              ClusterIP
IP:                10.103.97.40
Port:              <unset>  5000/TCP
TargetPort:        5000/TCP
Endpoints:         172.17.0.8:5000
Session Affinity:  None
Events:            <none>

sakshiarora @ Sakshis-MacBook-Pro Student_Registration%

我认为,如果在DNS表中显示的范围是172,17.0.X,那么它将起作用,但不确定为什么它不在dns条目中显示,是否有任何指针?

4 个答案:

答案 0 :(得分:1)

Ping无法用于服务ClusterIP地址,因为它们来自由iptables规则创建的虚拟地址,该规则将数据包重定向到端点(pods)。

您应该能够对Pod进行ping操作,但不能对服务进行操作。

您可以使用curlwget

例如wget -qO- POD_IP:80

或者您可以尝试

wget -qO- http://your-service-name:port/yourpath
curl POD_IP:port_number

答案 1 :(得分:1)

如果您想使用python-data-service从集群外部访问NodePort,并且您正在使用minikube,那么应该可以从集群外部的任何地方(例如,从您的系统

如果要在集群中的两个微服务之间进行通信,则只需使用curl $(minikube service python-data-service --url)类型的服务,而不是ClusterIP类型的

要确定是服务问题还是吊舱问题,请直接在NodePort命令中使用PODIP。从curl的输出来看,服务kubectl describe svc python-data-service的Pod IP为python-data-service。因此,尝试172.17.0.6

答案 2 :(得分:1)

为了开始调试您的服务,我建议执行以下步骤:

检查您的服务1是否可以作为Pod访问:

kubectl run test1 -it --rm=true --image=busybox --restart=Never -n default -- wget -O - http://172.17.0.6:5000

检查您的服务2是否可以作为Pod访问:

kubectl run test2 -it --rm=true --image=busybox --restart=Never -n default -- wget -O - 172.17.0.8:5000

然后,检查服务1是否可以使用相应的群集IP,然后使用DNS名称作为服务进行访问:

kubectl run test2 -it --rm=true --image=busybox --restart=Never -n default -- wget -O - 10.107.11.236:5000

kubectl run test2 -it --rm=true --image=busybox --restart=Never -n default -- wget -O - http://python-data-service:5000

然后,使用相应的群集IP和DNS名称,检查您的服务2是否可以作为服务访问:

kubectl run test2 -it --rm=true --image=busybox --restart=Never -n default -- wget -O - 10.103.97.40:5000

kubectl run test2 -it --rm=true --image=busybox --restart=Never -n default -- wget -O - http://python-data-service2:5000

然后,如果需要,请检查您的服务2是否可以通过您的节点端口访问(您需要知道已公开该服务的节点的IP地址,例如在minikube中它应该可以工作:)

wget -O - http://192.168.99.101:30400

我可以建议您从服务清单中同时指定porttargetPort,如您所见

https://canterafonseca.eu/kubernetes/certification/application/developer/cncf/k8s/cloud/native/computing/ckad/deployments/services/preparation-k8s-ckad-exam-part4-services.html#-services

另一方面,如果您只需要向外界公开其中一项服务,则可以创建无头服务(另请参见上面的我的博客文章)。

答案 3 :(得分:1)

尝试使用kubectl logs PODNAME查看日志,以便了解发生了什么。乍一看,我认为您需要公开两种服务的端口:kubectl port-forward yourService PORT:PORT