连接kubernetes集群中的客户端和服务器

时间:2019-03-15 00:28:52

标签: kubernetes

我正在尝试在开发环境中的一个Kubernetes集群(客户端和服务器)中连接2个Pod。

 apiVersion: v1 
 kind: Service 
 metadata:   
   name: client-node-port spec: 
 type: NodePort   
 ports: 
     - port: 4000
       targetPort: 4000
       nodePort: 31515   
 selector: 
     component: nri

服务器将是

apiVersion: v1
kind: Service
metadata:
  name: server-node-port
spec:
  type: NodePort
  ports: 
    - port: 1114
      targetPort: 1114
      nodePort: 30000
  selector: 
    component: nri

我可以成功访问该网页,但是它似乎能够连接到服务器。 (服务器在1114(运行)上运行)和客户端(在4000上运行)

如何连接它们?

1 个答案:

答案 0 :(得分:2)

问题是两个服务的selector相同。当您尝试使用任何一种服务时,它会随机将您路由到“客户端”或“服务器”窗格。

示例:

  1. 您使用nodeport:31515,因此服务为client-node-port

    • 如果该服务将您路由到“客户端”窗格,则端口4000将可用。端口1114不会打开,因为“客户端”窗格未打开。
    • 如果将您路由到Server Pod,则端口4000将不可用,因为Server Pod没有打开该端口。同时,它已打开1114端口,但是此服务未路由到1114端口,因此也将不可用。
  2. 您使用nodeport:30000,因此服务为server-node-port

    • 如果该服务将您路由到客户端吊舱,则端口4000将不可用,因为此服务不会路由到端口4000。同时,端口1114将不可用,因为它在客户端吊舱上未打开。 / li>
    • 如果将您路由到服务器窗格,则端口1114将可用。端口4000不会打开,因为Server pod尚未打开。

要解决该问题,您需要在“客户端”和“服务器”窗格中添加其他标签,例如分别为app: clientapp: server

...
metadata:
  name: client
  labels:
    component: nri
    app: client #Here is the new label
...

---

...
metadata:
  name: server
  labels: 
    component: nri
    app: server #Here is the new label
...

然后,在服务上添加这些标签:

apiVersion: v1 
kind: Service 
metadata:   
  name: client-node-port 
spec: 
  type: NodePort   
  ports: 
    - port: 4000
      targetPort: 4000
      nodePort: 31515   
  selector: 
    component: nri
    app: client #Here is the new label
---
apiVersion: v1
kind: Service
metadata:
  name: server-node-port
spec:
  type: NodePort
  ports: 
    - port: 1114
      targetPort: 1114
      nodePort: 30000
  selector: 
    component: nri
    app: server #Here is the new label
相关问题