Mastering Kubernetes Service Deployment: From Docker Build to HPA
This guide walks you through the complete Kubernetes service deployment workflow, covering Docker image creation with multi‑stage builds, pushing to a registry, defining Deployment and Service resources, applying and monitoring them, managing configuration, implementing horizontal pod autoscaling, and integrating logging and monitoring solutions.
1. Project Preparation: Docker Image Build
1.1 Write Dockerfile
Use official base images (e.g., openjdk:17-jdk-alpine, nginx:latest) for security and lightweight footprints.
Leverage multi‑stage builds to reduce final image size and improve startup speed.
Expose necessary ports and define the entrypoint to let Kubernetes manage the container lifecycle.
Example for a Java Spring Boot application:
# Builder stage
FROM maven:3.8.7-openjdk-17 AS builder
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests
# Run stage
FROM openjdk:17-jdk-alpine
WORKDIR /app
COPY --from=builder /app/target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]1.2 Build and Push Image
docker build -t your-registry.com/your-project/app:v1.0 .
docker push your-registry.com/your-project/app:v1.0It is recommended to use a private registry (e.g., Harbor or a private Docker Hub repository) and enforce strict version naming.
2. Kubernetes Deployment Resources
2.1 Deployment
Defines replica count, update strategy, container image, and environment variables.
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-deployment
labels:
app: app
spec:
replicas: 3
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app-container
image: your-registry.com/your-project/app:v1.0
ports:
- containerPort: 8080
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
imagePullSecrets:
- name: regcred # create this secret if using a private registry2.2 Service
Exposes the Deployment inside the cluster or to external clients.
apiVersion: v1
kind: Service
metadata:
name: app-service
spec:
type: ClusterIP # use LoadBalancer or Ingress for external access
selector:
app: app
ports:
- port: 80
targetPort: 8080Set type: LoadBalancer or configure an Ingress to make the service reachable from outside the cluster.
3. Application Release and Management
3.1 Apply Resources
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml3.2 Monitor Rollout Status
kubectl rollout status deployment/app-deploymentCheck that Pods start successfully and the desired number of replicas become ready.
3.3 Rolling Update and Rollback
Rolling Update
Update the image version in the Deployment:
kubectl set image deployment/app-deployment app-container=your-registry.com/your-project/app:v1.1Rollback
kubectl rollout undo deployment/app-deployment4. Configuration Management and Secrets
Use ConfigMap for non‑sensitive configuration files or parameters.
Use Secret for sensitive data such as database passwords or API keys.
Example ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
application.properties: |
server.port=8080
logging.level.root=INFOMount the ConfigMap into the container:
volumeMounts:
- name: config-volume
mountPath: /app/config
volumes:
- name: config-volume
configMap:
name: app-config5. Advanced Practice: Horizontal Pod Autoscaling (HPA)
Automatically scale replicas based on CPU usage or custom metrics.
kubectl autoscale deployment app-deployment --min=2 --max=10 --cpu-percent=606. Logging and Monitoring Integration
Logging : Deploy Fluentd → Elasticsearch → Kibana (EFK stack) to collect and visualize logs.
Metrics : Use Prometheus + Grafana to expose and chart application and cluster metrics.
7. Conclusion
Although the Kubernetes service deployment process appears complex, mastering Docker image construction, defining Deployment and Service resources, handling configuration, and enabling autoscaling ensures high availability and maintainability. As the ecosystem matures, CI/CD automation further accelerates delivery efficiency for development and operations teams.
Full-Stack DevOps & Kubernetes
Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
