惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
Y
Y Combinator Blog
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
M
MIT News - Artificial intelligence
Last Week in AI
Last Week in AI
V
V2EX
腾讯CDC
F
Fortinet All Blogs
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss

Nemo

20260819 - Nemo 再见,2025 Complete ORB-SLAM3 Setup Guide for Jetson Xavier NX with RealSense D455 20250723 再见,2024 Create Your Own GPS Data Publisher Support SSL AGV Dispatching System Technical Documentation Finding Nemo No title 香港賽馬會呈獻系列:黑白——攝影敘事 再见,2023 团团是只猫 Design an FSM for Robot State Machines – Basics of Computer Science Data-driven robot lifespan: Collection 数据驱动的机器人寿命:收集、诊断、预测 Exploring the design space of binary search trees 特首来了 Large language models, explained with a minimum of math and jargon 设计有缓存异步逻辑的监控脚本并测试其资源占用 使用loguru记录串口数据并使用Docker搭建ARM开发环境 在vscode的Dev Container中构建.NET开发环境及使用doxygen和graphviz绘制函数调用图 State or Status? A*算法两种时间复杂度 /A* Algorithm: Two Types of Time Complexity 使用KD-Tree快速收敛到最近坐标点/Fast convergence to the nearest coordinate point using KD-Tree 翻译 || 总结 - Go语言中的空结构体(The empty struct) 再见,2022 从PE工作报告中能读出什么 Give me miles, give me truth AMR调度系统性能优化/AMR Dispatch System Performance Optimization
Using CertBot for Automatic Secure EMQX Broker
Nemo · 2024-07-11 · via Nemo

As mentioned in my previous blog posts, I took a vacation at the beginning of May, so I haven’t written much in the past six months. Today, I want to share a simple project about using CertBot to automatically renew certificates for the EMQX Broker, which is crucial for managing numerous MQTT brokers in a robotics lab.

Logic Flow

image.png
The first step is to create a CertBot container to apply for a certificate for the host. If successful, then create the EMQX container to use the applied certificate. CertBot checks the validity period of the certificate every 12 hours, and if the certificate file is modified, the EMQX container will restart.

Structure

├── docker-compose.yml
├── emqx-custom
│   └── Dockerfile
├── start-emqx.sh
└── update-certs-and-setup.sh

Code Example

docker-compose.yml

version: '3'

services:
  certbot:
    image: certbot/certbot
    container_name: certbot
    volumes:
      - ./update-certs-and-setup.sh:/update-certs-and-setup.sh
      - certs:/certs
    entrypoint: ["/bin/sh", "-c", "/update-certs-and-setup.sh"]
    environment:
      DOMAIN : ${DOMAIN}
      EMAIL : ${EMAIL}
    ports:
      - "80:80"
    restart: always

  emqx:
    build:
      context: ./emqx-custom
    container_name: emqx
    environment:
      EMQX_NAME: emqx
      EMQX_HOST: 127.0.0.1
      EMQX_LISTENERS__SSL__DEFAULT__BIND: "0.0.0.0:8883"
      EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__CACERTFILE: "/emqx/certs/chain.pem"
      EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__CERTFILE: "/emqx/certs/fullchain.pem"
      EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__KEYFILE: "/emqx/certs/privkey.pem"
      EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__VERIFY: verify_none
    ports:
      - "1883:1883"
      - "8083:8083"
      - "8883:8883"
      - "18083:18083"
    volumes:
      - "./start-emqx.sh:/start-emqx.sh"
      - "certs:/emqx/certs"
      - "emqx-log:/opt/emqx/log"
      - "emqx-data:/opt/emqx/data"
    entrypoint: ["/bin/bash", "/start-emqx.sh"]
    restart: always

volumes:
  emqx-data:
  emqx-log:
  certs:

Dockerfile:

# Using EMQ X official image as the base image
FROM emqx/emqx:5.5.0

# Switch to root to install additional packages
USER root

# Update the package list and install inotify-tools
RUN apt-get update && \
    apt-get install -y inotify-tools

# Switch back to the default user for security, replace `emqx` with the actual user if it's different
USER emqx

start-emqx.sh:

#!/bin/bash

CERT_PATH="${EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__CERTFILE}"
KEY_PATH="${EMQX_LISTENERS__SSL__DEFAULT__SSL_OPTIONS__KEYFILE}"

# Function to start EMQX
start_emqx() {
    echo "Start EMQX..."
    emqx start
}

# Monitor for changes
inotifywait -m -e modify "$CERT_PATH" "$KEY_PATH" |
while read path action file; do
    echo "Detected new certificate. Restarting EMQX..."
    emqx stop
    start_emqx
done &

# Start EMQX initially if not already running
if [ -f "$CERT_PATH" ] && [ -f "$KEY_PATH" ]; then
    start_emqx
fi

# Keep script running
while true; do
    sleep 60
done

update-certs-and-setup.sh:

#!/bin/sh

set -e

DOMAIN="${DOMAIN}"
EMAIL="${EMAIL}"

SOURCE_DIR="/etc/letsencrypt/live/${DOMAIN}"
DEST_DIR="/certs"
LOCKFILE="/tmp/certbot.lock"

while :; do
    if [ ! -f "$LOCKFILE" ]; then
        touch $LOCKFILE
        certbot certonly --standalone --preferred-challenges http-01 -d ${DOMAIN} --email ${EMAIL} --agree-tos --non-interactive

        mkdir -p ${DEST_DIR}

        if [ -d "${SOURCE_DIR}" ]; then
            cp -L ${SOURCE_DIR}/* ${DEST_DIR}/
            chmod -R 755 ${DEST_DIR}
        fi

        rm $LOCKFILE
    else
        echo "Certbot is already running."
    fi

    sleep 12h
done

Prerequisites

The only configuration needed is the domain name in the .env file. You should change the :

DOMAIN=xx.com
EMAIL=xx@xx

Domain name and email address to your own.

Then pull the repo and make sure the script is executable.

chmod +x update-certs-and-setup.sh

Lastly, you need to make sure that the port 80 is open for the Let’s Encrypt to verify the domain name.

Build and Run

docker compose up -d --build

Inspection

You can inspect the logs by running the following command:

docker logs --details certbor

then you can see the logs of the certbot include the expiration.

docker logs --details emqx

then you can see the logs of the emqx broker, started successed.