Server management using Docker Swarm and Traefik
Introduction
While operating personal projects, I found myself managing multiple services, and the increasing AWS costs each month were becoming burdensome. In the early stages where there was no revenue, reducing fixed expenses was crucial, so I decided to purchase three relatively inexpensive mini servers and set up a self-hosting environment.
Until now, I had been configuring services on a single server using only Docker Compose, but as the number of servers increased, there were clear limitations to the Compose method. Especially when it came to manually placing containers based on CPU and memory usage, it was inefficient in terms of scalability and management.
To address this, I introduced Docker Swarm. Swarm allows multiple servers to be configured as a single cluster, supporting flexible deployment and scaling of services. When used together with Traefik, there is no need to configure Nginx settings separately for each service, and HTTPS certificates are automatically issued and renewed, reducing operational burdens significantly.
For someone like me who develops and operates alone, Docker Swarm and Traefik were great choices to maintain a complex infrastructure in a concise manner. In this article, I will try to summarize the process of how I actually configured it.
Hardware Configuration
Introduction of Mini Server Specifications
The servers I built this time are 3 mini PCs based on Asrock DeskMini X300 barebones. Despite their small size, they provide sufficient performance for server use, making them a good choice for a self-hosting environment.
The main specifications of each server are as follows:
- Model: Asrock DeskMini X300
- CPU: Ryzen 5600G
- RAM: 64GB
- HDD: 1TB SSD
- Cooler: [NOCTUA] NH-L9a-AM4 (low-noise cooler)
This configuration provides enough performance to run about 10-20 personal services in containers, and the ample RAM and SSD capacity allow for the smooth operation of heavy services as well. I purchased 3 units. Considering that they would be running 24/7 at home, I bought and installed low-noise coolers. (My previous experience operating servers at work was helpful. They were so noisy...)
The power consumption is relatively low compared to a regular desktop, making it suitable for use as a home server that is always on.
Introduction of Network Equipment
The current internet connection used is KT Gigabit Internet, and one of KT's features is that it provides a separate public IP for each LAN port of the modem. I configured the 3 servers to be independently accessible from the outside using this feature.
The network equipment consists of the following TP-Link Omada series:
- Router: TP-Link ER605
- Switch: TP-Link TL-SG2210P (supports 2.5Gbps, PoE)
- Controller: TP-Link OC200
- Wireless AP: TP-Link EAP650 (Wi-Fi 6)
These devices are all managed through the Omada SDN system, providing an environment where VLAN configuration, traffic monitoring, and port-level control are possible.
Server 1
- Directly connected to the KT modem for public IP assignment
- Operating fixed services such as Harbor Docker registry, DB, and document system
Server 2
- Configured as a cluster with Docker Swarm
- Distributing multiple personal services in container units
Thanks to this configuration, I completed a structure where I could manage images in Harbor while flexibly deploying/scaling/managing services on the other servers.
Initially, I tried to configure all 3 servers as a cluster to integrate all services and infrastructure, but there were constraints in stably operating complex services like Harbor in a Docker Swarm environment. For a more flexible configuration, I ended up separating roles as it is now.
Advantages
Easy Traffic Management with Traefik
One of the most convenient aspects of introducing Traefik was that traffic management became much simpler. Previously, I had to manually write Nginx configurations for each service and obtain SSL certificates manually. However, Traefik automatically handles the following functions:
- Automatic issuance and renewal of HTTPS certificates through Let’s Encrypt
- Domain linking, redirection, and port specification with just label settings in docker-compose.yml
- Visual monitoring of current routing status and service status through the provided dashboard
As a solo developer, the fact that I didn't have to repeat configurations was a huge advantage.
Easy Scaling
Using Docker Swarm makes deploying or scaling new services much simpler. By simply registering a new worker node in the cluster, Swarm automatically distributes containers according to available resources.
In other words, there is no need to worry about "this service should run on this server" anymore; Swarm deploys containers and distributes resources automatically.
Zero-Downtime Deployment (Rolling Update)
Through the update_config
setting, you can replace the existing service with a new version without stopping it. Swarm first launches the new container before shutting down the existing one, ensuring proper operation before terminating the old instance.
services:
my-app:
image: myapp:v1
deploy:
replicas: 1
update_config:
order: start-first
parallelism: 1
delay: 5s
When there are 2 or more servers, the new container is placed on a different node first, so the deployment takes place without users of the actual service noticing.
Service Isolation and Rollback Feature
Each service runs in an isolated container, allowing you to quickly fix or rollback a specific service if problems arise. For example, if an issue occurs after updating an image, you can easily revert to the previous state with a single command, enabling fast and safe operation.
docker service rollback my-service
Dynamic Routing Based on Traefik
Traefik automatically configures routing based on Docker labels. With just a few lines added to the docker-compose.yml, domain linking, path configuration, and HTTPS application are automatically handled.
Unlike Nginx, which requires a lot of manual configuration, this is a significant advantage as there is no need to reconfigure routing when services are added or renamed.
Conclusion
For a solo developer who has to handle both development and operations, I believe that the most important thing is to keep a complex infrastructure simple.
Through configuring servers using Docker Swarm and Traefik this time, I gained confidence that I can create an infrastructure that works well on its own, even without complex tools or cloud environments.
Of course, there were trial and error in the initial setup, and especially due to the lack of practical resources on Docker Swarm, I struggled in some areas. However, the experience I gained from setting up and testing myself became a stronger foundation than any documentation.
Some may question, "Do you really need to use Docker Swarm?" But in situations where you have to handle everything from development to deployment and operation alone, I believe that simplicity, automation, and ease of maintenance are the biggest advantages and weapons.
