top of page
Writer's picturevP

Lab 16 - Docker Swarm for Orchestration - Day 89

Welcome back to the #90DaysOfDevOps series! As we near the finish line, it's time to explore a crucial aspect of container orchestration: Docker Swarm. In today's lab (Lab 16), we'll explore the Docker Swarm and understand how it can simplify the management of containerized applications.


Why Docker Swarm?

Docker Swarm is Docker's built-in orchestration tool that allows you to manage and scale your containerized applications effortlessly. It's an excellent choice for those looking for a straightforward and native solution without the need for external tools.


Setting Up Docker Swarm

Let's jump into the practical side of things. Assuming you have Docker installed on your machine, follow these steps:

1. Initialize Swarm:

Open your terminal and run the following command to initialize Docker Swarm:

docker swarm init

2. Join Nodes (if needed):

If you have multiple machines and want to create a multi-node Swarm, use the provided command from the output of the docker swarm init command on other machines to join the Swarm.


Deploying Services

Now that your Swarm is up and running, let's deploy a simple service.

1. Create a Docker Compose file:

Create a file named docker-compose.yml with the following content:

version: '3'
services:
  web:
    image: nginx:alpine
    deploy:
      replicas: 3
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

2. Deploy the service:

Run the following command to deploy the service defined in the Compose file:

docker stack deploy -c docker-compose.yml myapp

Scaling Services

One of the key benefits of Docker Swarm is its ability to scale services effortlessly.

Scale the service:

To scale the service to 5 replicas, use the following command:

docker service scale myapp_web=5

Inspecting the Swarm

Let's take a look at the current state of our Swarm.

List services:

Use the following command to see the deployed services and their replicas:

docker service ls

Check nodes:

Verify the nodes in the Swarm by running:

docker node ls

Updating Services

Docker Swarm makes it easy to update services without downtime.

Update the service:

Modify the docker-compose.yml file, changing the image version to the latest. Then, update the service with:

docker stack deploy -c docker-compose.yml myapp

Cleaning Up

As always, it's good practice to clean up after you've completed your experiments.

Remove the service:

Stop and remove the running service with:

docker stack rm myapp

Leave Swarm:

If you want to dismantle the Swarm, run:

docker swarm leave --force

Congratulations! You've successfully navigated through setting up Docker Swarm, deploying services, scaling, inspecting, and updating them. Docker Swarm simplifies orchestration, making it accessible even if you're just starting with containerization.


Keep the DevOps spirit high, and happy orchestrating!


Thank you for reading!


*** Explore | Share | Grow ***

8 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page