Prasyarat dan Instalasi Docker
Sistem Operasi yang Didukung
- Ubuntu 20.04 LTS atau lebih baru
- Debian 10 atau lebih baru
- Distribusi Linux berbasis APT lainnya
Instalasi Docker Engine
Ikuti panduan resmi Docker untuk menginstal Docker Engine di Ubuntu. Setelah instalasi selesai, tambahkan pengguna saat ini ke grup docker:
1
2
| sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
|
Konfigurasi Hak Akses Docker
1
2
3
4
5
| sudo usermod -aG docker $USER
newgrp docker # Muat ulang grup, atau log out/in
# Verifikasi instalasi berhasil
docker --version
|
Penggunaan sudo dengan docker dapat dihindari dengan menambahkan pengguna ke grup docker. Pastikan hanya pengguna tepercaya yang memiliki akses ini.
Persiapan Struktur Direktori
Struktur direktori yang terorganisir memudahkan manajemen konfigurasi dan data:
1
2
3
4
5
6
| # Buat direktori utama untuk proyek MQTT
mkdir ~/mqtt
cd ~/mqtt
# Buat subdirektori untuk konfigurasi, data, dan log
mkdir -p config data log
|
Konfigurasi Mosquitto Broker
Membuat File Konfigurasi Mosquitto
Buat file konfigurasi utama:
1
| nano config/mosquitto.conf
|
Konten Konfigurasi Mosquitto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| # Konfigurasi Jaringan dan Port
allow_anonymous false
listener 1883 0.0.0.0
listener 9001 0.0.0.0
protocol websockets
# Keamanan dan Autentikasi
password_file /mosquitto/config/pwfile
# Persistensi Data
persistence true
persistence_file mosquitto.db
persistence_location /mosquitto/data/
# Logging dan Monitoring
log_dest file /mosquitto/log/mosquitto.log
log_type all
# Performa dan Optimasi
# max_connections -1
# persistent_client_expiration 1d
# Untuk Banyak Koneksi
# max_connections 10000
# max_keepalive 300
# message_size_limit 268435455
|
Inisialisasi Sistem Autentikasi
Buat file password kosong (akan diisi nanti):
Konfigurasi Docker Compose
Membuat File docker-compose.yml
1
| nano docker-compose.yml
|
Konten Docker Compose
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| services:
mqtt-broker:
image: eclipse-mosquitto:latest
container_name: mosquitto-local
restart: unless-stopped
ports:
- "1883:1883" # MQTT standard port
- "9001:9001" # MQTT websocket port
- "8080:8080" # Port untuk monitoring web (opsional)
volumes:
- ./config:/mosquitto/config:rw
- ./data:/mosquitto/data:rw
- ./log:/mosquitto/log:rw
networks:
- mqtt-network
networks:
mqtt-network:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
|
Deployment Container Mosquitto
Menjalankan Container
1
2
3
| docker compose up -d
docker compose ps
docker compose logs -f
|
Verifikasi Instalasi
1
2
3
4
5
| # Cek versi Mosquitto di dalam container
docker exec mosquitto-local mosquitto -v
# Periksa kesehatan container
docker compose ps --format "table \t\t"
|
Manajemen Pengguna dan Autentikasi
Membuat Pengguna Pertama
Akses shell container:
1
| docker exec -it mosquitto-local sh
|
Buat pengguna admin dengan password:
1
2
| mosquitto_passwd -c /mosquitto/config/pwfile admin
# Masukkan password ketika diminta (contoh: Admin@2024!)
|
Tambah pengguna tambahan:
1
2
| mosquitto_passwd /mosquitto/config/pwfile user1
mosquitto_passwd /mosquitto/config/pwfile user2
|
Verifikasi daftar pengguna:
1
2
| cat /mosquitto/config/pwfile
exit
|
Restart Service untuk Menerapkan Perubahan
1
2
| docker compose restart
docker compose logs --tail=20
|
Konfigurasi Jaringan dan Discovery
1
2
3
4
5
| # Dapatkan IP address container
docker inspect mosquitto-local | grep IPAddress
# Atau gunakan format output yang lebih bersih
docker inspect -f '' mosquitto-local
|
Untuk Ubuntu/Debian:
1
2
3
4
5
6
| # Install Mosquitto Client Utilities
sudo apt install -y mosquitto-clients
# Verifikasi instalasi
mosquitto_sub --version
mosquitto_pub --version
|
Testing dan Validasi Koneksi
Test 1: Koneksi Dasar dengan Localhost
1
2
3
4
5
| # Terminal 1: Subscribe ke topic tertentu
mosquitto_sub -h localhost -t "test/connection" -u user1 -P "user1" -v
# Terminal 2: Publish pesan test
mosquitto_pub -h localhost -t "test/connection" -m "MQTT Broker berjalan dengan baik" -u user1 -P "user1"
|
Test 2: Koneksi dengan WebSocket
1
2
3
4
5
| // Contoh kode JavaScript untuk koneksi WebSocket
const client = mqtt.connect('ws://localhost:9001', {
username: 'user1',
password: 'user1'
});
|
Integrasi dengan Systemd untuk Auto-start
Membuat Service Systemd untuk Subscriber
1
2
| # Buat file service systemd
sudo nano /etc/systemd/system/mosquitto-subscriber.service
|
Konfigurasi Service Systemd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [Unit]
Description=Mosquitto MQTT Subscriber untuk iot/device1
After=network.target
Wants=network.target
[Service]
Type=simple
User=your_username
ExecStart=/usr/bin/mosquitto_sub -h ip/domain -t "test/connection" -u user1 -P user1 -v
Restart=always
RestartSec=10
StandardOutput=append:/var/log/mosquitto-sub.log
StandardError=append:/var/log/mosquitto-sub-error.log
[Install]
WantedBy=multi-user.target
|
Manajemen Service Systemd
1
2
3
4
5
| sudo systemctl daemon-reload
sudo systemctl enable mosquitto-subscriber.service
sudo systemctl start mosquitto-subscriber.service
sudo systemctl status mosquitto-subscriber.service
sudo journalctl -u mosquitto-subscriber.service -f
|
Monitoring dan Maintenance
Melihat Log Container
1
2
3
4
5
6
7
8
| # Log secara real-time
docker compose logs -f
# Log dengan filter waktu
docker compose logs --since 1h
# Log hanya untuk service tertentu
docker compose logs mqtt-broker
|
Update Container
1
2
3
| sudo docker compose pull
sudo docker compose up -d
sudo docker image prune -f
|
Troubleshooting Umum
Problem: Koneksi Ditolak
1
2
3
4
5
6
7
8
9
| # Periksa firewall
sudo ufw status
sudo ufw allow 1883/tcp
# Verifikasi container berjalan
docker compose ps
# Cek binding port
netstat -tlnp | grep 1883
|
Problem: Autentikasi Gagal
Verifikasi file password:
1
| docker exec mosquitto-local cat /mosquitto/config/pwfile
|
Reset password pengguna:
1
| docker exec mosquitto-local mosquitto_passwd -b /mosquitto/config/pwfile admin new_password
|
Monitoring Resource
Monitor resource container:
1
| docker stats mosquitto-local
|
Cek penggunaan disk:
Optimasi storage:
Kesimpulan
MQTT broker dengan Docker dan Mosquitto memberikan solusi IoT yang scalable, secure, dan mudah dikelola. Konfigurasi ini cocok untuk:
- Pengembangan aplikasi IoT lokal
- Validasi sistem sebelum deployment produksi
- Deployment untuk jumlah device terbatas
Langkah Selanjutnya
- Implementasi SSL/TLS untuk enkripsi data
- Setup cluster Mosquitto untuk high availability
- Integrasi dengan database untuk penyimpanan data historis
- Implementasi dashboard monitoring dengan Grafana
Disclaimer
Konfigurasi ini dioptimalkan untuk lingkungan lokal. Untuk deployment produksi di lingkungan publik, pertimbangkan:
- Implementasi firewall yang ketat
- Penggunaan sertifikat SSL/TLS
- Mekanisme autentikasi yang lebih kuat
- Monitoring dan alerting yang komprehensif