Kubernetes - communication between services

匆匆过客 提交于 2021-02-10 18:23:02

问题


I'm currently working on a kubernetes cluster. Cluster is working properly. I need to establish communication between services without using proxy. For example I have services below:

  1. worker
  2. app1
  3. app2
  4. app3

Worker needs to login to app containers directly via SSH and do some commands. In docker-compose file it was easy by using links and then ssh app1, ssh app2. How to do it in Kubernetes ?


回答1:


You'll want to create a headless Service (spec.clusterIP: None) selecting your app Pods. This will create a DNS entry (something like my-svc.my-namespace.svc.cluster.local) that will resolve to the set of IPs of the Pods selected by your Service. You can then loop through the returned list of Pod IPs and ssh into each.

More details can be found here.




回答2:


There are pseudocode,

kind: Service
apiVersion: v1
metadata:
  name: worker
  labels:
    app: worker
spec:
  selector:
    app: worker
  ports:
  - protocol: TCP
    port: 22
    targetPort: 22
  type: NodePort 
---
kind: Service
apiVersion: v1
metadata:
  name: app1
  labels:
    app: app1
spec:
  selector:
    app: app1
  ports:
  - protocol: TCP
    port: 22
    targetPort: 22
  type: ClusterIP 
---
kind: Service
apiVersion: v1
metadata:
  name: app2
  labels:
    app: app2
spec:
  selector:
    app: app2
  ports:
  - protocol: TCP
    port: 22
    targetPort: 22
  type: ClusterIP 

Then, on worker

ssh app1
ssh app2


来源:https://stackoverflow.com/questions/49294895/kubernetes-communication-between-services

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!