Link one service to another service in Kubernetes

11/30/2017

I have individual kubernetes containers up and running. Can someone please help me how to link them? I have worked on docker compose and there it works like this:

redis:
    image: redis
    hostname: redis
    expose:
      - "6379"

  nginx:
    image: xxx.xx.xx.xx:5000/nginx:0.1
    expose:
      - "8080"
    ports:
      - "8080:8080"
    depends_on:
      - active-webserver
      - passive-webserver

  active-webserver:
    image: xxx.xx.xx.xx:5000/webserver:0.1
    hostname: active-webserver
    depends_on:
      - redis
    links:
      - redis
    expose:
      - "8081"
    ports:
      - "8090:8081"

  passive-webserver:
    image: xxx.xx.xx.xx:5000/webserver:0.1
    hostname: passive-webserver
    depends_on:
      - redis
      - kafka
    links:
      - redis
      - kafka
    expose:
      - "8081"
    ports:
      - "8091:8081"

But when coming to kubernetes, I have no clue how to proceed. I have service and deployment files created after using kompose but it actually broke the links between the code.

Service file:

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert -f docker-compose.yaml
    kompose.version: 1.5.0 (999278f)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  ports:
  - name: "8080"
    port: 8080
    targetPort: 8080
  selector:
    io.kompose.service: nginx
status:
  loadBalancer: {}

And my deployment file is:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert -f docker-compose.yaml
    kompose.version: 1.5.0 (999278f)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: nginx
    spec:
      containers:
      - image: xxx.xx.xx.xx:5000/nginx:0.1
        name: nginx
        ports:
        - containerPort: 8080
        resources: {}
      restartPolicy: Always
status: {}
-- Akhil Sharma
docker-compose
kubernetes

1 Answer

11/30/2017

It depends.

In kubernetes the atomic unit of container scheduling is a Pod, a pod can contain multiple containers but the pod is your unit of scaling. So if the containers are linked and you always want them to scale them together then put them all in the same pod.

If you don't, if you might want more of one container than another or want to scale them independently then the best you can do is one or a few of the following to achieve some kind of order.

Create a namespace to put your whole "stack" in. Have a deployment(or whatever) for each group of containers you might want to scale independently, but keep them together in one namespace. That way your internal dns names (if you are using kubedns) will be, say, redis.mystack.svc.cluster.local (where mystack is the name of your namespace and redis is the name of your service in front of, in this case, your redis pods).

Use labels to tie the different portions of your app together. Labels are a great way of organizing you stack. For instance give each resource a label of "app: mystack". Then you can do things like kubectl get pods -l app=mystack

Use a tool like helm ( https://github.com/kubernetes/helm ) it has a concept of grouping resources together into one "application".

Any of these can be used together without issue.

-- Brett Wagner
Source: StackOverflow