This tutorial describes how to setup Docker Swarm on Ubuntu 20.04 and install a NTP service on it.
Docker
Docker Swarm is a native clustering system for Docker containers. It allows you to run your containers in a swarm of hosts, providing high availability, load balancing, and scalability without having to worry about managing any infrastructure.
The first step is to install Docker and Docker Compose on the machine that you want to use as a host for your swarm. You can follow the official installation instructions here: https://docs.docker.com/engine/installation/.
The next thing we need to do is add this docker engine to a swarm by running:
docker swarm init
This command will create a Docker Swarm with one node, which will be the machine running our container.
You can verify this by running:
docker swarm list
Cturra
Chrony is a powerful implementation of the Network Time Protocol (NTP). It can synchronise your system clock to NTP servers, reference clocks like GPS receivers, or even manual inputs from wristwatch and keyboard.
It can serve as an NTPv4 server (RFC 5905) and peer with other computers to provide a time service.
Once this has been done, we will create our first container.
This container runs chrony on a lightweight distribution of Linux called Alpine.
First create the docker file:
vim docker-compose.yml
Set it to the following and edit the environmental variables as needed.
version: '3.9'
services:
ntp:
build:
image: cturra/ntp:latest
container_name: ntp
restart: always
ports:
- 123:123/udp
environment:
- NTP_SERVERS=time.cloudflare.com
- LOG_LEVEL=0
To Deploy as a Swarm:
# deploy ntp stack to the swarm
$> docker stack deploy -c docker-compose.yml cturra
# check that service is running
$> docker stack services cturra
# (optional) view the ntp logs
$> docker service logs -f cturra_ntp
# one liner
sudo docker service create \ -p 12345:12345/udp \ -p 12345:12345/tcp \ --name ntp \ cturra/ntp \ -gid 1000 \ -rm \ --restart always
Alternative
Once you've got Docker up and running, let's start by creating a new project directory:
$ mkdir docker-ntp && cd docker-ntp
Next, we need to create an overlay network so that containers can communicate with each other:
$ docker network create -d overlay ntp_network
Now let's create our first container:
$ docker run -d --name ntp1 --net ntp_network -e TZ=America/New_York -e NTP_SERVER="pool.ntp.org" -e NTP_KEY="123456789" eulerian/alpine-ntp
The above command will start an NTP server in our newly created container called 'ntp1' which will be accessible from any other container in our 'ntp_network' overlay network. The server will use pool.ntp.org
Bonus
If you want to add more NTP servers to your cluster, just add them as environment variables in your Dockerfile:
-e TZ=America/New_York -e NTP_SERVER="pool.ntp.org" -e NTP_KEY="123456789"
-e TZ=Europe/London -e NTP_SERVER="pool.uk.ntp.org" -e NTP_KEY="123456789"