Deploying Applications in Docker Swarm

sachin maharjansachin maharjan
3 min read

Prerequisites

1. Operating System Installation: Ensure the required operating system is installed on all nodes.

2. Network Configuration: Verify that the physical network between all nodes is correctly configured and that the nodes can communicate with each other.

Docker Installation

1. Install Docker: Install Docker on all nodes (both master and worker nodes) following the official Docker documentation: https://docs.docker.com/engine/install/ubuntu/

Ensure Docker is properly installed by running:

docker --version

Setting Up Docker Swarm

1. Initialize Docker Swarm on the Master Node:

  • On the master node, initialize the Docker Swarm by running:

    docker swarm init

  • Note the command and token provided after the initialization. This token is required for worker nodes to join the swarm.

2. Join Worker Nodes to the Swarm:

  • On each worker node, use the command provided by the master node to join the swarm:

    docker swarm join --token <worker-token> <master-node-ip>:2377

  • Verify that the worker nodes have successfully joined the swarm by running the following command on the master node:

    docker node ls

Deploying the Stack

1. Create/Update the Stack YML File:

  • Ensure that the stack YML file (e.g., docker-stack.yml) is up to date with the necessary services and configurations.

2. Deploy the Stack:

  • Deploy the stack using the following command on the master node:

    docker stack deploy -c docker-stack.yml <stack-name>

  • Confirm that all services are running as expected:

    docker stack services <stack-name>

Troubleshooting Ingress Network Issues

1. Network Troubleshooting:

  • If there are issues related to the ingress network, such as network connectivity problems between nodes, use the following command on the affected node(s) to disable checksum offloading:

    ethtool -k ens160 tx-checksum-ip-generic off

  • Replace ens160 with the appropriate network interface name for your environment.

2. Verify Network Configuration:

  • Ensure that all nodes can communicate over the required ports and that no firewall or network issues are causing problems.

Additional Considerations

1. Regular Monitoring: Continuously monitor the health and performance of the Docker Swarm cluster using tools like docker service ps, docker node ls, and Docker logs.

2. Scaling Services: Adjust the number of replicas or scale services as needed using the stack YML file or docker service scale command.

3. Updating Services: Update services in the stack by modifying the YML file and re-running the docker stack deploy command.

Container Registry

We use Docker’s open-source container registry, Registry 2, to store our container images. This registry can be easily installed and configured within Docker.

docker run -d -p 5000:5000 –restart=always –name registry \

-v /opt/docker-registry/data: /var/lib/registry\

registry:2

This command sets up the registry as a Docker container, with persistent storage for your images at /opt/docker-registry/data.

Note: We must build an image and make the tag to push it in the registry. Also, we need to add insecure registries in the daemon.json of docker. Deamon.json file can be found in /etc/docker/

docker build -t dmn/image_name path_to_dockerfile

docker tag local_image image_with_registry_url

docker push image_with_registry_url

0
Subscribe to my newsletter

Read articles from sachin maharjan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

sachin maharjan
sachin maharjan

Hello, Devs! I am a software developer from Nepal.