Connect frontend and backend pod using Kubernetes services

Connect frontend and backend pod using Kubernetes services

In this article, we'll create the front-end and back-end spring boot applications, Docker images to deploy both applications to the Kubernetes cluster and connect them with the help of Kubernetes services.

For security reasons, in most of the microservice-based application architecture, we have both frontend and backend microservices. Frontend service interacts with the user. It does not have direct access to the actual database. It has to access data through backend service only. Backend service deals with actual data, so we do not want it to expose to end-users. There are different ways we can add authentication on the backend services but in Kubernetes, we can create a special service so that it can be accessed within-cluster only.

Here, we'll create two Kubernetes services that are linked with a specific Kubernetes pod, one for the front-end and one for the back-end. The front-end one will be accessible to the end-user, but the Back-end one will be only accessible within the cluster only.

In this article, I have added a core code snippet and a few commands. For detailed reference, refer BackEnd-GITHUB and FrontEnd-GITHUB

If you are new to Kubernetes, then refer to HelloWorld in Kubernetes for initial setup and basic application.

Prerequisites

  1. JDK
  2. Maven
  3. Docker
  4. Minikube or Kubernetes cluster
  5. Basic knowledge of Java, Spring boot, docker, and Kubernetes

BackEnd Application

  • Create a spring boot application and add the below rest controller. We can use this controller to interact with databases or deal with any kind of sensitive information. Here, just to keep it simple, we'll print messages only.
@RestController
public class BackendController {
    @RequestMapping(value = "backend", method= RequestMethod.GET)
    public String backend() {
        return "Data from the backend application";
    }
}
  • Create an executable JAR and build a docker image from the below commands
mvn clean install
docker build -t backend:1.0.0 .
  • Create a backend pod from the below file and command
apiVersion: v1
kind: Pod
metadata:
  name: backend
  labels:
    name: backend
spec:
  containers:
    - name: backend
      image: backend:1.0.0
      ports:
        - containerPort: 8081
kubectl apply -f pod.yaml
  • Create a backend service from the below file and command. Here we are creating a Kubernetes service of type ClusterIP, so we can access it within the cluster only. Only a pod or any container that is running on the same cluster can access this service.
apiVersion: v1
kind: Service
metadata:
  labels:
    name: backend
  name: backend-svc
spec:
  selector:
    name: backend
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
  type: ClusterIP
kubectl apply -f service.yaml

FrontEnd Application

  • Create a spring boot application and add the below rest controller. Here we are trying to access the backend service with the help of the rest template. As this pod will run on the same Kubernetes cluster so it will access back-end service. This application will be exposed to end-users.
@RestController
public class FrontEndController {
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping(value = "frontend", method = RequestMethod.GET)
    public String frontend() {
        String url = "http://backend-svc:8081/backend";
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody();
        } else {
            return "Error occurred while fetching data from backend service";
        }
    }
}
  • Create an executable JAR and build a docker image from the below commands
mvn clean install
docker build -t frontend:1.0.0 .
  • Create a frontend pod from the below file and command
apiVersion: v1
kind: Pod
metadata:
  name: frontend
  labels:
    name: frontend
spec:
  containers:
    - name: frontend
      image: frontend:1.0.0
      ports:
        - containerPort: 8080
kubectl apply -f pod.yaml
  • Create a frontend service from the below file and command. Here type of the service is LoadBalancer so we can access this from outside the Kubernetes cluster.
apiVersion: v1
kind: Service
metadata:
  labels:
    name: frontend
  name: frontend-svc
spec:
  selector:
    name: frontend
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  type: LoadBalancer
kubectl apply -f service.yaml
  • List all the pods.

pod-list-service-conn.png

  • List all the services. As shown below in the image, the only exposed service is the frontend.

service-conn-list.png

  • Test backend service from a browser. We won't be able to access it because its cluster type and we can only access it within the cluster only. (localhost:8081/backend)

  • Test frontend service from browser - localhost:8080/frontend

service-conn.png