Interview question
How do you deploy Spring Boot applications on Kubernetes? Kubernetes पर Spring Boot applications कैसे deploy करते हैं?
Answer
Create Kubernetes manifests (Deployment, Service, ConfigMap) to run containerized Spring Boot apps. Use kubectl for deployment, manage scaling, and enable service discovery in cluster.
// deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
labels:
app: app
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: app:1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
env:
- name: SPRING_PROFILES_ACTIVE
value: 'kubernetes'
- name: SPRING_DATASOURCE_URL
valueFrom:
configMapKeyRef:
name: app-config
key: db-url
resources:
requests:
memory: '512Mi'
cpu: '250m'
limits:
memory: '1024Mi'
cpu: '500m'
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
// service.yaml
apiVersion: v1
kind: Service
metadata:
name: app-service
spec:
type: LoadBalancer
selector:
app: app
ports:
- protocol: TCP
port: 80
targetPort: 8080
// configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
db-url: 'jdbc:mysql://mysql-service:3306/appdb'
log-level: 'INFO'
// Deployment Commands
# Apply manifests
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f configmap.yaml
# Check deployment status
kubectl get deployments
kubectl get pods
kubectl logs -f pod-name
# Scale application
kubectl scale deployment app --replicas=5
// Liveness vs Readiness Probes
// Liveness: Is app alive? (restart if false)
// Readiness: Can app handle traffic? (remove from LB if false)
// Production Checklist:
// 1. Resource limits (CPU, memory)
// 2. Health checks (liveness, readiness)
// 3. Rolling updates
// 4. Service discovery
// 5. ConfigMap for config
// 6. Secrets for credentials
// 7. Persistent volumes for data
// 8. Horizontal Pod Autoscaler (HPA)Kubernetes Deployment:
Manifests बनाना:
1. Deployment - pods manage करना
2. Service - networking expose करना
3. ConfigMap - configuration
4. Secret - credentials (passwords)
Deployment Config:
- Image: app:1.0
- Replicas: 3 (HA)
- Resource limits
- Environment variables
- Health checks
Probes:
- Liveness: Is alive?
- Readiness: Can serve?
Service:
- Type: LoadBalancer
- Port mapping
- Service discovery
Commands:
kubectl apply -f manifest.yaml
kubectl scale deployment app --replicas=5
kubectl logs pod-name
Scaling:
- Horizontal (HPA) - pods add करना
- Vertical (VPA) - resources बढ़ानाWas this answer clear?