Search code examples
dockerkubernetesenvironment-variablesload-balancingmicroservices

Kubernetes and Docker: how to let two service to communicate correctly


I have two Java microservices (caller.jar which calls called.jar)

We can set the caller service http port through an env-var CALLERPORT and the address of called service through an env-var CALLEDADDRESS. So caller uses two env-var.

We must set also called service env-var CALLEDPORT in order to set the specific http port on which called service is listening for http requests.

I don't know exactly how to simply expose these variables from a Dockerfile, in order to set them using Kubernetes.

Here is how I made the two Dockerfiles:

Dockerfile of caller

FROM openjdk:8-jdk-alpine

# ENV CALLERPORT     (it's own port)
# ENV CALLEDADDRESS  (the other service address)

ADD caller.jar /

CMD ["java", "-jar", "caller.jar"]

Dockerfile of called

FROM openjdk:8-jdk-alpine

# ENV CALLEDPORT (it's own port)

ADD called.jar /

CMD ["java", "-jar", "called.jar"]

With these I've made two Docker images:

  • myaccount/caller
  • myaccount/called

Then I've made the two deployments.yaml in order to let K8s deploy (on minikube) the two microservices using replicas and loadbalancers.

deployment-caller.yaml

apiVersion: v1
kind: Service              
metadata:
  name: caller-loadbalancer
spec:
  type: LoadBalancer       
  ports:
  - port: 8080               
    targetPort: 8080        
  selector:            
    app: caller    
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: caller
  labels:
    app: caller
spec:
  replicas: 2                                             
  minReadySeconds: 15
  strategy:
    type: RollingUpdate                                   
    rollingUpdate: 
      maxUnavailable: 1                                   
      maxSurge: 1                                         
  selector:
    matchLabels:
      app: caller
      tier: caller
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: caller
        tier: caller
    spec:
      containers:
      - image: myaccount/caller
        name: caller
        env:
        - name: CALLERPORT
          value: "8080"
        - name: CALLEDADDRESS
          value: called-loadbalancer  # WHAT TO PUT HERE?!
        ports:
        - containerPort: 8080
          name: caller

And deployment-called.yaml

apiVersion: v1
kind: Service              
metadata:
  name: called-loadbalancer
spec:
  type: LoadBalancer       
  ports:
  - port: 8081               
    targetPort: 8081        
  selector:            
    app: called    
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: called
  labels:
    app: called
spec:
  replicas: 2                                             
  minReadySeconds: 15
  strategy:
    type: RollingUpdate                                   
    rollingUpdate: 
      maxUnavailable: 1                                   
      maxSurge: 1                                         
  selector:
    matchLabels:
      app: called
      tier: called
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: called
        tier: called
    spec:
      containers:
      - image: myaccount/called
        name: called
        env:
        - name: CALLEDPORT
          value: "8081"
        ports:
        - containerPort: 8081
          name: called

IMPORTANT: The single services work well if called singularly (such as calling an healthcheck endpoint) but, when calling the endpoint which involves the communication between the two services, then there is this error:

java.net.UnknownHostException: called

The pods are correctly running and active, but i guess the problem is the part of deployment.yaml in which I must define how to find the pointed service, so here:

spec:
  containers:
    - image: myaccount/caller
      name: caller
      env:
        - name: CALLERPORT
          value: "8080"
        - name: CALLEDADDRESS
          value: called-loadbalancer  # WHAT TO PUT HERE?!
        ports:
        - containerPort: 8080
          name: caller

Neither

called

nor

called-loadbalancer

nor

http://caller 


kubectl get pods,svc -o wide

NAME                          READY     STATUS    RESTARTS   AGE       IP           NODE       NOMINATED NODE   READINESS GATES
pod/called-855cc4d89b-4gf97   1/1       Running   0          3m23s     172.17.0.4   minikube   <none>           <none>
pod/called-855cc4d89b-6268l   1/1       Running   0          3m23s     172.17.0.5   minikube   <none>           <none>
pod/caller-696956867b-9n7zc   1/1       Running   0          106s      172.17.0.6   minikube   <none>           <none>
pod/caller-696956867b-djwsn   1/1       Running   0          106s      172.17.0.7   minikube   <none>           <none>

NAME                          TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE       SELECTOR
service/called-loadbalancer   LoadBalancer   10.99.14.91    <pending>     8081:30161/TCP   171m      app=called
service/caller-loadbalancer   LoadBalancer   10.107.9.108   <pending>     8080:30078/TCP   65m       app=caller
service/kubernetes            ClusterIP      10.96.0.1      <none>        443/TCP          177m      <none>

works if put in that line of the deployment.yaml. So what to put in this line?


Solution

  • To access services inside Kubernetes you should use this DNS: