Ethickfox kb page with all notes
Install and set up Kubernetes on your local machine or cloud platform. If you use GCP, you can create Kubernetes cluster using Google Kubernetes Engine.
Create a new Spring Boot project using any IDE of your choice or use the Spring Initializr website (https://start.spring.io/) to generate a project template.
Add the necessary dependencies in your **pom.xml** or **build.gradle** file. These dependencies should include the Spring Boot Starter Web and the Kubernetes Java client libraries.
Create a Dockerfile in the root directory of your project that specifies how to build your application image. Here is an example Dockerfile for a Spring Boot application:
FROM openjdk:11-jre-slim
WORKDIR /app
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
This Dockerfile uses the official OpenJDK 11 JRE image as the base image, sets the working directory to **/app**, copies the JAR file generated by the Maven build into the container, and specifies the entry point command to run the JAR file.
Build a Docker image of your application using the Dockerfile created in step 4. Run the following command in your project directory:
docker build -t <image-name>:<tag> .
This command builds a Docker image tagged with a name and a version specified in **<image-name>** and **<tag>**, respectively.
Push the Docker image to a container registry of your choice. For example, to push the image to Docker Hub, run the following command:
docker push <image-name>:<tag>
Create a Kubernetes deployment YAML file that specifies how to deploy your application as a containerized workload. Here is an example deployment YAML file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: <deployment-name>
spec:
replicas: 1
selector:
matchLabels:
app: <app-name>
template:
metadata:
labels:
app: <app-name>
spec:
containers:
- name: <container-name>
image: <image-name>:<tag>
ports:
- containerPort: 8080
This YAML file specifies a deployment with one replica, a container image specified by **<image-name>** and **<tag>**, and a container port set to 8080.
Create a Kubernetes service YAML file that specifies how to expose your application as a network service. Here is an example service YAML file:
apiVersion: v1
kind: Service
metadata:
name: <service-name>
spec:
selector:
app: <app-name>
ports:
- protocol: TCP
port: 80
targetPort: 8080
This YAML file specifies a network service with a name of **<service-name>**, which routes traffic to the deployment selector with the **app** label set to **<app-name>**. The port configuration maps the container port of 8080 to the service port of 80.
Apply the deployment and service YAML files to your Kubernetes cluster using the **kubectl apply** command. Run the following commands:
kubectl apply -f <deployment-file>.yaml
kubectl apply -f <service-file>.yaml
These commands create the Kubernetes deployment and service objects in the cluster.
Verify that your application is running by accessing the service URL in a web browser or by using the **curl** command:
curl http://<