TL;DR
Master Docker and Kubernetes integration with this comprehensive guide covering container orchestration, deployment strategies, and best practices for cloud-native applications
Docker Kubernetes Integration: A Complete Guide
Docker and Kubernetes integration enables powerful container orchestration capabilities for modern cloud-native applications. This guide explores the integration patterns, deployment strategies, and best practices for using Docker with Kubernetes.
$1
`` graph TB
subgraph "Docker Environment"
A[Docker Engine]
B[Container Registry]
C[Docker Build]
end
subgraph "Kubernetes Cluster"
D[API Server]
E[Controller Manager]
F[Scheduler]
G[kubelet]
H[Container Runtime]
end
A --> B
B --> H
C --> B
D --> G
G --> H
E --> D
F --> D
classDef docker fill:#1a73e8,stroke:#fff,color:#fff
classDef k8s fill:#34a853,stroke:#fff,color:#fff
class A,B,C docker
class D,E,F,G,H k8s
mermaid
`
$1
$1
` {
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"registry-mirrors": [
"https://registry.docker-cn.com",
"https://mirror.gcr.io"
]
}
yaml
`/etc/docker/daemon.json
$1
` version = 2
[plugins."io.containerd.grpc.v1.cri"]
[plugins."io.containerd.grpc.v1.cri".containerd]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
yaml
`containerd-config.toml
$1
$1
` apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myregistry.azurecr.io/myapp:v1
ports:
- containerPort: 8080
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "200m"
memory: "256Mi"
yaml
`deployment.yaml
$1
` apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: app
image: myregistry.azurecr.io/myapp:v1
volumeMounts:
- name: shared-data
mountPath: /data
- name: sidecar
image: myregistry.azurecr.io/sidecar:v1
volumeMounts:
- name: shared-data
mountPath: /data
volumes:
- name: shared-data
emptyDir: {}
yaml
`multi-container-pod.yaml
$1
$1
| Registry | Authentication | Integration |
|---|---|---|
| Docker Hub | docker login | imagePullSecrets |
| Azure Container Registry | az acr login | Managed Identity |
| Google Container Registry | gcloud auth | Workload Identity |
$1
` apiVersion: v1
kind: Secret
metadata:
name: registry-secret
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: ---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
template:
spec:
imagePullSecrets:
- name: registry-secret
containers:
- name: myapp
image: myregistry.azurecr.io/myapp:v1
yaml
`secret.yaml
deployment-with-secret.yaml
$1
$1
` apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: myapp ---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myapp-service
port:
number: 80
yaml
`service.yaml
ingress.yaml
$1
$1
` apiVersion: v1
kind: PersistentVolume
metadata:
name: myapp-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/myapp ---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myapp-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
yaml
`persistent-volume.yaml
persistent-volume-claim.yaml
$1
$1
` apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-quota
spec:
hard:
requests.cpu: "4"
requests.memory: 4Gi
limits.cpu: "8"
limits.memory: 8Gi
yaml
`resource-quota.yaml
$1
` apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: myapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
yaml
`hpa.yaml
$1
$1
` apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: myapp-monitor
spec:
selector:
matchLabels:
app: myapp
endpoints:
- port: metrics
yaml
`prometheus-servicemonitor.yaml
$1
| Component | Log Path | Collection Method |
|---|---|---|
| Container Logs | /var/log/containers | Fluentd DaemonSet |
| Docker Daemon | /var/log/docker | Filebeat |
| Kubernetes Events | API Server | Event Exporter |
$1
$1
` apiVersion: v1
kind: Pod
metadata:
name: secure-pod
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
containers:
- name: myapp
image: myregistry.azurecr.io/myapp:v1
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
yaml
`secure-pod.yaml
$1
` apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: myapp-policy
spec:
podSelector:
matchLabels:
app: myapp
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 8080
yaml
``network-policy.yaml
$1
$1
| Practice | Description | Implementation |
|---|---|---|
| ConfigMaps | Store configuration | Use volumes |
| Secrets | Secure sensitive data | Use env vars |
| RBAC | Access control | Use roles |
$1
$1
| Issue | Cause | Solution |
|---|---|---|
| ImagePullBackOff | Registry auth | Check secrets |
| CrashLoopBackOff | App failure | Check logs |
| Pending | Resource limits | Check quota |
$1
1. [Kubernetes Documentation](https://kubernetes.io/docs/)
2. [Docker Documentation](https://docs.docker.com/)
3. [Container Runtime Interface](https://kubernetes.io/docs/concepts/architecture/cri/)
4. [Kubernetes Networking](https://kubernetes.io/docs/concepts/cluster-administration/networking/)
5. [Kubernetes Security](https://kubernetes.io/docs/concepts/security/)
6. [Kubernetes Best Practices](https://kubernetes.io/docs/concepts/configuration/overview/)
$1
Why This Matters
Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.
Trade-offs to Consider
Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.
When NOT to Use This
Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.
Decision Framework
Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.