Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Manage Docker Swarm Cluster

DZone 's Guide to

Manage Docker Swarm Cluster

These code scripts should help you get started with setting up and managing Docker Swarm.

· Cloud Zone ·
Free Resource

In this article, I am listing the scripts to install Docker and setup Docker Swarm commands. Some of the commands may help you manage your Docker Swarm nodes, services and containers.

Docker-CE Installation

On Ubuntu 16.04

Using these commands, you can install Docker CE on Ubuntu 16.04.

# Install docker-ce supporting packages
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

# Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Verify that you now have the key with the fingerprint
sudo apt-key fingerprint 0EBFCD88

#Use the following command to set up the stable repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt-get update

# List the versions available in your repo
apt-cache madison docker-ce

# Install a specific version using the version string from the second column, for example, 
sudo apt-get install docker-ce=18.03.0~ce-0~ubuntu

# Verify that Docker CE is installed correctly by running the hello-world image.
sudo docker run hello-world


On RHEL/CentOS 7

These commands will help you to install Docker CE on RHEL/CentOS 7.

# Install updates
sudo yum update

# Remove old or existing docker packages
sudo yum remove docker docker-common docker-selinux docker-engine-selinux docker-engine docker-ce -y

# Install dependencies and utilities
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 -y

# If you get below error, execute the next command. it installs latest container-selinux.
# You can also change url to latest version
# http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-x.xx.xxx.noarch.rpm 
# Error: Package: docker-ce-xx.xx.xx.xx.x86_64 (docker-ce-stable)
#           Requires: container-selinux >= 2.9
sudo yum install http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.68-1.el7.noarch.rpm -y

# Add docker repositories to the yum repositories.
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo -y

# Now you are ready to install docker-ce
sudo yum install docker-ce -y

# After successful installation, you need to enable and start docker service.
sudo systemctl enable docker.service
sudo systemctl start docker


Manage Docker Swarm

sudo docker swarm init

Swarm initialized: current node (a35hhzdzf4g95w0op85tqlow1) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join \
--token SWMTKN-1-3k7ighcfs9352hmdfzh31t297fd8tdskg6x6oi8kpzzszznffx-6kovxm3akca2qe3uaxtu07fj3 111.111.111.111:2377


Now you are ready to set up your Docker Swarm cluster.

Some of the most useful commands are given below. The purpose of each command is described in the comments.

# Run either of the below commands on Master node to get Worker or Manager token
sudo docker swarm join-token manager # return a Token to join as a master
sudo docker swarm join-token worker # return a Token to join as a worker

# To create slave nodes After running above command, you will get an output something like this
sudo docker swarm join --token SWMTKN-1-4fpn76698rvexdvfftnt37rx9hqwwwadeuup05zxxuqf2ihfcw-bg44jj84tmpclw4me1yr0oqep 172.31.89.230:2377

# To check list of nodes
sudo docker node ls

# To leave a swarm mode
sudo docker swarm leave

#To create a docker service e.g. Nginx
sudo docker service create -p 8080:80 --name webserver nginx

#To list services
sudo docker service ls
sudo docker service rm <service_id>

#Deploying image from docker hub to docker container
sudo docker run -d -p 80:8761 --name eureka-server arunsharma/eureka-server

#Deploying same image on docker swarm
sudo docker service create -p 80:8761 --name eureka-server arunsharma/eureka-server

#Promote or demote a node
sudo docker node demote node-3 node-2
sudo docker node promote node-3 node-2


Finally, here's how to create a Portainer UI container to monitor the docker swarm cluster from a user interface.

#Create a volume for Portainer data. 
sudo docker volume create portainer_volume

# Creating and running Portainer as a service on Docker Swarm
sudo docker service create --name portainer --publish 9000:9000 --replicas=1 --constraint 'node.role == manager' --mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock --mount type=volume,src=portainer_volume,dst=/data portainer/portainer -H unix:///var/run/docker.sock


Topics:
docker ,microservice ,docker swarm cluster ,docker swarm ,docker ce

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}