Kubernetes (Minikube): link between client and server

6/5/2018

I'm running a simple spring microservice project with Minikube. I have two projects: lucky-word-client (on port 8080) and lucky-word-server (on port 8888). But I can't communicate client with server. Infact if lucky-word-client communicates with lucky-word-server, the result is the word "Evviva", else the word is "Default". When I run on terminal: minikube service lucky-client the output is Default, instead of Evviva.

This is the file Dockerfile of lucky-word-server:

FROM frolvlad/alpine-oraclejdk8 

ADD build/libs/common-config-server-0.0.1-SNAPSHOT.jar common-config-server.jar

EXPOSE 8888

ENTRYPOINT ["/usr/bin/java", "-Xmx128m", "-Xms128m"]

CMD ["-jar", "common-config-server.jar"]

This is the file Dockerfile of lucky-word-client:

FROM frolvlad/alpine-oraclejdk8 

ADD build/libs/lucky-word-client-0.0.1-SNAPSHOT.jar lucky-word-client.jar

EXPOSE 8080

ENTRYPOINT ["/usr/bin/java", "-Xmx128m", "-Xms128m"]

CMD ["-jar", "-Dspring.profiles.active=italian", "lucky-word-client.jar"]

This is deployment of lucky-word-server:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lucky-server
spec:
  selector:
      matchLabels:
        app: lucky-server
  replicas: 1
  template:
    metadata:
      labels:
        app: lucky-server
    spec:
      containers:
        - name: lucky-server
          image: lucky-server-img
          imagePullPolicy: Never
          ports:
            - containerPort: 8888

This is the service of lucky-word-server:

kind: Service
apiVersion: v1
metadata:
  name: lucky-server
spec:
  selector:
    app: lucky-server
  ports:
  - protocol: TCP
    port: 8888
  type: NodePort

This is the deployment of lucky-word-client:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lucky-client
spec:
  selector:
      matchLabels:
        app: lucky-client
  replicas: 1
  template:
    metadata:
      labels:
        app: lucky-client
    spec:
      containers:
        - name: lucky-client
          image: lucky-client-img
          imagePullPolicy: Never
          ports:
            - containerPort: 8080

This is the service of lucky-word-client:

kind: Service
apiVersion: v1
metadata:
  name: lucky-client
spec:
  selector:
    app: lucky-client
  ports:
  - protocol: TCP
    port: 8080
  type: NodePort
-- Danny
kubernetes
microservices
minikube

2 Answers

6/5/2018

Your services are sending the requests to port 80 now. You need to specify parameter targetPort. Should look like this:

kind: Service
apiVersion: v1
metadata:
  name: lucky-server
spec:
  selector:
    app: lucky-server
  ports:
  - protocol: TCP
    targetPort: 8888  #this is your container port. where to send the requests
    port: 8888   #this is the service port. it is running on svc-ip:8888
  type: NodePort

You should do the same with the other service. Also check the service port. Now it is on 8080 and 8888. You might be hitting them on port 80.

There might be more issues, but for now, these for sure cause a problem.

-- suren
Source: StackOverflow

6/5/2018

As @suren stated you should specify the target port in the service definition.

And you should change the endpoint URL of the server that client calls to reflect minikube_host_ip. There are couple of ways to achieve that. The naive method would be as follows.

Change your Kubernetes service for the server to have a static Nodeport as follows:

kind: Service
apiVersion: v1
metadata:
  name: lucky-server
spec:
  selector:
    app: lucky-server
  ports:
  - protocol: TCP
    port: 8080
    nodePort: 32002
  type: NodePort

And in your client code just change the endpoint of the server as follows: http://{minikube_host_ip}:32002 Replace your {minikube_host_ip} with the ip address of the minikube host here.

But if you don't want to hard code the minikube ip you can inject it as an environment variable in your Kuberenetes deployment script. And that environment variable should be captured in your docker file.

-- Insightcoder
Source: StackOverflow