Kubernetes - communication between services

3/15/2018

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 ?

-- Sebastian Dziedzic
kubernetes

2 Answers

8/9/2018

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
-- liviaerxin
Source: StackOverflow

3/15/2018

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.

-- dippynark
Source: StackOverflow