apiVersion: apps/v1
kind: Deployment
metadata:
name: user-service
labels:
app: user-service
spec:
replicas: 3
selector:
matchLabels:
app: user-service
template:
metadata:
labels:
app: user-service
spec:
containers:
- name: user-service
image: myregistry/user-service:1.0.0
ports:
- containerPort: 8080
name: http
- containerPort: 9090
name: management
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
- name: SPRING_DATASOURCE_URL
valueFrom:
configMapKeyRef:
name: user-service-config
key: database.url
- name: SPRING_DATASOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: user-service-secret
key: database.password
resources:
requests:
memory: "512Mi"
cpu: "250m"
limits:
memory: "1Gi"
cpu: "500m"
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 9090
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 9090
initialDelaySeconds: 20
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 3
apiVersion: v1
kind: Service
metadata:
name: user-service
spec:
type: ClusterIP
selector:
app: user-service
ports:
- name: http
port: 80
targetPort: 8080
protocol: TCP
- name: management
port: 9090
targetPort: 9090
protocol: TCP
apiVersion: v1
kind: ConfigMap
metadata:
name: user-service-config
data:
database.url: "jdbc:postgresql://postgres:5432/userdb"
application.yml: |
spring:
application:
name: user-service
jpa:
hibernate:
ddl-auto: validate
apiVersion: v1
kind: Secret
metadata:
name: user-service-secret
type: Opaque
data:
database.password: cGFzc3dvcmQxMjM= # base64 encoded
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: user-service-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: user-service
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
Kubernetes orchestrates containerized applications at scale. Deployments manage ReplicaSets ensuring desired pod count. Services expose pods with stable endpoints. ConfigMaps externalize configuration. Secrets store sensitive data. Resource limits prevent resource exhaustion. Liveness probes detect failed containers; readiness probes control traffic. Horizontal Pod Autoscaler scales based on metrics. Ingress manages external access. Namespaces organize resources. Helm charts template Kubernetes manifests. Kubernetes enables zero-downtime deployments, self-healing, and automatic scaling. Spring Boot apps integrate via health endpoints, graceful shutdown. Proper Kubernetes configuration ensures high availability, efficient resource usage, and operational excellence in production.
Related snips
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
createdAt: String!
GraphQL API with Spring Boot
package com.example.starter.config;
import com.example.starter.properties.CustomProperties;
import com.example.starter.service.CustomService;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Custom Spring Boot starters
package com.example.demo.config;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
Messaging with Apache Kafka
require "timeout"
module HealthCheck
class Probe
Result = Struct.new(:name, :status, :latency_ms, :critical, :error, keyword_init: true) do
def healthy?
Health Check Endpoint with Dependency Probes
package com.example.demo.controller;
import com.example.demo.dto.FileMetadata;
import com.example.demo.service.FileStorageService;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
File upload and download handling
// Express app with health checks and graceful shutdown
const express = require('express');
const { createServer } = require('http');
const app = express();
const server = createServer(app);
Container health checks and graceful shutdown patterns
Share this code
Here's the card — post it anywhere.