Kubernetes for Developers
A practical guide to understanding Kubernetes concepts, deploying applications, and managing container orchestration in production environments.
DevOpsAdvanced24 min read
AI Summary
Quick context for Kubernetes for Developers
Kubernetes for Developers is a advanced guide in DevOps. A practical guide to understanding Kubernetes concepts, deploying applications, and managing container orchestration in production environments.. Key topics: containers, docker, kubernetes, orchestration.
## Why Kubernetes Matters
Kubernetes (K8s) has become the industry standard for container orchestration. It automates deployment, scaling, and management of containerized applications across clusters of machines. For developers, understanding Kubernetes is no longer optional—it is a core skill for building cloud-native applications.
At its core, Kubernetes solves the problem of running many containers reliably. It handles scheduling, networking, storage, and self-healing so you can focus on building features instead of managing infrastructure.
## Core Concepts
### Pods
A Pod is the smallest deployable unit in Kubernetes. It represents a single instance of a running process and can contain one or more containers that share network and storage resources.
```yaml
apiVersion: v1
kind: Pod
meta data-removed:
name: my-app
labels:
app: my-app
spec:
containers:
- name: app
image: my-app:1.0.0
ports:
- containerPort: 3000
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "500m"
```
### Deployments
Deployments manage the desired state for Pods and ReplicaSets. They declaratively update applications, rolling out changes safely with zero downtime.
```yaml
apiVersion: apps/v1
kind: Deployment
meta data-removed:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
meta data-removed:
labels:
app: my-app
spec:
containers:
- name: app
image: my-app:1.0.0
ports:
- containerPort: 3000
```
### Services
Services provide stable networking for Pods. Since Pods are ephemeral and can be destroyed or restarted at any time, Services give you a consistent endpoint to reach your application.
```yaml
apiVersion: v1
kind: Service
meta data-removed:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: ClusterIP
```
## ConfigMaps and Secrets
Kubernetes separates configuration from code using ConfigMaps and Secrets. This allows you to change application behavior without rebuilding images.
```yaml
apiVersion: v1
kind: ConfigMap
meta data-removed:
name: app-config data-removed:
DATABASE_HOST: "postgres.default.svc.cluster.local"
CACHE_TTL: "3600"
```
Secrets work similarly but are base64-encoded and should be managed with additional security measures like encryption at rest.
## Networking and Ingress
Ingress resources route external HTTP traffic to Services within the cluster. An Ingress controller interprets these rules and manages load balancing.
```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
meta data-removed:
name: my-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
```
## Scaling and Resource Management
Horizontal Pod Autoscaling adjusts the number of Pod replicas based on CPU utilization or custom metrics. This ensures your application handles traffic spikes without manual intervention.
```yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
meta data-removed:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
```
## Best Practices for Developers
- **Use labels consistently** to organize and query resources effectively
- **Set resource requests and limits** to prevent resource contention
- **Implement health checks** using liveness and readiness probes
- **Externalize configuration** through environment variables and ConfigMaps
- **Use namespaces** to isolate environments and teams
## Getting Started Locally
Tools like Minikube, kind, or Docker Desktop's Kubernetes cluster let you experiment locally before deploying to production. Start small with a single-node cluster and gradually explore more advanced features like persistent volumes, RBAC, and Helm charts.
Continue with related content
Suggested next reads and tools from the knowledge graph.