Summary of MicroK8s Workflow and Spring Boot Deployment
1. Setting Up MicroK8s and Deploying a Spring Boot Application
- Why Kubernetes: MicroK8s is a lightweight, local Kubernetes environment suitable for both development and production. It provides an efficient way to experiment with Kubernetes.
Key Steps:
-
Install MicroK8s:
sudo snap install microk8s --classic
-
Check MicroK8s status:
microk8s status --wait-ready
-
Enable necessary add-ons:
microk8s enable dns registry ingress
2. Docker Image Build and Push Using Jib
- Why Jib: Jib Maven plugin is used for a Docker-less build process for your Spring Boot app. It eliminates the need to create a
Dockerfile
.
Key Commands and Configuration:
-
Build Docker Image:
./mvnw compile jib:build
-
Jib Configuration (
pom.xml
):<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>3.3.2</version> <configuration> <to> <image>localhost:32000/hello-spring-boot:latest</image> </to> <allowInsecureRegistries>true</allowInsecureRegistries> </configuration> </plugin>
-
Build with Version Tag:
<image>${project.artifactId}:${project.version}</image>
3. Deployment and Service Configuration in Kubernetes
- Why YAML Files: YAML files allow you to declaratively define the desired state for your app. This makes it easier to manage in the long term.
Key Configuration:
-
Deployment YAML:
apiVersion: apps/v1 kind: Deployment metadata: name: hello-spring-boot spec: replicas: 1 selector: matchLabels: app: hello-spring-boot template: metadata: labels: app: hello-spring-boot spec: containers: - name: hello-spring-boot image: localhost:32000/hello-spring-boot:latest ports: - containerPort: 8080
-
Service YAML:
apiVersion: v1 kind: Service metadata: name: hello-spring-boot spec: selector: app: hello-spring-boot ports: - protocol: TCP port: 80 targetPort: 8080 type: NodePort
-
Apply the Configurations:
microk8s kubectl apply -f k8s/spring-boot-deployment.yaml microk8s kubectl apply -f k8s/spring-boot-service.yaml
-
Verify the Deployment:
microk8s kubectl get pods
4. Handling Updates to the App (Image Version)
- Why Update Manually: Kubernetes doesn’t detect changes if the image tag remains the same. You need to manually trigger an update to apply the new changes.
How to Trigger an Update:
- Force a Pod Restart:
Or:microk8s kubectl rollout restart deployment/hello-spring-boot
microk8s kubectl set image deployment/hello-spring-boot hello-spring-boot=localhost:32000/hello-spring-boot:latest
5. Troubleshooting and Working with Kubernetes
- Useful Commands:
-
Check All Resources:
microk8s kubectl get all --all-namespaces
-
Delete Resources:
microk8s kubectl delete deployment <deployment-name> microk8s kubectl delete service <service-name>
-
6. Working with MicroK8s Local Registry
- Why Local Registry: The local registry is used for storing Docker images in your local environment (
localhost:32000
), which simplifies the development process.
Push Image to Local Registry:
- After building the app using Jib, the image is pushed to
localhost:32000
:microk8s ctr images ls
7. Common Issues and Solutions:
- No Update After Rebuild with Same Tag: Kubernetes doesn’t detect changes if the image tag is the same. You need to either:
- Trigger a rolling update (
rollout restart
). - Use
kubectl set image
to reset the image.
- Trigger a rolling update (
- Kubernetes Services Exposing Random Ports: The
NodePort
service exposes a random port by default. Use the following to find the assigned port:microk8s kubectl get svc
8. Why Everything Works the Way It Does:
- Declarative vs Imperative: Kubernetes works in a declarative manner, where you define the desired state (e.g., the image, replica count) in YAML files. Kubernetes ensures that the actual state matches the desired state.
- No Automatic Image Pulls: Kubernetes won’t pull the image again unless the tag is updated or you explicitly tell it to pull the latest image.
9. Next Steps:
- API Gateway: Integrate an API Gateway like Spring Cloud Gateway.
- Authentication: Configure Keycloak for securing your APIs.
- Microservices Architecture: Scale your app and manage services using Kubernetes.
Key Commands and Concepts:
-
Build and Push Image (with Jib):
./mvnw compile jib:build
-
Deploy and Apply YAML:
microk8s kubectl apply -f spring-boot-deployment.yaml microk8s kubectl apply -f spring-boot-service.yaml
-
Force Deployment Update:
microk8s kubectl rollout restart deployment/hello-spring-boot
-
Manage Local Registry:
microk8s ctr images ls
-
Check Pods and Services:
microk8s kubectl get pods microk8s kubectl get svc
This markdown document should serve as a solid reference for your future Kubernetes development workflow. It covers key concepts, configurations, and troubleshooting steps for deploying and managing your Spring Boot app on MicroK8s.