Spring Boot On Azure AKS

Spring Boot On Azure AKS

Create AKS cluster in Azure

search kubernetes services on azure portal , click on create option select kubernetes cluster

create resource group if not existed previously and enter kubernetes cluster name.

cluster present configuration select Dev/Test is best for testing existing or new workloads.

select region based on your location. AKS pricing should be free.

click on next for node pool configuration.

Node Pool configuration

The already existing node pool for control plan which is managed by azure

we have to create new node pool for worker node.

enter the node pool name and node size is virtual machine for testing we can use D2s V3. enter add button for creating aks cluster.

Azure CLI

Connecting the AKS cluster we can use azure cli or powershell.

now i’m using azure cli for connecting AKS cluster.

search tenant in azure portal ,in tenant properties you will find tenant id.

az login --tenant <Tenant ID>
az login

Enter the login commands you will be redirected to azure login pop for authentication.

if you want to know the login details you can use these command

az ad signed-in-user show

connecting the azure aks cluster

az aks get-credentials --resource-group <resource-groupname> --name <cluster-name>

Spring Boot Docker Image

FROM openjdk:8
ADD target/Application-Name-0.0.1-SNAPSHOT.jar Application-Name-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar","Application-Name-0.0.1-SNAPSHOT.jar"]

we are using openjdk:8 image for existing project we are coping the jar file which is preset in target folder .exposing the port of image to 8080 ,on entry point mean when ever the image run will we run these command for running the spring boot application.

If jar file is not present in location of your project run mvn clean and install command.

Push Image to Docker Hub

login to docker

docker login

build docker image

docker build -t <DOCKER_USERNAME>/application_name .

push docker image to docker hub

docker push <DOCKER_USERNAME>/application_name

Create Kubernetes Deployment

apiVersion: apps/v1 #kubernetes api version 
kind: Deployment   
metadata:
  name: wserver  #deployment name
spec:
  replicas: 1  # no of replica of application
  selector:
    matchLabels:
      app: wserver  #deployment manage pods with label app:server
  template:
    metadata:
      labels:
        app: wserver # which pods to manage 
    spec:
      containers:
        - name: app
          image: <DOCKER_USERNAME>/application_name:latest
          ports:
            - containerPort: 8080  #container port expose 8080

Deployment defines the how application should be deployed ,scaled and updated in kubernetes cluster. its manages the set of pods to run on application workload.

Kubectl apply -f deployment.yaml

Create Kubernetes service

apiVersion: v1
kind: Service
metadata:
  name: wserver
spec:
  selector:
    app: wserver
  ports:
    - port: 8080 #port exposed to outside world
      targetPort: 8080  #container port
  type: LoadBalancer

Load Balancer manages the traffic distribution across pods instance based on load.

users send request to load balancer with port 8080 which redirects to pod then traffic then forwarded to target port 8080 on the container which is inside pod.

if we want to run the service or container inside the cluster not expose it outside then user type as ClusterIP. which is accessible inside the cluster only.

kubectl apply -f service.yaml
kubectl get svc <service-name>

The details of service will be displayed. to access the service we have to use external Ip.

Accessing the external Ip of wserver service through browser.