Linking containers based on pod names in kubernetes

1/30/2020

I tried to make different pods each of them runs its own container, I tried to follow the same instructions of linking two docker containers:

docker run -d -e POSTGRES_USER=name -e POSTGRES_PASSWORD=pass -e POSTGRES_DB=postgres --name db postgres:10

The other container:

docker run -p port:port --name image-name --link db:db -t image

and the Kubernetes deployments:

DB

apiVersion: apps/v1
kind: Deployment
metadata:
  name: db
  labels:
    app: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
        - name: db
          image: postgres
          ports:
          - containerPort: 5432
          env:
          - name: "POSTGRES_DB"
            value: "postgres"
          - name: "POSTGRES_USERNAME"
            value: "name"
          - name: "POSTGRES_PASSWORD"
            value: "pass"

APP

apiVersion: apps/v1
kind: Deployment
metadata:
  name: APP
  labels:
    app: APP
spec:
  replicas: 1
  selector:
    matchLabels:
      app: APP
  template:
    metadata:
      labels:
        app: APP
    spec:
      containers:
        - name: APP
          image: image
          ports:
          - containerPort: port

Now the question, do I need to create containers in the same pod and name it in a similar name to the docker command(db=db)? or do I need to configure the network correctly so containers can find the hostname?

-- omeraiman
docker
kubernetes

3 Answers

1/30/2020

In Kubernetes, a pod is considered as the smallest unit so it makes more sense to place them in a separate pod so they have their own lifecycle. The way you are doing is definitely correct; to make them work you need to expose the DB so your App can connect to it i.e. to expose DB it as a Service of type ClusterIP and then you can reach it via its service name inside your cluster.

A simple example -

apiVersion: v1
kind: Service
metadata:
  name: "postgres"
  labels:
    app.kubernetes.io/name: postgres
spec:
  type: ClusterIP
  ports:
    - port: 5432
      targetPort: 5432
      protocol: TCP
      name: dbport
  selector:
    app.kubernetes.io/name: postgres
-- Pankaj
Source: StackOverflow

1/30/2020

You need to create a service over the database. The kubernetes docs explain the various options for doing this. Once you have a service, your pod will be able to look up that service by name using the DNS provided by kubernetes to the pod. The service acts as a proxy to the (in this case, database) pod.

-- wobr
Source: StackOverflow

1/30/2020

You have to create a service which points to your pod (deployment). Via service, you can manage traffic across deployments.

For more info you can check this document : https://kubernetes.io/docs/concepts/services-networking/service/

if you are looking for internal dns communication

please check : https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

-- Harsh Manvar
Source: StackOverflow