Mattermost ChatOps
On-premise Slack — ChatOps: nhận alert Prometheus, Wazuh, ArgoCD.
Bối cảnh & Lý do triển khai
Hạ tầng hiện có Prometheus, Wazuh, Gitea, ArgoCD, TheHive — mỗi service gửi alert/notification riêng lẻ qua email hoặc không gửi gì cả. Mattermost cung cấp một kênh duy nhất để team nhận tất cả notifications theo context: #alerts cho Grafana, #security cho Wazuh/CrowdSec, #deployments cho Gitea/ArgoCD, #incidents cho TheHive. Ngoài ra Mattermost là công cụ chat nội bộ tự host, không phụ thuộc Slack/Teams — phù hợp cho lab enterprise self-contained.
| Mục | Nội dung |
|---|---|
| Ứng dụng trong doanh nghiệp | Team chat nội bộ + aggregation point cho tất cả DevOps/security alerts |
| Tác dụng chính | Một màn hình xem tất cả event hạ tầng, phối hợp incident response real-time |
| Nếu không triển khai | Alert rải rác qua email, không có kênh giao tiếp team real-time |
| Các service phụ thuộc vào đây | Prometheus Alertmanager, Wazuh, Gitea, ArgoCD, TheHive/CrowdSec (send notifications) |
Phiên bản phần mềm sử dụng (đã xác minh ngày 2026-10-15; cập nhật 2026-10-15; số bản sẽ tăng theo thời gian, bài ghim bản này để các bước khớp nhau):
| Thành phần | Phiên bản dùng trong bài | Ghi chú |
|---|---|---|
| Ubuntu Server (VM272) | 24.04.5 LTS "Noble Numbat" | ISO ubuntu-24.04.5-live-server-amd64.iso (phát hành 2026-09-09); kernel 6.8; hỗ trợ chuẩn tới 04/2029 |
| Docker Engine | 29.9.1 (repo download.docker.com, suite noble, gói ngày 2026-10-14) | Gói docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin, docker-compose-plugin 5.6.0; dùng lệnh docker compose |
| Mattermost | mattermost/mattermost-enterprise-edition:11.7.12 (2026-10-08) | Thay cho bản 9.11 của bài cũ (đã hết hỗ trợ 2025-05-15); nhánh 11.7 được hỗ trợ bảo mật tới 2027-05-15; chạy không license = gói Entry (miễn phí), xem ghi chú bên dưới |
| PostgreSQL | 16 (postgres:16-alpine) | Cùng nhánh 16 với gói mặc định của Ubuntu 24.04; Mattermost yêu cầu PostgreSQL từ 14 |
| Nginx | nginx:1.30-alpine (nhánh stable, 1.30.6) | Thay cho 1.25 (nhánh mainline cũ, không còn bản vá) |
| step CLI | 0.31.0 (step-cli_0.31.0-1_amd64.deb) | Giá trị dùng chung của cả series |
| node_exporter | 1.12.1 | Giá trị dùng chung của cả series, cổng 9100 |
| proxmox-backup-client | Gói proxmox-backup-client-static từ repo pbs-client, suite trixie | Bản static, không phụ thuộc glibc của Ubuntu 24.04; cần kiểm lại trên máy thật |
| Authentik (VM234) | Theo bài 14 (2025.10 trở lên) | Mattermost dùng OAuth 2.0 qua tuỳ chọn GitLab, xem mục 4.3 |
| Prometheus + Alertmanager (VM235) | Theo bài 15 | Alertmanager gửi vào #alerts bằng slack_configs, xem mục 4.4 |
mattermost-team-edition bị gỡ GitLab SSO (và giới hạn 250 người dùng), trong khi bài này đăng nhập Authentik qua tuỳ chọn GitLab (mục 4.3). Image mattermost-enterprise-edition chạy không license ở chế độ Entry (miễn phí) vẫn có GitLab SSO theo chuẩn OAuth 2.0. (3) Đổi lại, Entry giới hạn lịch sử hiển thị 10.000 tin nhắn kênh (tin cũ hơn vẫn nằm trong database nhưng không xem/tìm được) và chỉ có hỗ trợ cộng đồng; với lab nhỏ thì chấp nhận được. Nếu không cần SSO thì dùng mattermost/mattermost-team-edition:11.7.12 thay thế. Nguồn: forum.mattermost.com (Mattermost v11 Changes in Free Offerings), docs.mattermost.com (Editions and Offerings, GitLab Single Sign-On). Cần kiểm lại trên máy thật: màn hình System Console của gói Entry ở 11.7.12 và việc cổng 8067 (/metrics) có hoạt động khi không có license hay không (mục 4.8).
Danh sách máy chủ & IP liên quan
Bảng dưới liệt kê các máy chủ mà bài này tạo hoặc kết nối tới. Địa chỉ, VMID, VLAN và node lấy từ bảng quy hoạch chung của lab, dùng thống nhất cho cả bộ bài lab.
| Máy chủ | IP | VLAN · Node | Vai trò trong bài này |
|---|---|---|---|
| mattermost (Mattermost) · VM272 | 10.11.65.57 | 65 (SRV) · node1 | Máy chủ của bài — chạy Ubuntu Server 24.04, Mattermost 11.7 (image enterprise-edition, gói Entry), PostgreSQL 16 và Nginx bằng Docker Compose; HTTPS cổng 443, ứng dụng cổng 8065. |
| node1 (Proxmox VE) | 10.11.45.210 | 45 (MGMT) · cluster HOATRANHUB | Node Proxmox chứa VM272; tạo VM Ubuntu Server 24.04 (scsi0 30 GB trên SSD vm-ceph) tại đây. |
| pihole (Pi-hole DNS) · VM247 | 10.11.45.53 | 45 (MGMT) · node3 | DNS của VM và phân giải mattermost.hoatranlabs.local về 10.11.65.57, thêm bằng Admin UI hoặc add_dns_records.sh (bài 12). |
| step-ca (step-CA PKI) · VM242 | 10.11.25.42 | 25 (SEC) · node1 | Cấp chứng chỉ TLS cho Nginx của Mattermost bằng step ca certificate (2160h), gia hạn bằng cron step ca renew (bài 13). |
| mailu (Mailu Mail Server) · VM246 | 10.11.35.46 | 35 (DMZ) · node2 | Relay SMTP cổng 587 (STARTTLS) gửi email của Mattermost, đăng nhập bằng tài khoản [email protected] (bài 16). |
| authentik (Authentik SSO) · VM234 | 10.11.25.40 | 25 (SEC) · node1 | Nhà cung cấp OAuth2/OpenID cho nút Sign in with GitLab của Mattermost (scope mapping read_user), redirect URI /login/gitlab/complete và /signup/gitlab/complete (bài 14). |
| prometheus (Prometheus + Alertmanager) · VM235 | 10.11.45.35 | 45 (MGMT) · node2 | Alertmanager (cổng 9093) gửi cảnh báo critical/warning vào #alerts; Prometheus scrape node_exporter cổng 9100 (metrics app cổng 8067 chỉ khi có license Enterprise) (bài 15). |
| wazuh-siem (Wazuh SIEM) · VM280 | 10.11.45.25 | 45 (MGMT) · node1 | Manager gửi cảnh báo mức 12 trở lên vào #security qua integration slack (hook_url) trong wazuh_manager.conf (bài 17). |
| crowdsec (CrowdSec LAPI) · VM271 | 10.11.35.20 | 35 (DMZ) · node3 | Gửi cảnh báo ban IP vào kênh #security bằng webhook của Mattermost; phần cấu hình nằm ở bài 36. |
| gitea (Gitea + Actions) · VM240 | 10.11.45.40 | 45 (MGMT) · node2 | Webhook Push, Pull request và Release gửi thông báo vào kênh #deployments của Mattermost (bài 22). |
| thehive (TheHive + Cortex) · VM270 | 10.11.25.11 | 25 (SEC) · node2 | Notification HTTP khi tạo case có severity từ Medium trở lên gửi vào kênh #incidents (bài 35). |
| pbs01 (PBS Primary) · VM101 | 10.11.45.17 | 45 (MGMT) · node4 | Nhận backup /opt/mattermost/app, db, nginx bằng proxmox-backup-client, datastore syno-primary, namespace vm-mattermost (bài 04). |
Gateway của mỗi VLAN là địa chỉ .1. Tên DNS nội bộ do Pi-hole phân giải (bài 12). Tên in đậm là hostname; FQDN = hostname + .hoatranlabs.local.
Thông tin VM
| Thông số | Giá trị |
|---|---|
| VMID | 272 |
| Hostname | mattermost |
| IP | 10.11.65.57/24 |
| Gateway | 10.11.65.1 |
| VLAN | 65 (VLAN_SRV) |
| vCPU | 2 |
| RAM | 4 GB |
| Disk | 30 GB (scsi0, SSD vm-ceph) |
| OS | Ubuntu Server 24.04.5 LTS |
| Phân bổ node | node1 |
create-lab-vms.sh thì scsi0 = 30 GB trên SSD vm-ceph, không có đĩa dữ liệu scsi1 (PostgreSQL /opt/mattermost/db/data cần IOPS ngẫu nhiên; file đính kèm /opt/mattermost/app/data của lab ít nên ở lại SSD); số 30 GB khớp vms.csv và chỉ là trần danh nghĩa (thin provisioning), môi trường thật nên cấp lớn hơn (bài từng ghi 50 GB). Vì vậy bài hạ cache Nginx xuống max_size=1g (mục 2.4) và chỉ giữ bản pg_dump 7 ngày (mục 4.9).
Tạo VM trên Proxmox
Tải Ubuntu ISO (nếu chưa có) · Tạo VM · Cài đặt Ubuntu Server 24.04
1.1 Tải Ubuntu ISO (nếu chưa có)
# Chạy trên node1. Nếu thư mục 24.04 đã có bản point mới hơn 24.04.5, thay tên file cho khớp: https://releases.ubuntu.com/24.04/
cd /var/lib/vz/template/iso
wget https://releases.ubuntu.com/24.04/ubuntu-24.04.5-live-server-amd64.iso
wget -O /tmp/SHA256SUMS https://releases.ubuntu.com/24.04/SHA256SUMS
sha256sum -c /tmp/SHA256SUMS --ignore-missing # Kết quả mong đợi: ubuntu-24.04.5-live-server-amd64.iso: OK
1.2 Tạo VM
Cách 1: Web UI Proxmox (GUI)
- VM ID:
272 - Name:
mattermost - Bỏ tick Start after created
- Storage:
local(hoặc nơi chứa ISO) - ISO Image:
ubuntu-24.04.5-live-server-amd64.iso - Guest OS Type:
Linux, Version:6.x - 2.6 Kernel
- Machine:
q35 - SCSI Controller:
VirtIO SCSI single - BIOS:
SeaBIOS - Qemu Agent: ☑
- Bus/Device:
SCSI 0, Storage:vm-ceph - Disk size:
30 GiB, Cache:Write back, Discard: ☑
- Sockets:
1, Cores:2, Type:x86-64-v2-AES
- Memory (MiB):
4096
- Bridge:
vmbr0, VLAN Tag:65, Model:VirtIO (paravirtualized), Firewall: ☐
Cách 2: CLI (qm create)
qm create 272 --name mattermost --memory 4096 --cores 2 --sockets 1 \
--net0 virtio,bridge=vmbr0,tag=65 \
--scsi0 vm-ceph:30,cache=writeback \
--scsihw virtio-scsi-single \
--ide2 local:iso/ubuntu-24.04.5-live-server-amd64.iso,media=cdrom \
--boot "order=ide2;scsi0" \
--ostype l26 --machine q35 --bios seabios \
--agent 1 --serial0 socket --vga serial0
qm start 272
qm terminal 272), dừng VM, đổi --vga serial0 thành --vga std (qm set 272 --vga std) và cài qua tab Console (noVNC) của Web UI. Cần kiểm lại trên máy thật.
1.3 Cài đặt Ubuntu Server 24.04
Theo dõi console qua qm terminal 272:
- Chọn Ubuntu Server (không chọn bản minimized); nếu có màn hình Ubuntu Pro thì chọn Skip for now; không chọn snap nào ở màn hình Featured Server Snaps
- Username:
sysadmin, hostname:mattermost - SSH: Install OpenSSH server ✓
- Mạng: để DHCP tạm trong trình cài đặt, địa chỉ tĩnh đặt ở mục 1.4
1.4 Cấu hình sau cài đặt
# Chạy bằng root (sudo -i)
export NEEDRESTART_MODE=a # Ubuntu 24.04: needrestart tự khởi động lại dịch vụ, không hiện hộp thoại khi apt
apt update && apt upgrade -y
apt install -y qemu-guest-agent curl wget git vim net-tools ufw ca-certificates
# Docker Engine từ repo chính thức (suite noble, dạng deb822); gỡ gói Docker không chính thức nếu có
apt remove -y $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc 2>/dev/null | cut -f1)
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
# Heredoc KHÔNG đặt nháy quanh EOF để $(...) tự điền codename "noble"
cat > /etc/apt/sources.list.d/docker.sources << EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF
apt update
apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Trên Ubuntu 24.04 qemu-guest-agent là unit tĩnh: udev tự kích hoạt khi VM có kênh virtio-serial
# (Options → QEMU Guest Agent = Enabled). "enable" có thể báo "no installation config", không phải lỗi.
systemctl start qemu-guest-agent
systemctl enable --now docker
usermod -aG docker sysadmin
# Static IP — kiểm tra tên NIC trước: VM q35 thường là enp6s18, máy i440fx là ens18
ip -br link
# Ubuntu 24.04 (Subiquity + cloud-init) ghi sẵn /etc/netplan/50-cloud-init.yaml dùng DHCP.
# Tắt phần cấu hình mạng của cloud-init, bỏ file đó và dùng một file tĩnh duy nhất để hai file không chồng nhau.
cat > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg << 'EOF'
network: {config: disabled}
EOF
mv /etc/netplan/50-cloud-init.yaml /root/50-cloud-init.yaml.bak 2>/dev/null || true
cat > /etc/netplan/01-netcfg.yaml << 'EOF'
network:
version: 2
ethernets:
enp6s18: # thay bằng tên NIC thật từ ip -br link
dhcp4: false
addresses: [10.11.65.57/24]
routes:
- to: default
via: 10.11.65.1
nameservers:
addresses: [10.11.45.53]
search: [hoatranlabs.local]
EOF
chmod 600 /etc/netplan/01-netcfg.yaml # netplan 1.0 cảnh báo khi file cấu hình cho phép người khác đọc
netplan apply
# Firewall
ufw default deny incoming
ufw default allow outgoing
ufw allow from 10.11.0.0/16 to any port 22
ufw allow from 10.11.0.0/16 to any port 80
ufw allow from 10.11.0.0/16 to any port 443
ufw allow from 10.11.0.0/16 to any port 8065 # Mattermost direct (debug)
ufw allow from 10.11.45.35 to any port 8067 proto tcp # Prometheus (VM235) scrape metrics app, mục 4.8
ufw --force enable
Cài đặt Mattermost
Cấu trúc thư mục · Lấy TLS cert từ step-CA · Docker Compose
2.1 Cấu trúc thư mục
mkdir -p /opt/mattermost/{app,db,nginx}
mkdir -p /opt/mattermost/app/{data,logs,config,plugins,client/plugins}
mkdir -p /opt/mattermost/db/data
mkdir -p /opt/mattermost/nginx/certs
# Permissions — Mattermost chạy với UID 2000
chown -R 2000:2000 /opt/mattermost/app
chown -R 999:999 /opt/mattermost/db/data
2.2 Lấy TLS cert từ step-CA
# Cài step CLI 0.31.0 (gói .deb của smallstep/cli, binary nằm ở /usr/bin/step) — bỏ qua nếu VM đã có
STEP_VERSION="0.31.0"
wget "https://github.com/smallstep/cli/releases/download/v${STEP_VERSION}/step-cli_${STEP_VERSION}-1_amd64.deb"
dpkg -i "step-cli_${STEP_VERSION}-1_amd64.deb"
rm -f "step-cli_${STEP_VERSION}-1_amd64.deb"
step ca bootstrap --ca-url https://step-ca.hoatranlabs.local:9000 --fingerprint <FINGERPRINT>
step ca certificate mattermost.hoatranlabs.local \
/opt/mattermost/nginx/certs/mattermost.crt \
/opt/mattermost/nginx/certs/mattermost.key \
--san mattermost.hoatranlabs.local \
--san 10.11.65.57 \
--ca-url https://step-ca.hoatranlabs.local:9000 \
--not-after 2160h
2.3 Docker Compose
cat > /opt/mattermost/docker-compose.yml << 'EOF'
# Compose v2 (docker-compose-plugin) không còn dùng khóa "version:" nên không khai báo
services:
# ===== PostgreSQL 16 =====
postgres:
image: postgres:16-alpine
container_name: mattermost-db
restart: unless-stopped
environment:
POSTGRES_USER: mattermost
POSTGRES_PASSWORD: Hoatran@2024!
POSTGRES_DB: mattermost
volumes:
- /opt/mattermost/db/data:/var/lib/postgresql/data
networks:
- mm-net
healthcheck:
test: ["CMD-SHELL", "pg_isready -U mattermost"]
interval: 10s
timeout: 5s
retries: 5
shm_size: 256mb
# ===== Mattermost 11.7 (image enterprise-edition, không license = gói Entry; xem phần Tổng quan) =====
mattermost:
image: mattermost/mattermost-enterprise-edition:11.7.12
container_name: mattermost-app
restart: unless-stopped
depends_on:
postgres:
condition: service_healthy
environment:
MM_SQLSETTINGS_DRIVERNAME: postgres
MM_SQLSETTINGS_DATASOURCE: >-
postgres://mattermost:Hoatran%402024%21@postgres:5432/mattermost?sslmode=disable&connect_timeout=10
MM_BLEVESETTINGS_INDEXDIR: /mattermost/bleve-indexes
MM_SERVICESETTINGS_SITEURL: https://mattermost.hoatranlabs.local
MM_SERVICESETTINGS_LISTENADDRESS: :8065
MM_SERVICESETTINGS_ALLOWEDUNTRUSTEDINTERNALCONNECTIONS: "10.11.0.0/16 172.0.0.0/8 127.0.0.1"
MM_SERVICESETTINGS_ENABLELOCALMODE: "true"
MM_TEAMSETTINGS_SITENAME: HoatranLabs
MM_TEAMSETTINGS_MAXUSERSPERTEAM: 100
# Email via Mailu
MM_EMAILSETTINGS_SMTPSERVER: mailu.hoatranlabs.local
MM_EMAILSETTINGS_SMTPPORT: "587"
MM_EMAILSETTINGS_CONNECTIONSECURITY: STARTTLS
MM_EMAILSETTINGS_ENABLESMTPAUTH: "true"
MM_EMAILSETTINGS_SMTPUSERNAME: [email protected]
MM_EMAILSETTINGS_SMTPPASSWORD: Noreply@Mailu2024!
MM_EMAILSETTINGS_FEEDBACKEMAIL: [email protected]
MM_EMAILSETTINGS_REPLYTOADDRESS: [email protected]
MM_EMAILSETTINGS_FEEDBACKNAME: HoatranLabs Mattermost
# Logging
MM_LOGSETTINGS_CONSOLELEVEL: INFO
MM_LOGSETTINGS_FILELEVEL: INFO
MM_LOGSETTINGS_ENABLEFILE: "true"
MM_LOGSETTINGS_FILELOCATION: /mattermost/logs
# File storage
MM_FILESETTINGS_DIRECTORY: /mattermost/data/
# Plugin settings
MM_PLUGINSETTINGS_ENABLE: "true"
MM_PLUGINSETTINGS_ENABLEUPLOADS: "true"
# Performance
MM_CACHEAHEAD_MAXCACHESIZE: "10"
volumes:
- /opt/mattermost/app/data:/mattermost/data
- /opt/mattermost/app/logs:/mattermost/logs
- /opt/mattermost/app/config:/mattermost/config
- /opt/mattermost/app/plugins:/mattermost/plugins
- /opt/mattermost/app/client/plugins:/mattermost/client/plugins
# Root CA của step-CA (Bài 13, Bước 9: distribute-root-ca.sh; file phải có trước khi docker compose up).
# Mount vào /etc/ssl/certs (cách docs Mattermost dùng cho SSO với CA nội bộ) để Mattermost tin cậy
# HTTPS của Authentik (mục 4.3) và STARTTLS của Mailu (SMTP).
- /usr/local/share/ca-certificates/hoatranlabs-root.crt:/etc/ssl/certs/hoatranlabs-root.crt:ro
ports:
- "8065:8065"
- "8067:8067" # metrics app cho Prometheus (mục 4.8), chỉ có tác dụng khi bật Performance Monitoring
networks:
- mm-net
healthcheck:
test: ["CMD-SHELL", "curl -sf http://localhost:8065/api/v4/system/ping || exit 1"]
interval: 30s
timeout: 10s
retries: 5
start_period: 60s
# ===== Nginx reverse proxy =====
nginx:
image: nginx:1.30-alpine
container_name: mattermost-nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- /opt/mattermost/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- /opt/mattermost/nginx/certs:/etc/nginx/certs:ro
networks:
- mm-net
depends_on:
mattermost:
condition: service_healthy
networks:
mm-net:
driver: bridge
ipam:
config:
- subnet: 172.21.0.0/24
EOF
2.4 Nginx configuration
cat > /opt/mattermost/nginx/nginx.conf << 'EOF'
worker_processes auto;
error_log /var/log/nginx/error.log warn;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
upstream mattermost {
server mattermost-app:8065;
keepalive 64;
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mattermost_cache:10m
max_size=1g inactive=120m use_temp_path=off; # lab đĩa 30 GB: cache tối đa 1 GB
server {
listen 80;
server_name mattermost.hoatranlabs.local;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
http2 on; # nginx từ 1.25.1 dùng chỉ thị riêng, "listen ... http2" đã bị đánh dấu lỗi thời
server_name mattermost.hoatranlabs.local;
ssl_certificate /etc/nginx/certs/mattermost.crt;
ssl_certificate_key /etc/nginx/certs/mattermost.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_session_timeout 1d;
ssl_session_cache shared:MattermostSSL:50m;
ssl_stapling on;
# Security headers
add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;
add_header X-XSS-Protection "1; mode=block";
client_max_body_size 50m;
location ~ /api/v[0-9]+/(users/)?websocket$ {
proxy_pass http://mattermost;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
location / {
proxy_pass http://mattermost;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_read_timeout 600s;
proxy_buffering off;
}
}
}
EOF
2.5 Khởi động stack
cd /opt/mattermost
docker compose up -d postgres
sleep 15 # Chờ PostgreSQL ready
docker compose up -d mattermost
sleep 60 # Chờ Mattermost init DB (~1 phút lần đầu)
docker compose up -d nginx
docker compose ps
docker compose logs mattermost --tail=30
Cấu hình ban đầu
Tạo admin account · Tạo channels · Tạo Incoming Webhooks cho mỗi channel
3.1 Tạo admin account
Truy cập https://mattermost.hoatranlabs.local:
- Email:
[email protected] - Username:
admin - Password:
Hoatran@2024!
HoatranLabs3.2 Tạo channels
Vào HoatranLabs team → + Add channels:
| Channel | Mô tả | Loại |
|---|---|---|
alerts | Prometheus/Grafana infrastructure alerts | Public |
security | Wazuh + CrowdSec security alerts | Private |
deployments | ArgoCD + Gitea CI/CD notifications | Public |
incidents | TheHive case notifications | Private |
general | Team communication | Public |
3.3 Tạo Incoming Webhooks cho mỗi channel
System Console → Integrations → Incoming Webhooks → Add Incoming Webhook:
Tạo webhook cho từng channel, lưu lại URLs:
-
https://mattermost.hoatranlabs.local/hooks/<id-alerts>→ dùng cho Prometheus -
https://mattermost.hoatranlabs.local/hooks/<id-security>→ dùng cho Wazuh/CrowdSec -
https://mattermost.hoatranlabs.local/hooks/<id-deployments>→ dùng cho Gitea/ArgoCD -
https://mattermost.hoatranlabs.local/hooks/<id-incidents>→ dùng cho TheHive
Tích hợp với hạ tầng
Pi-hole DNS · step-CA PKI — cert renewal · Authentik SSO — GitLab-compatible OAuth
4.1 Pi-hole DNS
# Thêm DNS record mattermost.hoatranlabs.local → 10.11.65.57
# Cách 1: Pi-hole Admin UI → Settings → Local DNS Records (Domain: mattermost.hoatranlabs.local, IP: 10.11.65.57)
# Cách 2: SSH vào VM Pi-hole (chạy Docker), thêm dòng add_dns "mattermost.hoatranlabs.local" "10.11.65.57"
# vào /opt/pihole/scripts/add_dns_records.sh (Bài 12) rồi chạy lại script
ssh [email protected]
4.2 step-CA PKI — cert renewal
# Cronjob tự renew cert 30 ngày trước hết hạn
cat > /etc/cron.d/mattermost-cert-renew << 'EOF'
0 3 * * * root step ca renew --force \
/opt/mattermost/nginx/certs/mattermost.crt \
/opt/mattermost/nginx/certs/mattermost.key \
--ca-url https://step-ca.hoatranlabs.local:9000 \
--expires-in 720h \
&& docker compose -f /opt/mattermost/docker-compose.yml exec nginx nginx -s reload
EOF
4.3 Authentik SSO — GitLab-compatible OAuth
Mattermost nối với Authentik bằng OAuth 2.0 qua tuỳ chọn GitLab: theo docs Mattermost, gói Free/Entry tự host dùng chuẩn OAuth 2.0 (GitLab SSO), còn chuẩn OpenID Connect riêng của Mattermost thuộc gói Professional/Enterprise. Authentik (bài 14) là nhà cung cấp OAuth2/OpenID nên chỉ cần trả về đúng bộ trường mà tuỳ chọn GitLab đọc (id, username, email, name); theo hướng dẫn tích hợp Mattermost của Authentik, việc này làm bằng một scope mapping riêng.
Bước chuẩn bị trong Authentik (VM234) — scope mapping: Customization → Property Mappings → Create → Scope Mapping, đặt Name mattermost-read-user, Scope name read_user, Expression:
username = request.user.username
return {
"id": request.user.pk,
"username": username,
"login": username,
"preferred_username": username,
"email": request.user.email,
"name": request.user.name or username,
}
Trong Authentik (VM234):
- Name:
mattermost-oauth - Client type:
Confidential - Client ID:
mattermost-client-id - Client Secret: generate (copy lại)
- Redirect URIs (kiểu Strict, thêm cả hai):
https://mattermost.hoatranlabs.local/login/gitlab/completevàhttps://mattermost.hoatranlabs.local/signup/gitlab/complete - Advanced protocol settings → Selected Scopes:
openidvàmattermost-read-user
Mattermost, Slug mattermost, Provider mattermost-oauth, Launch URL https://mattermost.hoatranlabs.local
Trong Mattermost (System Console → Authentication → GitLab; trường Application ID, Secret, Site URL và ba endpoint như dưới đây. Theo Authentik, System Console không đặt đủ endpoint ở mọi bản nên cách chắc chắn là dùng biến môi trường phía dưới hoặc config.json. Cần kiểm lại màn hình này trên máy thật với gói Entry 11.7.12):
Enable: true
Application ID: mattermost-client-id
Application Secret Key: <secret-từ-Authentik>
GitLab Site URL: https://authentik.hoatranlabs.local:9443
User API Endpoint: /application/o/userinfo/
Auth Endpoint: /application/o/authorize/
Token Endpoint: /application/o/token/
Hoặc set bằng environment variable (thêm vào docker-compose.yml):
environment:
MM_GITLABSETTINGS_ENABLE: "true"
MM_GITLABSETTINGS_ID: "mattermost-client-id"
MM_GITLABSETTINGS_SECRET: "<authentik-client-secret>"
MM_GITLABSETTINGS_AUTHENDPOINT: "https://authentik.hoatranlabs.local:9443/application/o/authorize/"
MM_GITLABSETTINGS_TOKENENDPOINT: "https://authentik.hoatranlabs.local:9443/application/o/token/"
MM_GITLABSETTINGS_USERAPIENDPOINT: "https://authentik.hoatranlabs.local:9443/application/o/userinfo/"
docker compose up -d mattermost # Restart với config mới
4.4 Prometheus Alertmanager → #alerts
# /opt/prometheus/alertmanager/alertmanager.yml (VM235, container `alertmanager`, cổng 9093 — Bài 15)
# Gộp receiver Mattermost vào khối `receivers:` và route con vào khối `route:` đã có trong file
# (receiver mặc định của Bài 15 tên `default`), không tạo khối trùng lặp:
receivers:
- name: 'mattermost-alerts'
# Incoming webhook của Mattermost nhận payload định dạng Slack => dùng slack_configs,
# api_url chính là webhook URL (webhook_configs gửi JSON riêng của Alertmanager, không có title/text)
slack_configs:
- api_url: 'https://mattermost.hoatranlabs.local/hooks/<id-alerts>'
channel: 'alerts' # tên channel (không phải display name); trùng channel của webhook ở mục 3.3
send_resolved: true
http_config:
tls_config:
ca_file: /etc/prometheus/certs/hoatranlabs-root.crt # root CA đã mount sẵn vào container alertmanager (Bài 15)
title: '[{{ .Status | toUpper }}] {{ .GroupLabels.alertname }}'
text: |-
{{ range .Alerts }}
**{{ .Labels.alertname }}** [{{ .Labels.severity | toUpper }}]
{{ .Annotations.summary }}
{{ if .Labels.instance }}Instance: `{{ .Labels.instance }}`{{ end }}
{{ end }}
route:
receiver: 'default'
group_by: ['alertname', 'cluster', 'service']
group_wait: 30s
group_interval: 5m
repeat_interval: 4h
routes:
# critical/warning gửi vào Mattermost VÀ vẫn gửi email như Bài 15 (route con khớp thì không rơi về receiver cha)
- matchers:
- severity=~"critical|warning"
receiver: 'mattermost-alerts'
continue: true
- matchers:
- severity=~"critical|warning"
receiver: 'default'
# Nạp lại cấu hình Alertmanager (Bài 15)
cd /opt/prometheus && docker compose restart alertmanager
4.5 Wazuh → #security
# Trên VM280 (Wazuh chạy Docker — Bài 17 mục 5.6): ossec.conf của manager được mount từ file trên host
# và copy vào container mỗi lần khởi động, nên sửa file này rồi restart container
cat >> /opt/wazuh-docker/single-node/config/wazuh_cluster/wazuh_manager.conf << 'EOF'
<ossec_config>
<integration>
<name>slack</name>
<hook_url>https://mattermost.hoatranlabs.local/hooks/<id-security></hook_url>
<level>12</level>
<alert_format>json</alert_format>
</integration>
</ossec_config>
EOF
docker restart single-node-wazuh.manager-1
4.6 Gitea → #deployments
Trong Gitea (VM240) → Organization Settings hoặc Repository → Settings → Webhooks → Add webhook:
- Payload URL:
https://mattermost.hoatranlabs.local/hooks/<id-deployments> - Content type:
application/json - Events: Push, Pull requests, Releases
4.7 TheHive → #incidents
# Trong TheHive → Admin → Notifications → Add
# Type: HTTP
# URL: https://mattermost.hoatranlabs.local/hooks/<id-incidents>
# Trigger: Case created với severity >= Medium
# Payload:
cat << 'EOF'
{
"text": ":fire: **New Incident** — {{ object.title }}\nSeverity: {{ object.severity }}\nStatus: {{ object.status }}\nLink: https://thehive.hoatranlabs.local/cases/{{ object.id }}"
}
EOF
4.8 Prometheus — Mattermost metrics
# Cài node_exporter trên VM272 (bỏ qua đoạn cài đặt này nếu VM272 đã cài bằng script ở Bước 5 của Bài 15)
NODE_EXPORTER_VERSION="1.12.1"
wget "https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz"
tar xzf "node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz"
mv "node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter" /usr/local/bin/
cat > /etc/systemd/system/node_exporter.service << 'EOF'
[Unit]
Description=Node Exporter
After=network.target
[Service]
Type=simple
User=nobody
ExecStart=/usr/local/bin/node_exporter --collector.systemd
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now node_exporter
# prometheus.yml (VM235):
- job_name: 'mattermost'
static_configs:
- targets: ['10.11.65.57:9100']
relabel_configs:
- target_label: instance
replacement: mattermost-vm272
# Mattermost app metrics: COMMENT sẵn vì endpoint /metrics (cổng 8067) cần license Mattermost; gói Entry không license
# có thể chưa mở endpoint này (cần kiểm lại trên máy thật). Chỉ bỏ dấu # khi `curl http://10.11.65.57:8067/metrics`
# trả về metrics sau khi đã bật Performance Monitoring (đoạn bên dưới); không cần token/params.
# - job_name: 'mattermost-app'
# static_configs:
# - targets: ['10.11.65.57:8067']
# metrics_path: /metrics
Bật metrics trong Mattermost: System Console → Environment → Performance Monitoring → Enable performance monitoring: true, Listen address: :8067
/metrics qua HTTP. Vì vậy, khi chạy image mattermost-enterprise-edition:11.7.12 chưa nhập license, hãy giữ job mattermost-app ở dạng comment như trên và chỉ bật sau khi lệnh curl -s http://10.11.65.57:8067/metrics | head (chạy từ VM235) trả về metrics (cổng 8067 đã publish ở mục 2.3, UFW mở cho VM235 ở mục 1.4 để sẵn khi cần); job mattermost (node_exporter) vẫn dùng bình thường. Cần kiểm lại trên máy thật.
4.9 PBS Backup
# Cài proxmox-backup-client trên Ubuntu 24.04 (cách chuẩn của series): repo client-only pbs-client, suite trixie,
# dạng deb822 + Signed-By. Gói proxmox-backup-client-static là bản tĩnh (chỉ phụ thuộc qrencode), nên không vướng glibc của noble.
# (Cần kiểm lại trên máy thật: apt trên Ubuntu 24.04 nhận repo suite trixie và chạy được binary static.)
wget https://enterprise.proxmox.com/debian/proxmox-archive-keyring-trixie.gpg -O /usr/share/keyrings/proxmox-archive-keyring.gpg
cat > /etc/apt/sources.list.d/pbs-client.sources << 'EOF'
Types: deb
URIs: http://download.proxmox.com/debian/pbs-client
Suites: trixie
Components: main
Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg
EOF
apt update && apt install -y proxmox-backup-client-static
cd /opt/mattermost
# 1. Dump DB bằng pg_dump (chạy online, không cần dừng Mattermost).
# Lưu trong /opt/mattermost/db/dump để nằm trong mattermost-db.pxar (không để ở /tmp vì /tmp không được backup)
mkdir -p db/dump
docker exec mattermost-db pg_dump -U mattermost mattermost \
> db/dump/mattermost-db-$(date +%Y%m%d).sql
find db/dump -name 'mattermost-db-*.sql' -mtime +7 -delete # giữ 7 ngày để đĩa 30 GB không đầy dần
# 2. Dừng stack trong lúc backup file-level để thư mục dữ liệu PostgreSQL và file upload nhất quán
docker compose stop
# Token + namespace tạo sẵn theo Bài 04 mục 5.7 (user vm-backup@pbs, token "mattermost",
# ACL DatastoreBackup tại /datastore/syno-primary/vm-mattermost)
export PBS_PASSWORD='<TOKEN_SECRET>' # secret của token vm-backup@pbs!mattermost
export PBS_FINGERPRINT='<FINGERPRINT>' # fingerprint của pbs01 (Bài 04 mục 5.3), cần khi chứng chỉ PBS là tự ký
proxmox-backup-client backup \
mattermost-app.pxar:/opt/mattermost/app \
mattermost-db.pxar:/opt/mattermost/db \
mattermost-nginx.pxar:/opt/mattermost/nginx \
--repository 'vm-backup@[email protected]:syno-primary' \
--ns vm-mattermost
# 3. Chạy lại stack sau khi backup xong
docker compose start
Kiểm tra & Xác minh
# 0. Phiên bản khớp bảng "Phiên bản phần mềm sử dụng"
lsb_release -ds # Ubuntu 24.04.x LTS
docker --version && docker compose version
docker inspect --format '{{.Config.Image}}' mattermost-app mattermost-db mattermost-nginx
proxmox-backup-client version # bản static từ repo pbs-client (mục 4.9)
# 1. Tất cả containers running
cd /opt/mattermost && docker compose ps
# 2. Mattermost API health
curl -sf https://mattermost.hoatranlabs.local/api/v4/system/ping | python3 -m json.tool
# Expected: {"status":"OK","database_status":"OK","filestore_status":"OK"}
# 3. PostgreSQL connection
docker exec mattermost-db psql -U mattermost -c "\l"
# 4. Test gửi message vào channel alerts bằng webhook
WEBHOOK_URL="https://mattermost.hoatranlabs.local/hooks/<id-alerts>"
curl -X POST "$WEBHOOK_URL" \
-H 'Content-Type: application/json' \
-d '{"text": ":white_check_mark: **Test notification từ hạ tầng** — Mattermost hoạt động bình thường."}'
# 5. Kiểm tra SMTP email (khi user đăng ký)
# System Console → Reporting → Server Logs → filter "smtp"
# 6. Kiểm tra WebSocket (quan trọng cho real-time)
# Mở browser dev tools → Network → WS khi truy cập Mattermost
# Phải có connection tới wss://mattermost.hoatranlabs.local
# 7. Authentik SSO test
echo "Thử login tại: https://mattermost.hoatranlabs.local → 'Sign in with GitLab'"
# 8. Prometheus metrics
curl -sf http://10.11.65.57:9100/metrics | grep node_load
# 9. Kiểm tra log
docker logs mattermost-app --tail=20
docker logs mattermost-db --tail=10
docker logs mattermost-nginx --tail=10
Quản lý sau triển khai
Tạo bot account cho automation (Prometheus Alertmanager) · Cập nhật Mattermost
Tạo bot account cho automation (Prometheus Alertmanager)
# Dùng mmctl (CLI Mattermost)
docker exec mattermost-app ./bin/mmctl user create \
--email [email protected] \
--username alerts-bot \
--password "Hoatran@2024!" \
--system-admin=false
docker exec mattermost-app ./bin/mmctl user convert alerts-bot --bot
# Lấy bot token: System Console → Integrations → Bot Accounts → alerts-bot → Token
Cập nhật Mattermost
pg_dump rồi restore vào container 16 mới, có sẵn bản dump ở mục 4.9), và Mattermost phải nâng theo đường nâng cấp trong docs (9.11 → 10.x → 11.x, đọc upgrade notes từng bản). Với VM dựng mới theo bài này thì chỉ cần làm như bên dưới. Cần kiểm lại đường nâng cấp trên máy thật.
# Sửa version trong docker-compose.yml (đọc ghi chú phát hành trước khi nâng), sau đó:
cd /opt/mattermost
docker compose pull mattermost
docker compose up -d mattermost
docker compose logs mattermost --tail=30
Troubleshooting
| Vấn đề | Nguyên nhân | Giải pháp |
|---|---|---|
502 Bad Gateway từ Nginx | Mattermost container chưa ready | Chờ 2-3 phút, kiểm tra docker logs mattermost-app |
| WebSocket disconnect liên tục | Nginx proxy_read_timeout ngắn | Verify proxy_read_timeout 86400s trong nginx.conf |
| Email không gửi được | SMTP auth sai hoặc Mailu chưa accept | Kiểm tra mailu.hoatranlabs.local:587, test bằng swaks |
| Authentik SSO redirect fail | Redirect URI mismatch | Provider phải có cả hai URI Strict /login/gitlab/complete và /signup/gitlab/complete (không phải /callback) |
| Đăng nhập Authentik xong báo thiếu email/username | Provider chưa gắn scope mapping mattermost-read-user | Làm lại mục 4.3: scope mapping (scope name read_user) và chọn nó cùng openid trong Selected Scopes |
| Banner/thông báo giới hạn lịch sử 10.000 tin nhắn | Chạy image enterprise-edition không license (gói Entry) | Bình thường với lab; tin cũ vẫn nằm trong database. Muốn bỏ giới hạn thì cần license Mattermost |
netplan apply cảnh báo "Permissions ... too open" | File trong /etc/netplan cho phép người khác đọc (Ubuntu 24.04, netplan 1.0) | chmod 600 /etc/netplan/*.yaml |
PostgreSQL role does not exist | DB init failed | Xóa /opt/mattermost/db/data/*, restart postgres + mattermost |
| Webhook message không hiện | Channel name sai trong webhook config | Thêm "channel": "alerts" vào webhook payload |
413 Request Entity Too Large | File upload quá lớn | Tăng client_max_body_size trong nginx.conf |
| Mattermost không load được plugins | Plugin dir permission sai | chown -R 2000:2000 /opt/mattermost/app/plugins |
database_status: UNHEALTHY | PostgreSQL restart | docker compose restart postgres, chờ 30s rồi restart mattermost |
| HTTPS cert không trust được | Client chưa có root CA | Install hoatranlabs-root.crt vào trust store client |
Checklist triển khai
- VM272 tạo thành công, IP 10.11.65.57, VLAN 65, Ubuntu Server 24.04.5 LTS, Docker Engine từ repo noble
- Phiên bản đúng bảng "Phiên bản phần mềm sử dụng" (Mattermost 11.7.12, PostgreSQL 16, Nginx 1.30)
- TLS cert cho mattermost.hoatranlabs.local từ step-CA
- PostgreSQL container running và healthy
- Mattermost container running,
/api/v4/system/pingtrả về OK - Nginx reverse proxy hoạt động tại
https://mattermost.hoatranlabs.local - Admin account
[email protected]tạo thành công - Team
HoatranLabstạo thành công - 5 channels tạo: #general, #alerts, #security, #deployments, #incidents
- Incoming webhooks tạo cho mỗi channel (4 webhooks)
- DNS record mattermost.hoatranlabs.local → 10.11.65.57
- Authentik OIDC login (via "Sign in with GitLab") hoạt động
- Mailu SMTP: gửi test email thành công từ Mattermost
- Prometheus Alertmanager gửi test alert vào #alerts
- Wazuh integration gửi alert level 12+ vào #security
- Gitea webhook push notification vào #deployments
- TheHive case notification vào #incidents
- node_exporter chạy, Prometheus scrape thành công
- WebSocket ổn định (không bị disconnect)
- PBS backup chạy thành công