acme.sh实现了acme协议,可以从letsencrypt生成免费的证书。APISIX可以通过开放的的API来实现自动更新SSL证书,本文简要介绍基于acme.sh的renew-hook来自动更新apisix的APISIX SSL 证书。
环境介绍
| OS | APISIX-version |
|---|---|
| Ubuntu x86_64 | apache/apisix:3.2.0 |
1. 安装acme.sh
curl https://get.acme.sh | sh -s email=my@example.com
设置环境变量,这里使用DNS验证模式的方式申请证书,以阿里的id为例:
export Ali_Key="$YOUR-KEY"
export Ali_Secret="$YOUR-SECRET"
添加成功会增加到对应的/root/.acme.sh/account.conf
SAVED_Ali_Key='$YOUR-KEY'
SAVED_Ali_Secret='$YOUR-SECRET'
2. 安装脚本
安装jq
安装脚本需要用到jq
sudo apt-get update
sudo apt-get install jq
下载脚本
curl --output /root/.acme.sh/renew-hook-update-APISIX.sh --silent https://gist.github.com/overstarry/0f5c2cf7cd4ccfe653dfa071390ae90b/renew-hook-update-APISIX.sh
(国内环境可以手动下载 renew-hook-update-apisix.sh,上传到服务器,并复制到/root/.acme.sh 路径)
执行脚本
$ chmod +x /root/.acme.sh/renew-hook-update-APISIX.sh
$ /root/.acme.sh/renew-hook-update-APISIX.sh
Usage : /root/.acme.sh/renew-hook-update-APISIX.sh -h <apisix admin host> -p <certificate pem file> -k <certificate private key file> -a <admin api key> -t <print debug info switch off/on,default off>
3. 生成证书
~/.acme.sh/acme.sh --issue --dns dns_ali -d *.xx.com --renew-hook '~/.acme.sh/renew-hook-update-APISIX.sh -h http://127.0.0.1:9280 -p ~/.acme.sh/"*.xx.com_ecc"/"fullchain.cer" -k ~/.acme.sh/"*.xx.com_ecc"/"*.xx.com.key" -a {admin-key}' --log --debug
其中
*.xx.com为要申请的域名http://127.0.0.1:9280是APISIX的admin api地址{admin-key}是apisix的admin-key
待执行完成,出现如下提示表示成功,可以在dashboard看到已经添加到列表。
[Wed 06 Dec 2023 04:47:14 PM CST] Your cert is in: /root/.acme.sh/YOUR-SITE_ecc/YOUR-SITE.cer
[Wed 06 Dec 2023 04:47:14 PM CST] Your cert key is in: /root/.acme.sh/YOUR-SITE_ecc/YOUR-SITE.key
[Wed 06 Dec 2023 04:47:14 PM CST] The intermediate CA cert is in: /root/.acme.sh/YOUR-SITE_ecc/ca.cer
[Wed 06 Dec 2023 04:47:14 PM CST] And the full chain certs is there: /root/.acme.sh/YOUR-SITE_ecc/fullchain.cer
[Wed 06 Dec 2023 04:47:14 PM CST] _on_issue_success
以上。
遇到的问题
- 执行脚本未增加到APISIX
可以使用--debug执行,查看调试日志,这里笔者的原因是*.xx.com_ecc 写成了*.xx.com。
~/.acme.sh/renew-hook-update-APISIX.sh -h http://127.0.0.1:9280 -p ~/.acme.sh/"*.xx.com_ecc"/"fullchain.cer" -k ~/.acme.sh/"*.xx.com_ecc"/"*.xx.com.key" -a {admin-key}