首页 > 解决方案 > Kubernetes:无法在两个服务内通信(不同的 pod,相同的命名空间)

问题描述

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

部署后.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

部署后2.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

当我尝试从 1 个容器 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 条目中,任何指针?

标签: kubernetesminikubekube-dns

解决方案


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

您应该能够 ping 一个 pod,但不能 ping 一个服务。

您可以使用curlwget

例如wget -qO- POD_IP:80

或者你可以试试

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

推荐阅读