0
0
Dockerdevops~5 mins

Swarm vs Kubernetes decision in Docker - CLI Comparison

Choose your learning style9 modes available
Introduction
When you want to run many containers together, you need a way to manage them easily. Docker Swarm and Kubernetes are two tools that help you organize and control groups of containers. Choosing the right one depends on your needs and how complex your setup is.
When you want a simple way to run a few containers across multiple servers with easy setup.
When you need to manage a large number of containers with advanced features like automatic scaling and self-healing.
When you want to quickly deploy containerized apps without learning complex commands.
When you require strong community support and many integrations for your container orchestration.
When you want built-in load balancing and service discovery with minimal configuration.
Commands
This command starts a new Docker Swarm cluster on your current machine, making it the manager node that controls the cluster.
Terminal
docker swarm init
Expected OutputExpected
Swarm initialized: current node (abcdef123456) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-0abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890 192.168.1.100:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
This command checks the installed Kubernetes client version to confirm Kubernetes tools are ready to use.
Terminal
kubectl version --client
Expected OutputExpected
Client Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.0", GitCommit:"abcdef123456", GitTreeState:"clean", BuildDate:"2024-05-01T12:00:00Z", GoVersion:"go1.20", Compiler:"gc", Platform:"linux/amd64"}
--client - Show only client version without connecting to a cluster
This command creates a simple web service in Docker Swarm with 3 copies running and exposes port 8080 on the host to port 80 in the containers.
Terminal
docker service create --name my-web --replicas 3 -p 8080:80 nginx
Expected OutputExpected
qwertyuiop1234567890
--replicas - Number of container copies to run
-p - Publish container port to host port
This command creates a Kubernetes deployment named my-web running 3 copies of the nginx container.
Terminal
kubectl create deployment my-web --image=nginx --replicas=3
Expected OutputExpected
deployment.apps/my-web created
--replicas - Number of pod copies to run
This command creates a service to expose the my-web deployment outside the cluster on port 8080, forwarding to port 80 in the containers.
Terminal
kubectl expose deployment my-web --type=LoadBalancer --port=8080 --target-port=80
Expected OutputExpected
service/my-web exposed
--type=LoadBalancer - Expose service externally with a load balancer
Key Concept

If you remember nothing else, remember: Docker Swarm is simple and quick for small setups, while Kubernetes is powerful and flexible for complex, large-scale container management.

Common Mistakes
Trying to use Docker Swarm commands without initializing a swarm first
The commands will fail because there is no swarm cluster to manage yet
Always run 'docker swarm init' on the manager node before creating services
Exposing Kubernetes services without specifying the correct port mappings
The service will not be reachable on the expected ports, causing connection failures
Use '--port' for the external port and '--target-port' for the container port when exposing services
Assuming Docker Swarm and Kubernetes commands are interchangeable
They use different command sets and concepts; mixing them causes confusion and errors
Learn and use the commands specific to each tool separately
Summary
Initialize Docker Swarm with 'docker swarm init' to start managing containers simply.
Use 'docker service create' to run container services in Swarm with replicas and port mapping.
Use 'kubectl create deployment' and 'kubectl expose' to manage containers and services in Kubernetes.
Docker Swarm suits small, quick setups; Kubernetes suits complex, scalable environments.