Skip to content
Plurafy

Kubernetes Basics

Kubernetes orchestrates containerized applications at scale. Learn fundamental Kubernetes concepts for production deployments.

Deploy Node.js applications to Kubernetes with proper configuration, resource management, and high availability.

  • Pod: Smallest deployable unit
  • Service: Network exposure
  • Deployment: Replica management
  • ConfigMap: Configuration data
  • Secret: Sensitive data
  • Ingress: HTTP/HTTPS routing
  • Orchestration: Automate container management
  • Scaling: Horizontal pod autoscaling
  • Self-healing: Restart failed pods
  • Load Balancing: Distribute traffic
  • Updates: Rolling deployments
k8s/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-app
labels:
app: nodejs-app
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: nodejs-app
template:
metadata:
labels:
app: nodejs-app
spec:
containers:
- name: app
image: myapp:latest
imagePullPolicy: Always
ports:
- containerPort: 3000
name: http
env:
- name: NODE_ENV
value: production
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: app-secrets
key: database-url
- name: REDIS_URL
valueFrom:
configMapKeyRef:
name: app-config
key: redis-url
# Resource limits
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
# Health checks
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 10
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 2
# Graceful shutdown
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 15"]
# Volumes
volumeMounts:
- name: logs
mountPath: /app/logs
# Volume configuration
volumes:
- name: logs
emptyDir: {}
# Node affinity
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
nodeAffinityTerm:
matchExpressions:
- key: node-type
operator: In
values: [app-node]
k8s/service.yaml
apiVersion: v1
kind: Service
metadata:
name: nodejs-app-service
labels:
app: nodejs-app
spec:
type: ClusterIP
selector:
app: nodejs-app
ports:
- port: 80
targetPort: 3000
protocol: TCP
name: http
# Session affinity
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
---
# Load balancer for external access
apiVersion: v1
kind: Service
metadata:
name: nodejs-app-lb
labels:
app: nodejs-app
spec:
type: LoadBalancer
selector:
app: nodejs-app
ports:
- port: 80
targetPort: 3000
protocol: TCP
k8s/ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nodejs-app-ingress
annotations:
cert-manager.io/cluster-issuer: "letsencrypt-prod"
nginx.ingress.kubernetes.io/rate-limit: "100"
spec:
ingressClassName: nginx
tls:
- hosts:
- api.example.com
secretName: tls-secret
rules:
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nodejs-app-service
port:
number: 80
- path: /api/v2
pathType: Prefix
backend:
service:
name: nodejs-app-v2-service
port:
number: 80
k8s/config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
LOG_LEVEL: "info"
REDIS_URL: "redis://redis-service:6379"
API_TIMEOUT: "30000"
MAX_CONNECTIONS: "100"
---
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
stringData:
database-url: postgresql://user:password@postgres:5432/appdb
jwt-secret: your-secret-key-here
api-key: your-api-key
---
# Docker registry secret for private images
apiVersion: v1
kind: Secret
metadata:
name: regcred
type: kubernetes.io/dockercfg
data:
.dockercfg: eyJhdXRocyI6IHt...
k8s/hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nodejs-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nodejs-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 30
scripts/k8s-deploy.sh
#!/bin/bash
set -e
NAMESPACE=${NAMESPACE:-default}
IMAGE=${IMAGE:-myapp:latest}
ENV=${ENV:-staging}
echo "Deploying to Kubernetes ($ENV)..."
# Update image
kubectl set image deployment/nodejs-app \
app=$IMAGE \
-n $NAMESPACE
# Wait for rollout
kubectl rollout status deployment/nodejs-app \
-n $NAMESPACE \
--timeout=5m
# Verify health
echo "Verifying deployment health..."
kubectl get pods -n $NAMESPACE -l app=nodejs-app
echo "✓ Deployment successful"
  1. Resource Management

    • Set resource requests and limits
    • Monitor usage
    • Use namespaces
    • Define quotas
  2. Health Checks

    • Liveness probes
    • Readiness probes
    • Startup probes
    • Graceful shutdown
  3. Security

    • Use Secrets for sensitive data
    • RBAC policies
    • Network policies
    • Container security
  4. High Availability

    • Multiple replicas
    • Pod disruption budgets
    • Anti-affinity rules
    • Rolling updates
  5. Monitoring

    • Prometheus metrics
    • Pod monitoring
    • Node monitoring
    • Alerting
  • Production Deployments: Scale applications globally
  • Multi-Environment: Dev, staging, production
  • Auto-Scaling: Handle variable load
  • Disaster Recovery: High availability setup
  • Service Mesh: Advanced networking