顯示廣告
隱藏 ✕
看板 KnucklesNote
作者 Knuckles (站長 那克斯)
標題 [Linode] NodeBalancer 使用 Let's Encrypt SSL憑證
時間 2023-07-04 Tue. 02:58:47


[圖]


Linode 的負載分流器 NodeBalancer
可以使用 HTTPS 加密模式,但需要輸入通過認證的 SSL 憑證

要申請免費的 Let's Encrypt SSL憑證的話
先依照這篇的方法: [CentOS7] Apache 使用 Certbot 申請 Let's Encrypt 的SSL憑證
使用 snap 安裝 certbot

但當 NodeBalancer 連結兩台以上主機時,若使用 HTTP-01 Challenge 認證
認證時可能會連到沒有確認檔的主機導致認證失敗
必須改用 DNS-01 Challenge 認證,將確認檔寫在DNS上


申請 Linode API 的 Personal Access Token

用來自動更新 Linode 的 Domains 與 NodeBalancer
在 Linode 管理頁面右上角點 My Profile 的 API Tokens
點「Create A Personal Access Token」
[圖]


Label輸入「update_ssl」,過期時間選擇「Never」
點一下Select All的「None」後,再點 Domains 與 NodeBalancers 的「Read/Write」
[圖]


點下面的「Create Token」後會產生一串 Personal Access Token,先複製起來存著
[圖]



安裝 Certbot 的 DNS 擴充功能

Certbot 有對各家 DNS 寫了擴充功能,其中就有 Linode 的 DNS

參考 Certbot官網的DNS認證說明
先設定這個才能安裝 certbot 的擴充功能
$ sudo snap set certbot trust-plugin-with-root=ok
安裝 certbot-dns-linode 擴充功能
$ sudo snap install certbot-dns-linode

新增 credentials 檔,就是 Linode API 的設定值
$ sudo mkdir /root/.config
$ sudo vim /root/.config/linode.ini
# Linode API credentials used by Certbot
dns_linode_key = {Personal Access Token}
dns_linode_version = 4
其中 {Personal Access Token} 改為之前產生的 Personal Access Token

將權限設為只限擁有者讀寫
$ sudo chmod 600 /root/.config/linode.ini

執行 certbot 使用 DNS 認證申請 SSL 憑證
$ sudo certbot certonly --dns-linode --dns-linode-credentials /root/.config/linode.ini --dns-linode-propagation-seconds 120 -d disp.cc -d *.disp.cc

其中 --dns-linode-credentials /root/.config/linode.ini
輸入之前建立的 credentials 檔,裡面有 Linode API 的 Personal Access Token
--dns-linode-propagation-seconds 120
代表執行後會等待120秒讓DNS更新資料後再確認,
會在DNS新增以下TXT記錄,確認後就會移除
_acme-challenge
	
xxxxxx

-d disp.cc -d *.disp.cc 代表要認證的網域名稱
使用 DNS-01 Challenge 認證可以申請萬用字元憑證(Wildcard Certificate)
就是網域名稱的第一個字可以是任意字串
例如使用 *.disp.cc,就可以將憑證用在 www.disp.cc, knuckles.disp.cc, xxx.disp.cc

執行結果:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for disp.cc and *.disp.cc
Waiting 120 seconds for DNS changes to propagate

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/disp.cc/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/disp.cc/privkey.pem
This certificate expires on 2023-10-07.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
產生了兩個憑證檔 fullchain.pem 與 privkey.pem

Certbot 已自動設定定期執行,不用再寫至 crontab
$ sudo systemctl list-timers

NEXT                         LEFT          LAST                         PASSED
Mon 2023-07-10 10:11:00 CST  14h left      Sun 2023-07-09 18:42:00 CST  49min ago

UNIT                         ACTIVATES
snap.certbot.renew.timer     snap.certbot.renew.service

手動設定 NodeBalancer 的 SSL憑證

申請好的憑證檔就可以用來輸入 NodeBalancer 的設定
例如在 Linode 設定了一台 NodeBalancer,Label 取為 nb2
[圖]


在 NodeBalancer 的 Configuration 新增 port 443,Protocol 選擇 HTTPS
將之前產生的兩個憑證檔
/etc/letsencrypt/live/disp.cc/fullchain.pem
/etc/letsencrypt/live/disp.cc/privkey.pem
的內容複製後分別貼在 SSL Certificate 與 Private Key 輸入框中
[圖]


然後在 Backend Nodes 使用 port 80 連結至 Linode 主機
[圖]


完成後測試看看能否使用 https 連線
[圖]




使用 Linode-cli 自動更新 NodeBalancer 的設定

由於 Let's Encrypt 憑證90天就會到期,所以要能自動更新 NodeBalancer 上的憑證

參考 https://www.linode.com/docs/products/tools/cli/guides/install/ 
安裝 Linode-cli

要先安裝 python3
$ sudo yum install python3 python3-pip

使用 pip3 安裝 linode-cli
$ sudo pip3 install linode-cli --upgrade

試試看能否執行
$ sudo linode-cli --help

若使用root執行找不到,在root的執行路徑加上 linode-cli 的軟連結
$ sudo ln -s /usr/local/bin/linode-cli /usr/bin/linode-cli

若出現 501 Protocol scheme 'https' is not supported (LWP::Protocol::https not installed)
就再安裝 perl-LWP-Protocol-https
$ sudo yum install perl-LWP-Protocol-https

第一次執行 linode-cli 要先設定
$ sudo linode-cli configure
Welcome to the Linode CLI.  This will walk you through some initial setup.

First, we need a Personal Access Token.  To get one, please visit
https://cloud.linode.com/profile/tokens and click
"Create a Personal Access Token".  The CLI needs access to everything
on your account to work correctly.
Personal Access Token:
輸入之前產生的 Personal Access Token

接著輸入預設的區域、方案、作業系統
Default Region for operations.  Choices are:
 14 - ap-northeast (日本機房選這個)
Default Type of Linode to deploy.  Choices are:
 3 - g6-standard-2 (Shared CPU 2GB)
Default Image to deploy to new Linodes.  Choices are:
 9 - linode/centos7

這邊設定的內容會存在 /root/.config/linode-cli


參考 NodeBalancer 的API https://www.linode.com/docs/api/nodebalancers/

先列出目前有使用的 NodeBalancers
$ sudo linode-cli nodebalancers list --text --format id,label,region,ipv4
id     label  region       ipv4
12345  nb2    ap-northeast 139.162.95.99
記下要更新的 nodeBalancerId 例如 12345
加 --text 代表用tab分隔資料就好,預設是會畫表格
加 --format xxx 代表列出xxx這些資料就好了

列出這個 NodeBalancer 的 Configurations
$ sudo linode-cli nodebalancers configs-list 12345 --text --format id,port,protocol
id    port  protocol
4566  80    http
4567  443   https
記下要更新的 configId 例如 4567

有了 nodeBalancerId 與 configId 就可以使用 config-update 更新憑證了

使用 config-update 更新 NodeBalancer 上的憑證設定
$ sudo linode-cli nodebalancers config-update 12345 4567 --ssl_cert /etc/letsencrypt/live/disp.cc/fullchain.pem --ssl_key  /etc/letsencrypt/live/disp.cc/privkey.pem

注意要用root權限才讀得到憑證檔

寫成bash檔
$ vim /root/update_linode.sh
#!/bin/bash

nodeBalancerId=12345
configId=4567

linode-cli nodebalancers config-update $nodeBalancerId $configId \
  --ssl_cert /etc/letsencrypt/live/disp.cc/fullchain.pem \
  --ssl_key /etc/letsencrypt/live/disp.cc/privkey.pem

設定 crontab 定期更新憑證至 NodeBalancer
例如在每週5的18:45執行一次
$ vim /etc/crontab
45 18 * * 5   root /root/update_linode.sh


參考
Linode Node Balancer Auto SSL - GitHub

--
※ 作者: Knuckles 時間: 2023-07-04 02:58:47 (台灣)
※ 編輯: Knuckles 時間: 2023-11-29 23:20:45 (台灣)
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 89 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇