Bookmark

Healthchecks trong Docker: giám sát cron job bằng ping HTTP

Healthchecks nhận HTTP request hoặc email “ping” từ cron job và gửi cảnh báo khi ping không tới đúng hạn. Tutorial 0813 chạy Healthchecks cùng PostgreSQL trong Docker, sau đó tạo tài khoản admin ở cổng 8000.[12]

Youtube video player

Video gốc của i12bretro được giữ ở YouTube.[17]

1. Cài Docker

  1. Đăng nhập thiết bị Linux.[12]
  2. Chạy các lệnh:[12]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# install prerequisites
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent -y
# add docker gpg key
curl -fsSL https://download.docker.com/linux/$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release)/gpg | sudo apt-key add -
# add docker software repository
sudo add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release) $(lsb_release -cs) stable"
# install docker
sudo apt install docker-ce docker-compose containerd.io -y
# enable and start docker service
sudo systemctl enable docker && sudo systemctl start docker
# add the current user to the docker group
sudo usermod -aG docker $USER
# reauthenticate for the new group membership to take effect
su - $USER

Đăng nhập lại sau khi group Docker có hiệu lực rồi kiểm tra daemon.[12]

2. Tạo thư mục, network và file .env

  1. Tạo thư mục làm việc, network Docker, tải cấu hình cơ sở và sinh chuỗi random 32 ký tự bằng các lệnh sau.[12]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# create working directories
mkdir ~/docker/postresql -p && mkdir ~/docker/healthchecks -p
# create containers network
docker network create containers
# download the base configuration
wget -O ~/docker/healthchecks/.env https://raw.githubusercontent.com/healthchecks/healthchecks/master/docker/.env.example
# generate a 32 character random string
head /dev/urandom | LC_ALL=C tr -dc 'A-Za-z0-9' | head -c 32
# copy the output string to the clipboard
# edit the .env file
nano ~/docker/healthchecks/.env
  1. Scroll qua file .env và cập nhật ít nhất các key/value sau.[12]

Tên postresql ở đường dẫn được giữ đúng theo tutorial. Tôi có thể sửa chính tả thành thư mục khác nếu muốn, nhưng phải sửa đồng nhất cả volume command sau đó.[12]

Trong .env, scroll qua file và điền các giá trị riêng cho database, email và secret key sau:[12]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
ALLOWED_HOSTS=*
---
DB=postgres
DB_HOST=postgres
DB_NAME=healthchecks
DB_PASSWORD=CHANGE_ME_DB_PASSWORD
DB_USER=healthchecks_rw
---
DEFAULT_FROM_EMAIL=healthchecks@i12bretro.local
---
EMAIL_HOST=smtp.i12bretro.local
EMAIL_HOST_PASSWORD=
EMAIL_HOST_USER=
EMAIL_PORT=25
EMAIL_USE_TLS=False
EMAIL_USE_VERIFICATION=False
---
SECRET_KEY=<random string from the command above>

ALLOWED_HOSTS=* là giá trị nguồn yêu cầu cập nhật; khi triển khai thật, tôi cân nhắc hostname cụ thể. SECRET_KEY phải là random string riêng và DB_PASSWORD phải khớp password dùng cho PostgreSQL.[12]

  1. Nhấn CTRL+W, tìm localhost.[12]
  2. Đổi tất cả reference localhost thành DNS hoặc IP của Docker host, ví dụ đặt SITE_ROOT=http://ubuntuser.local:8000 hoặc SITE_ROOT=http://ubuntuserver.local:8000 thay cho SITE_ROOT=http://localhost:8000.[12]
  3. Nhấn CTRL+O, Enter, CTRL+X để ghi file .env.[12]

3. Chạy PostgreSQL và Healthchecks

  1. Sau khi điền .env, tiếp tục với các lệnh tạo PostgreSQL, Healthchecks và admin.[12]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# set owner of working directories
sudo chown "$USER":"$USER" ~/docker -R
# run the postgresql docker container
docker run -d --name postgres -e POSTGRES_USER=healthchecks_rw -e POSTGRES_PASSWORD='CHANGE_ME_DB_PASSWORD' -e POSTGRES_DB=healthchecks -v /home/$USER/docker/postresql:/var/lib/postgresql/data --network containers --restart=unless-stopped postgres
# run the healthchecks container
docker run -d --name healthchecks --env-file ~/docker/healthchecks/.env -p 8000:8000 --network containers --restart=unless-stopped healthchecks/healthchecks
# connect to healthchecks container shell
docker exec -ti healthchecks /bin/bash
# create an admin user
/opt/healthchecks/manage.py createsuperuser
# enter an email address
# enter and confirm a password
# exit the container shell
exit

Mật khẩu PostgreSQL trong command phải giống DB_PASSWORD trong .env; tôi tạo một mật khẩu riêng rồi điền cùng giá trị vào hai vị trí. Email và password admin nhập tương tác ở createsuperuser.[12]

4. Đăng nhập và tạo ping

  1. Mở trình duyệt tới http://DNSorIP:8000.[12]
  2. Đăng nhập bằng tài khoản admin vừa tạo.[12]
  3. Kiểm tra dashboard Healthchecks.[12]
  4. Tạo check cho cron job và dùng ping URL do dashboard cấp trong job thật.[12]

Kiểm tra và giới hạn

Tôi kiểm tra docker ps, docker logs postgres, docker logs healthchecks, database volume và network containers. Nếu đổi SITE_ROOT, email hoặc DB password, restart container và đọc log thay vì đoán. Không mở port PostgreSQL ra WAN; Healthchecks có 2FA/WebAuthn và team features theo mô tả nguồn, nhưng bảo vệ dashboard vẫn là trách nhiệm của deployment.[12]

Video gốc của tutorial: [xem trên YouTube]1.[17]

Nguồn

Tài liệu gốc: [Running Healthchecks - A Cron Job Monitoring Service - In Docker]2, đăng/cập nhật 2024-07-28.[12]

[12] https://i12bretro.github.io/tutorials/0813.html — Running Healthchecks in Docker [17] https://www.youtube.com/watch?v=4l57CgiHKRI — Running Healthchecks — A Cron Job Monitoring Service — In Docker — i12bretro video


  1. https://www.youtube.com/watch?v=4l57CgiHKRI — video chính thức của i12bretro. ↩︎

  2. https://i12bretro.github.io/tutorials/0813.html — Running Healthchecks - A Cron Job Monitoring Service - In Docker. ↩︎


0 Bình luận

Góp Ý / Bình Luận / Đánh giá