Understanding Docker and Kubernetes: Principles, Architecture, and Deployment Practices
This article explains the fundamentals of containerization by reviewing virtualization concepts, detailing Docker's architecture and Dockerfile syntax, and then introduces Kubernetes' control‑plane and node components, providing step‑by‑step examples for deploying a simple Nginx service and a Java web application on a K8s cluster, both manually and with automation tools.
Preface
Developers often struggle with configuring development environments; Docker emerged as a solution that packages the required runtime and dependencies into a portable container.
Virtualization Technology
Virtualization abstracts physical resources into virtual ones, enabling programs to run across different platforms. Traditional virtualization examples include Java Virtual Machine, while containerization isolates applications in lightweight, independent environments.
Docker Container
What is Docker?
Docker packages an application together with its environment, allowing developers to avoid "it works on my machine" problems. It originated in 2010, built on Linux Containers (LXC), and became an open‑source project in 2013 with the Docker Engine.
Docker Architecture
Docker consists of three core components:
Docker Client : the user‑facing CLI that sends commands to the Docker Engine.
Docker Host : the machine (physical or virtual) that runs containers; it contains the Docker Daemon which manages container lifecycles, images, and networking.
Docker Registry : a storage service for Docker images.
Typical workflow: the user runs docker pull via the client, the daemon retrieves the image from the registry, and then docker run creates and starts a container on the host.
Docker Image and Dockerfile
An image is built from a Dockerfile , which defines a series of instructions (FROM, RUN, COPY, WORKDIR, CMD, etc.) that create layered filesystem snapshots.
# Base image using official Python image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy local code into the container
COPY . /app
# Install dependencies
RUN pip install -r requirements.txt
# Start the application
CMD ["python", "app.py"]Key Dockerfile directives:
FROM : specifies the base image.
RUN : executes commands inside the image (e.g., installing packages).
COPY : copies files from the host into the image.
WORKDIR : sets the working directory (similar to cd ).
CMD : defines the default command when a container starts.
Kubernetes (K8s) – The Container Orchestration Tool
Background
As container usage grew, managing many containers manually became impractical. Google created Kubernetes to automate deployment, scaling, and management of containerized applications.
K8s Architecture
Kubernetes separates the cluster into a control plane and worker nodes :
API Server : the front‑end for all cluster operations.
Scheduler : assigns Pods to suitable nodes.
etcd : a distributed key‑value store for cluster state.
Node : runs the actual workloads (can be a bare‑metal server or a VM).
Deploying a Service on K8s
Typical resources include Pod , Deployment , and Service . Below is an example that deploys a simple Nginx image.
Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3 # number of pod replicas
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest # use nginx image
ports:
- containerPort: 80 # expose port 80Service YAML
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx # matches the Deployment's pod label
ports:
- protocol: TCP
port: 80 # Service port
targetPort: 80 # forwards to pod port
type: LoadBalancer # optional, for cloud environmentsApply the manifests with:
kubectl apply -f deployment.yaml
kubectl apply -f service.yamlHands‑On K8s with a Java Web Application
Manual Deployment
After building the Java application with Maven ( mvn clean package ) and obtaining a .jar , create a Dockerfile:
# Use OpenJDK 8 as base image
FROM openjdk:8-jre-alpine
# Copy the built JAR into the container
COPY target/multi-thread-0.0.1-SNAPSHOT.jar /usr/app/multi-thread-0.0.1-SNAPSHOT.jar
# Expose the service port
EXPOSE 8080Build and push the image:
docker build -t multi-thread:latest .
docker push
/multi-thread:latestCreate the K8s manifests:
apiVersion: apps/v1
kind: Deployment
metadata:
name: multi-thread-deployment
spec:
replicas: 2
selector:
matchLabels:
app: multi-thread-service
template:
metadata:
labels:
app: multi-thread
spec:
containers:
- name: multi-thread
image: multi-thread:latest
ports:
- containerPort: 8080 apiVersion: v1
kind: Service
metadata:
name: multi-thread
spec:
selector:
app: multi-thread-service
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancerDeploy with:
kubectl apply -f deployment.yaml
kubectl apply -f service.yamlAutomated Deployment
Using Alibaba Cloud's Cloud Toolkit plugin, the Docker image can be built and pushed directly from the IDE, and the Deploy to Registry/Kubernetes action will create the resources in a K8s cluster automatically.
Conclusion
The article covered the core concepts of Docker and Kubernetes, demonstrated how to write Dockerfiles, build images, and deploy workloads on a K8s cluster both manually and with automation tools, providing a practical foundation for developers entering the cloud‑native ecosystem.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.