traefik https使用
之前已經使用traefik服務作為入口,測試並訪問了tomcat應用,之前是通過http來訪問的,而我們在yaml文件里面也添加8443端口用於https訪問,在實際環境中我們也是需要
https來進行訪問應用,通過traefik實現https,traefik http應用
操作實踐
這里我用了公司的證書,就是為了貼近真實,也滿足測試需求,創建一個secret,保存https證書,如果沒有證書,可以使用以下方式進行生成證書
簽證書
沒有證書可以使用命令生產證書
1 |
# mkdir certs |
部署准備
traefik.toml
-
http 和https共同存在
1
2
3
4
5
6
7
8
9
10
11defaultEntryPoints = ["http","https"]
[entryPoints]
[entryPoints.http]
address = ":80"
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[[entryPoints.https.tls.certificates]]
certFile = "/certs/xxlaila.cn.crt"
keyFile = "/certs/xxlaila.cn.key" -
所有http請求全部rewrite為https的規則
1
2
3
4
5
6
7
8
9
10
11
12defaultEntryPoints = ["http","https"]
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[[entryPoints.https.tls.certificates]]
certFile = "/certs/xxlaila.cn.crt"
keyFile = "/certs/xxlaila.cn.key" -
部分域名強制跳轉https
1
2
3
4
5
6
7
8
9
10
11
12
13defaultEntryPoints = ["http","https"]
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
regex = "^http://traefix.xxlaila.cn/(.*)"
replacement = "https://traefix.xxlaila.cn/$1"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[[entryPoints.https.tls.certificates]]
certFile = "/certs/xxlaila.cn.crt"
keyFile = "/certs/xxlaila.cn.key"
創建證書secret
1 |
# kubectl create secret generic traefik-cert --from-file=certs/xxlaila.cn.crt --from-file=certs/xxlaila.cn.key --from-file=certs/dev.xxlaila.cn.crt --from-file=certs/dev.xxlaila.cn.key --from-file=certs/test.xxlaila.cn.crt --from-file=certs/test.xxlaila.cn.key -n kube-system |
- traefik-cert.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21證書base64加密
# cat dev.xxlaila.cn.crt |base64 |tr -d '\n'
# cat > traefik-cert.yaml<<EOF
---
kind: Secert
apiVersion: v1
metadata:
name: traefik-cert
namespace: kube-system
data:
"dev.xxlaila.cn.crt":
"dev.xxlaila.cn.key":
"test.xxlaila.cn.crt"
"test.xxlaila.cn.key":
"xxlaila.cn.crt":
"xxlaila.cn.key":
type:
- Opaque
EOF
創建configmap保存traefix的配置
- traefik.toml
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# cat > traefik.toml<<EOF
defaultEntryPoints = ["http","https"]
[entryPoints]
[entryPoints.http]
address = ":80"
compress = true
[entryPoints.http.whitelist]
sourceRange = ["172.21.0.0/16", "172.16.0.0/16"]
useXForwardedFor = true
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[[entryPoints.https.tls.certificates]]
certFile = "/opt/traefix/certs/xxlaila.cn.crt"
keyFile = "/opt/traefix/certs/xxlaila.cn.key"
[[entryPoints.https.tls.certificates]]
certFile = "/opt/traefix/certs/dev.xxlaila.cn.crt"
keyFile = "/opt/traefix/certs/dev.xxlaila.cn.key"
[[entryPoints.https.tls.certificates]]
certFile = "/opt/traefix/certs/test.xxlaila.cn.crt"
keyFile = "/opt/traefix/certs/test.xxlaila.cn.key"
# rules
filename = "/opt/traefix/conf/rules.toml"
watch = true
EOF
# kubectl create configmap traefik-conf --from-file=conf/traefik.toml -n kube-system
configmap/traefik-conf created
# kubectl get configmap traefik-conf -n kube-system
NAME DATA AGE
traefik-conf 1 25s
重新部署Traefix
重新部署Traefix主要是要關聯創建的secret和configMap,並掛載相對應的主機目錄。
deployment 方式部署
修改片段
1 |
# vim traefik-deployment.yaml |
- 執行創建
1
# kubectl apply -f traefik-deployment.yaml
測試ui
1 |
# cat >ui.yaml<<EOF |
1 |
# cat >ui-test.yaml <<EOF |
注:
tls: traefikm默認加載的證書是tls開頭的crt、key證書。如果只有一個證書,可以這么設置。多個域名證書需要設定不同的secret名稱,在tls引用的時候根據不同的域名指定不同secret名稱
redirect-entry-point: 該域名強制跳轉https
traefik 代理外部服務
traefix對外部應用提供服務,這里以公司的一個應用app和harbor為列,
java app
1 |
# cat > java-app.yaml |
harbor
1 |
# cat >harbor.yaml<<EOF |
curl驗證證書:curl --resolve 'xxx.xxx.xxx:127.0.0.1' https://xxx.xxx.xxx/ -vvv