Kubernetes - First Microservice (step  by step)

Kubernetes - First Microservice (step by step)

The Project’s Structure:

No alt text provided for this image


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:

No alt text provided for this image
Hassan Talaat

Solution Architect | Java Expert | Bigdata | Cloud(AWS) | Business Analysis

2 年

Excellent Ahmed Thanks

回复
Omar Eldawy

Devops Senior Principal Engineer

2 年

Very good Ahmed????????

要查看或添加评论,请登录

Ahmed El-Sayed的更多文章

  • Open System Architecture

    Open System Architecture

    "Open System Architecture" typically refers to the design and implementation of systems using open standards, open…

  • ChatGPT: Revolutionizing Conversational AI

    ChatGPT: Revolutionizing Conversational AI

    Artificial intelligence (AI) has come a long way in recent years, and one of the most exciting developments in this…

  • Togaf 9.2 Level 1 (Part 1)

    Togaf 9.2 Level 1 (Part 1)

    efore we go in details , we have to know how can we read the open group standards document, you should download the…

    1 条评论
  • Kafka vs RabbitMQ

    Kafka vs RabbitMQ

    What’s the Difference Between a Message Broker and a Publish/Subscribe (Pub/Sub) Messaging System? Message brokers are…

  • What is the strangler pattern and how does it work?

    What is the strangler pattern and how does it work?

    What is the strangler pattern? Picture a motorcycle that works, but could stand to undergo extensive overhauls that…

  • MIGRATING FROM MONOLITH TO MICROSERVICES: STRATEGY & STEP-BY-STEP GUIDE

    MIGRATING FROM MONOLITH TO MICROSERVICES: STRATEGY & STEP-BY-STEP GUIDE

    Migrating from monolith to microservices is less costly and risky than redeveloping an entire system from scratch. But…

    1 条评论
  • Migrate a monolith application to microservices using DDD

    Migrate a monolith application to microservices using DDD

    A monolithic application is typically an application system in which all of the relevant modules are packaged together…

    1 条评论
  • Migrate From Monolithic To Microservices Using DDD Pattern

    Migrate From Monolithic To Microservices Using DDD Pattern

    The general migration approach has three steps: Stop adding functionality to the monolithic application Split the…

  • Migrate From Monolithic To Microservices Using Strangler Pattern

    Migrate From Monolithic To Microservices Using Strangler Pattern

    There are three steps to transition from a monolithic application to microservices by implementing the Strangler…

  • GraalVM

    GraalVM

    GraalVM has caused a revolution in Java development since it launched three years ago. One of the most discussed…

社区洞察

其他会员也浏览了