Deployment Object
This is one of the key objects in Kubernetes. Because our ultimate goal is to create pod objects in K8s. But we will not create pods directly, rather we will create deployment objects and provide instructions to create the number of pods (and containers) that need to be created.
If we look into the official documentation, we will find some guidelines on how to write deployment configuration. Here on the right, we have a deployment object to create a deployment of a simple spring boot application container. You can prepare your docker image and replace the shikhorroy/simple-spring-boot-app:0.0.1 image under containers.
Now run the following command to create a deployment:
kubectl apply -f deployment.yaml
Service Object
If we want to access the deployed application (in a pod) outside the Kubernetes cluster we need a service.
Here is the Service Object official documentation.
On the right side, I created one service object. The points that need to be cared for are the selector in service, and the matcheLabels in deployment should be matched with the label in template metadata.
Run the following command to create a service:
kubectl apply -f service.yaml
To access the application from the browser, you can run the following if you are using minikube for the local environment:
minikube service myapp-service
This will open the accessible URL in the browser and also show output like (for your case this will vary) this:
|-----------|---------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|---------------|-------------|---------------------------|
| default | myapp-service | 8080 | http://192.168.49.2:30193 |
|-----------|---------------|-------------|---------------------------|
* Starting tunnel for service myapp-service.
|-----------|---------------|-------------|------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|---------------|-------------|------------------------|
| default | myapp-service | | http://127.0.0.1:63187 |
|-----------|---------------|-------------|------------------------|
So manually we can hit: http://127.0.0.1:63187 to access the deployed application.
K8s Declarative Object Configurations
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp-java
image: shikhorroy/simple-spring-boot-app:0.0.1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
service.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 8080
targetPort: 8080
type: LoadBalancer