iT邦幫忙

2022 iThome 鐵人賽

DAY 19
2
DevOps

前端轉生~到了實驗室就要養幾隻可愛鯨魚:自架 Kubernetes 迷航日記系列 第 19

Day 19 — 艦隊自我認同:Ingress 套用 OpenSSL 自簽憑證

  • 分享至 

  • xImage
  •  

可愛鯨魚

使用憑證!可愛鯨魚穿上了護甲!

圖片來源:Docker (@Docker) / Twitter

架設了 Harbor 但還是只能用 insecure-registries 嗎? 我就不想買憑證也不想沒過幾個月又要去更新憑證...

就來用 OpenSSL 從根憑證開始簽吧!

自簽憑證

簡單列一下接下來的步驟~

  1. 簽根憑證
  2. 安裝根憑證到所有 Node
  3. 建立 CSR
  4. 簽發終端憑證
  5. 將 Traefik 綁上憑證
  6. 所有 Node 可以直接透過 Docker 或 Kubernetes 取得 Harbor 上的 Image

步驟指令已經統整在 github Day 19 - create-cert.sh 囉~

步驟示意圖

目標是所有 Node 不設定 insecure-registries 就能取得 Harbor 上的 Image~

簽根憑證

先產出根憑證 key,使用 -aes256 可以設定密碼,記得把 password 改掉哦!

openssl genrsa -aes256 -passout pass:password -out ca.key 4096

接著用 key 產生根憑證

openssl req -x509 -new -sha512 -days 365 \
    -subj "/C=TW/ST=Taipei/L=Taipei/O=test/OU=lab/CN=example" \
    -passin pass:password \
    -key ca.key \
    -out ca.pem
  • days: 設定憑證有效天數
  • subj: 設定 CSR 資訊
    • C: Country Name (2 個英文字母),國家/地區代號
    • ST: State (全名),州/省份全名
    • O: Organization Name,公司/組織名稱
    • OU: Organizational Unit Name,公司部門/組織單位
    • CN: Common Name,完全限定域名 (FQDN)

安裝根憑證

Ubuntu 的憑證資料夾在 /usr/share/ca-certificates,直接幫剛剛產出的 ca.pem 放到資料夾中就可以了!

可以在裡面多建立一個資料夾便於辨認憑證

sudo mkdir /usr/share/ca-certificates/cluster
sudo cp ca.pem /usr/share/ca-certificates/cluster/

使用指令更新 OS 的憑證,--fresh 會全部重新加載

sudo update-ca-certificates --fresh

如果需要 GUI 版本的可以使用
sudo dpkg-reconfigure ca-certificates

建立 CSR

建立 CSR key

這裡不能用 -aes256 加密! (Kubernetes 和 Traefik 都不支援)

openssl genrsa -out example.domain.com.key 4096

這裡直接簽 wildcard CN ~

openssl req -sha512 -new \
    -subj "/C=TW/ST=Taiwan/L=Taipei/O=test/OU=lab/CN=*.example.domain.com" \
    -key example.domain.com.key \
    -out example.domain.com.csr

關於 Kubernetes 和 Traefik 不支援 passphrase
Traefik 相關討論:

Kubernetes 相關討論:

簽發終端憑證

先建立終端憑證設定檔,記得修改 DNS~

cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=*.example.domain.com
DNS.2=example.domain.com
EOF

使用前面簽的 CA 簽發終端憑證,記得把 password 改成自己設定的!

openssl x509 -req -sha512 -days 365 \
    -extfile v3.ext \
    -passin pass:password \
    -CA ca.pem -CAkey ca.key -CAcreateserial \
    -in example.domain.com.csr \
    -out example.domain.com.pem

Traefik 綁上憑證

  1. Traefik 需要自己將 CA 一起綁進憑證
cat example.domain.com.pem ca.pem > fullchain.pem
  1. 建立 secret,這裡用的是 generic,把憑證通通丟進去~
kubectl create secret generic tls-secret -n traefik \
    --from-file=ca.pem \
    --from-file=tls.pem=fullchain.pem \
    --from-file=tls.key=example.domain.com.key
  1. 透過 configMap 建立一個 Traefik 的設定檔 (dyn.yaml),可以直接在 Traefik 設定所有 Ingress 的預設憑證
apiVersion: v1
kind: ConfigMap
metadata:
  name: traefik-config
  labels:
    name: traefik-config
  namespace: traefik
data:
  dyn.yaml: |
    tls:
      options:
        default:
          minVersion: VersionTLS12
      stores:
        default:
          defaultCertificate:
            certFile: '/certs/tls.pem'
            keyFile: '/certs/tls.key'
  1. 回到 Traefik values.yaml 修改設定

完整檔案可從 github Day 19 - values.yaml 取得

  • volumes: 將 secret 和 configMap 透過 volume 掛載
volumes:
  - name: tls-secret
    mountPath: '/certs'
    type: secret
  - name: traefik-config
    mountPath: '/config'
    type: configMap
  • additionalArguments: 指定 Traefik 設定檔使用 configMap 裡面的檔案
additionalArguments:
  - '--providers.file.filename=/config/dyn.yaml'

測試

來測試看看憑證吧~

確認事項

  1. 如果不是自己的 domain 記得到 /etc/hosts 設定...
10.1.0.1 aaa.example.domain.com harbor.example.domain.com
  1. 如果是用其他電腦連也要記得安裝 CA...
    電腦裝CA

測試 1 - 404 網頁

隨便開一個網域下的網址 ( 如: [aaa].example.domain.com )

畫面會顯示 Traefik 的預設頁面 404

接著看網站憑證,可以看到 example 的 rootCA,底下有一個 wildcard 憑證 ( .example.domain.com )

測試 2 - harbor

打開網頁可以看到憑證一樣

接著測試 Docker 指令

  1. 登入 harbor
$ docker login https://harbor.example.domain.com
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /home/user/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
  1. 推送 image
$ docker image ls alpine
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
alpine       3.16.2    9c6f07244728   7 weeks ago   5.54MB

$ docker tag alpine:3.16.2 harbor.example.domain.com/library/alpine:3.16.2

$ docker push harbor.example.domain.com/library/alpine:3.16.2
The push refers to repository [harbor.example.domain.com/library/alpine]
994393dc58e7: Pushed 
3.16.2: digest: sha256:1304f174557314a7ed9eddb4eab12fed12cb0cd9809e4c28f29af86979a3c870 size: 528
  1. 登出 harbor
$ docker logout https://harbor.example.domain.com
Removing login credentials for harbor.example.domain.com
  1. 從網頁上查看
    libaray
    alpine

推送成功~ /images/emoticon/emoticon42.gif


Ref


今天簽完自己的憑證並不是要取代外面簽發的憑證
是那些只有內部需要的服務用自己簽的憑證就夠了吧~

剛好很適合目前在亂搞的我~ /images/emoticon/emoticon25.gif


上一篇
Day 18 — 有個私人的港口還好吧:私有儲存庫 Harbor (二)(使用 Ingress)
下一篇
Day 20 — 艦艇儲物小間規劃:Volumes
系列文
前端轉生~到了實驗室就要養幾隻可愛鯨魚:自架 Kubernetes 迷航日記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
雷N
iT邦研究生 1 級 ‧ 2022-10-03 09:21:41

盔甲鯨魚好可愛/images/emoticon/emoticon07.gif

Time iT邦新手 4 級 ‧ 2022-10-03 13:32:35 檢舉

每天都會冒出一隻可愛鯨魚~ 希望能蒐集完30天的鯨魚~ /images/emoticon/emoticon37.gif

0
json_liang
iT邦研究生 5 級 ‧ 2022-10-03 10:48:55

感謝分享自簽憑證!

Time iT邦新手 4 級 ‧ 2022-10-03 13:35:55 檢舉

希望對你有幫助~ /images/emoticon/emoticon12.gif

我要留言

立即登入留言