Kubernetes - First Microservice (step by step)
The Project’s Structure:
Our? DemoApplication.java is the entry point of the application:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The code above contains the minimal lines needed to create a Spring Boot application.
The?HelloWorldCtrl.java contains a simple controller that maps the root path?(“/”) and returns a greeting String:
package com.example.demo;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloWorldCtrl {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
Creating K8s Resources
In order to create a K8s deployment, we’ll need a Docker image. Let’s add the following lines to our?Dockerfile :
FROM openjdk:12
EXPOSE 8080
COPY target/*.jar /opt/app.jar
WORKDIR /opt
ENTRYPOINT exec java $JAVA_OPTS -jar app.jar
before building the image, we have to build the application using mvn, then we can build the image and push to the hub:
领英推荐
docker build -t hamoshka/demo .
docker push hamoshka/demo
The K8s resources which we will use in our example are a?deployment?and a?service:
i recommend to create directory /kube under the project to put all k8s resources together.
our first resource we will use is the demo-deployment.yaml
apiVersion: apps/v
kind: Deployment
metadata:
? name: demo-deployment
? labels:
? ? app: demo
spec:
? replicas: 3
? selector:
? ? matchLabels:
? ? ? app: demo
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: demo
? ? spec:
? ? ? containers:
? ? ? - name: demo
? ? ? ? image: hamoshka/demo
? ? ? ? ports:
? ? ? ? - containerPort: 8080
The deployment defines 3 replicas of the pod that will be running the container that’s built from the image specified in the image attribute (hamoshka/demo).
Creating the resources in your cluster:
kubectl create -f /kube/demo-deployment.yaml
our second resource we will use is the service demo-loadbalancer.yaml
we will use the service type loadbalancer to make our pods accessible from outside the cluster.
apiVersion: v1
kind: Service
metadata:
? name: demo
? namespace: default
spec:
? allocateLoadBalancerNodePorts: true
? externalTrafficPolicy: Cluster
? internalTrafficPolicy: Cluster
? ipFamilies:
? - IPv4
? ipFamilyPolicy: SingleStack
? ports:
? - nodePort:?
? ? port: 80
? ? protocol: TCP
? ? targetPort: 8080
? selector:
? ? app: demo
? sessionAffinity: None
? type: LoadBalancer
status:
? loadBalancer:
? ? ingress:
? ? - hostname: localhost
then we have to create the resource using kubectl .
kubectl create -f /kube/demo-loadbalancer.yaml
now we are done, just test it from your browser:
Solution Architect | Java Expert | Bigdata | Cloud(AWS) | Business Analysis
2 年Excellent Ahmed Thanks
Devops Senior Principal Engineer
2 年Very good Ahmed????????