NTP Server Installation

The NTP server is used to synchronize time, which causes frequent bugs in the program. We got a sudden demand last week, so get one up.

1. The selection of

First, choose the right image. Due to time constraints, choose the most popular image on Hub.docker

2. View official documents

Docker-compose: docker-compose: docker-compose: docker-compose: Docker-compose: Docker-compose: Docker-compose: Docker-compose

From here, go directly to the code repository to view the code

Find the docker-compose file and copy it locally to make changes

First of all, build is definitely not needed, we do not need to rebuild the image, we can directly use the image constructed in the Docker warehouse. Secondly, the UPSTREAM NTP server should be changed to the available NTP server in China, and finally, add the local time zone.The modified YML file is as follows

version: '3.4'
services:
  ntp:
    image: cturra/ntp:latest
    container_name: ntp
    restart: always
    ports:
      - 123:123/udp
    read_only: true
    tmpfs:
      - /etc/chrony:rw,mode=1750
      - /run/chrony:rw,mode=1750
      - /var/lib/chrony:rw,mode=1750
    environment:
      - NTP_SERVERS=ntp1.aliyun.com,ntp2.aliyun.com,ntp3.aliyun.com,ntp4.aliyun.com
      - LOG_LEVEL=0
    volumes:
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
Copy the code

3. Pull the image and start it

Docker-comemage. yml copy docker-comemage. yml to the server and pull the image. Docker-compose up -d then starts the container

4. Test

On another machine, run yum install ntpdate to install ntpdate, and then run the ntpdate IP command to test whether the synchronization is normal.

After the test, the NTP service works properly and the machine time has been synchronized.

5. Use a Linux scheduled task to periodically synchronize time

  1. Run the service crond start command to enable the scheduled task service.
  2. Run the crontab -e command to edit scheduled tasks. To add a scheduled task, run the following command: cron[command], save the configuration, and exit the configuration file

Example: * * * * * / usr/sbin/ntpdate 192.168.30.1 > > / opt/app/NTP. Log 2 > &1