Featured image of post K3S中使用 traefik 给App关联ACME证书

K3S中使用 traefik 给App关联ACME证书

前情提要:

最近,把TrueNAS 中的app traefik 升级(2.11 –> 3.5.4 )。

结果发现,使用traefik反代的https流量,证书都变为traefik 的自签名证书了 ,修改app 中的证书关联项也不成功。

而升级前,app 中都是可以关联我通过ACME 自动更新的签名证书 。

注意:

我的TrueNAS 版本是: TrueNAS-SCALE-23.10.2

该版本还在使用K3S 管理app服务。

于是又有了一篇,折腾后的文章。

使用K3S secret的办法手动指定证书

优点

  • 符合 Kubernetes 最佳实践
  • 证书存储在 Kubernetes 内部,更安全
  • 更新证书只需更新 Secret,无需重启 Traefik 应用配置
  • 可以在多个 IngressRoute 中重复使用

缺点

  • 需要手动创建和管理 Secret

执行以下步骤创建traefik 的secret

TrueNAS 系统的证书文件都在 /etc/certificates/ 文件夹下。

1. 创建 Secret

1
2
3
4
k3s kubectl create secret tls traefik-default-cert \
  --namespace=ix-traefik \
  --cert=/etc/certificates/ACME_Certificate.crt \
  --key=/etc/certificates/ACME_Certificate.key

2. 验证 Secret

1
2
k3s kubectl get secret traefik-default-cert -n ix-traefik
k3s kubectl describe secret traefik-default-cert -n ix-traefik

日志:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
> k3s kubectl get secret traefik-default-cert -n ix-traefik -o yaml


apiVersion: v1
data:
  tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR2RENDQTBLZ0F3SUJBZ0lTQm9lTXdKYUxkZ2V2Mk1DZXBiSTZ0WEt0T......VJUSUZJQ0FURS0tLS0t
  tls.key: LS0tLS1CR......S0VZLS0tLS0K
kind: Secret
metadata:
  creationTimestamp: "2026-01-19T10:22:48Z"
  name: traefik-default-cert
  namespace: ix-traefik
  resourceVersion: "114365208"
  uid: d54fd403-330a-4e43-8104-1188ced48c83
type: kubernetes.io/tls

测试是否生效:

1. 创建一个测试 IngressRoute

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
cat > test-tls.yaml << EOF
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: test-cert
  namespace: ix-traefik
spec:
  entryPoints:
    - websecure
  routes:
    - match: Host('test.home.nxlan.cn')
      kind: Rule
      services:
        - name: whoami
          port: 80
  tls:
    secretName: traefik-default-cert
EOF

2. 应用上述测试配置

1
k3s kubectl apply -f test-tls.yaml

3. 使用curl 测试证书生效情况

 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
root@truenas[/etc/certificates]# curl -v https://test.home.nxlan.cn --insecure
*   Trying 192.168.202.6:443...
* Connected to test.home.nxlan.cn (192.168.202.6) port 443 (#0)
* ALPN: offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
* ALPN: server accepted h2
* Server certificate:
*  subject: CN=home.nxlan.cn
*  start date: Dec 22 01:53:44 2025 GMT
*  expire date: Mar 22 01:53:43 2026 GMT
*  issuer: C=US; O=Let's Encrypt; CN=E7
*  SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
* using HTTP/2
* h2h3 [:method: GET]
* h2h3 [:path: /]
* h2h3 [:scheme: https]
* h2h3 [:authority: test.home.nxlan.cn]
* h2h3 [user-agent: curl/7.88.1]
* h2h3 [accept: */*]
* Using Stream ID: 1 (easy handle 0x56179e69fc90)
> GET / HTTP/2
> Host: test.home.nxlan.cn
> user-agent: curl/7.88.1
> accept: */*
> 
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
< HTTP/2 404 
< content-type: text/plain; charset=utf-8
< x-content-type-options: nosniff
< content-length: 19
< date: Mon, 19 Jan 2026 12:08:54 GMT
< 
404 page not found
* Connection #0 to host test.home.nxlan.cn left intact

其中证书部分时间可以看出,ACME 证书已经生效了——

Server certificate:

  • subject: CN=home.nxlan.cn
  • start date: Dec 22 01:53:44 2025 GMT
  • expire date: Mar 22 01:53:43 2026 GMT
  • issuer: C=US; O=Let’s Encrypt; CN=E7

最后,在app 中关联secret 证书

1. 点击TrueNAS 中的 “应用”

truenas_apps

2. 编辑已关联traefik 的app,找到“TLS-Settings” 部分,关联刚设置好的secret

K3S_APP_certificate

应用生效。

3. 最后,访问app web 页面

浏览器不再提示“证书错误”,至此所有操作完成。

image-20260128131211776
Licensed under CC BY-NC-SA 4.0