Kubernetes: communication between containers located in different pods

3/21/2019

I have two microservices that communicate through http protocol.

  • AC6K: C# microservice that gets data from an Atlas Copco 6000 device.

  • LocalWriter: python aplication that gets data from AC6K and stores the info in a data base

I have tested it in Windows and Linux environments and it works fine. When I contenerize each microservice and make the deployment, there is no communication. Please find enclosed herewith the corresponding docker & yaml files used to containerize and deploy the application

ac6k docker file:

FROM microsoft/aspnetcore-build

EXPOSE 5010

WORKDIR /app

COPY . .

RUN dotnet restore

ENTRYPOINT ["dotnet", "ac6kcore.dll"]




-  ac6kUp.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: ac6kcore
      labels:
        run: ac6kcore  
    spec:
      type: NodePort
      ports: 
      - port: 5010
        name: ac6kcore
        targetPort: 5010
        nodePort: 32766
        protocol: TCP
      selector:
        run: ac6kcore
    ---
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: ac6kcore
    spec:
      selector:
        matchLabels:
          run: ac6kcore
      replicas: 1
      template:
        metadata:
          labels:
            run: ac6kcore
        spec:
          hostNetwork: true
          containers:
          - image: afierro/ac6kcore:lw
            name: ac6kcore
            ports:
            - containerPort: 5010
          restartPolicy: Always

local writer docker file:

FROM python:3.6

RUN mkdir -p /mongodbapp

WORKDIR /mongodbapp

COPY requirements.txt /mongodbapp

RUN pip install --no-cache-dir -r requirements.txt

ADD . /mongodbapp

EXPOSE 9090

CMD ["python", "runapp.py"]

- LocalWriter.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
  name: localwriter
  labels:
    app: localwriter
spec:
  type: NodePort
  ports:
  - port: 9090
    name: localwriter
    targetPort: 9090
    nodePort: 32756
    protocol: TCP
  selector:
    app: localwriter
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: localwriter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: localwriter
  template:
    metadata:
      labels:
        app: localwriter
    spec:
      containers:
      - name: flasknode
        image: afierro/localwriter:v1
        imagePullPolicy: Always
        ports:
        - containerPort: 9090

Thanks in advance

-- Alvaro Fierro Clavero
containers
kubernetes
microservices

2 Answers

3/21/2019

You use service to communicate a pod in one deployment to a pod in another deployment.

Check services here:

https://kubernetes.io/docs/concepts/services-networking/service/

Make sure you have the right selector in the service.

You can use the env vars that are inside the pod to call the other service. Kubernetes starts the pods with the service ip and port as env vars, to check this you can ssh in a pod and use the command printenv.

-- Leandro Donizetti Soares
Source: StackOverflow

3/22/2019

According to Leandro Donizetti Soares's recommendation, I have entered the following commands to see the environment variables, and the results seem to me redundant.

kubectl get pods

NAME                           READY   STATUS    RESTARTS   AGE
ac6kcore-77bc4c4987-dxn29      1/1     Running   0          14m
localwriter-55467c5495-px8m4   1/1     Running   0          14m
mongo                          1/1     Running   0          14m

kubectl exec ac6kcore-77bc4c4987-dxn29 -- printenv | grep SERVICE   

AC6KCORE_SERVICE_PORT_AC6KCORE=5010
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT_HTTPS=443
AC6KCORE_SERVICE_PORT=5010
AC6KCORE_SERVICE_HOST=10.107.208.212
LOCALWRITER_SERVICE_HOST=10.100.103.114
LOCALWRITER_SERVICE_PORT=9090
LOCALWRITER_SERVICE_PORT_LOCALWRITER=9090
KUBERNETES_SERVICE_PORT=443

kubectl exec localwriter-55467c5495-px8m4 -- printenv | grep SERVICE    

KUBERNETES_SERVICE_HOST=10.96.0.1
LOCALWRITER_SERVICE_PORT_LOCALWRITER=9090
AC6KCORE_SERVICE_PORT_AC6KCORE=5010
KUBERNETES_SERVICE_PORT=443
AC6KCORE_SERVICE_HOST=10.107.208.212
LOCALWRITER_SERVICE_PORT=9090
KUBERNETES_SERVICE_PORT_HTTPS=443
AC6KCORE_SERVICE_PORT=5010
LOCALWRITER_SERVICE_HOST=10.100.103.114
-- Alvaro Fierro Clavero
Source: StackOverflow