当前位置:网站首页>基于 Traefik 的激进 TLS 安全配置实践
基于 Traefik 的激进 TLS 安全配置实践
2022-04-22 15:11:00 【东风微鸣】
前言
Traefik[1]是一个现代的HTTP反向代理和负载均衡器,使部署微服务变得容易。
Traefik可以与现有的多种基础设施组件(Docker、Swarm模式、Kubernetes、Marathon、Consul、Etcd、Rancher、Amazon ECS...)集成,并自动和动态地配置自己。
今天我们基于 Traefik on K8S 来详细说明如何对 TLS 安全进行「激进」配置。
环境基本信息
1.K8S 集群;
2.域名:ewhisper.cn(由 DNSPod 进行 DNS 管理,已指向 K8S 集群的 Traefik Ingress 的 LoadBalancer 公网地址)
3.使用 cert-manager 自动管理的证书 *.ewhisper.cn 作为 Traefik 的默认证书;cert-manager 位于 cert-manager NameSpace 下
4.Traefik 2.4.8 安装于 K8S 集群的 kube-system NameSpace 下,且使用 CRDs 进行配置。
「激进」的 TLS 配置
全站受信证书 + HTTPS。具体如下:
1.全站 HTTPS 443 端口配置;
2.证书来自 Let's Encrypt(由 cert-manager 自动申请)(激进,生产慎用!)
3.监听 HTTP 请求,并重定向到 HTTPS;(激进,生产慎用!)
4.启用 HSTS 功能(激进,生产慎用!)
5.TLS 版本限定在 TLS 1.3(激进,生产慎用!)
配置实践
TLS 版本限定在 TLS 1.3
使用 Traefik 的 CRD - TLSOption[2] 配置如下:
apiVersion: traefik.containo.us/v1alpha1
kind: TLSOption
metadata:
name: default
namespace: kube-system
spec:
minVersion: VersionTLS13
说明:
•minVersion: VersionTLS13 指定 TLS 最小版本为 TLS 1.3.
️ Warning: 以防万一,建议
namespace: kube-system和 Traefik 所在的 ns 保持一致。
证书
使用 Traefik 的 CRD - TLSStore[3] 配置如下:
apiVersion: traefik.containo.us/v1alpha1
kind: TLSStore
metadata:
name: default
namespace: cert-manager
spec:
defaultCertificate:
secretName: ewhisper-crt-secret
说明:
•secretName: ewhisper-crt-secret 这个是 cert-manager 自动从 Let's Encrypt 申请到的证书的存放位置(cert-manager 会负责定期自动更新该证书)。Traefik 就使用该证书作为默认证书。
️ Warning: TLSStore,注意
namespace: cert-manager必须要在 证书的 secret 所在的 NameSpace。
接下来 2 个功能:
1.HTTP 重定向到 HTTPS
2.启用 HSTS
都是通过 Traefik CRD - Middleware[4] 来进行配置的。
HTTP 重定向到 HTTPS
Traefik CRD Middleware - redirectshttps 配置如下:
# Redirect to https
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: redirectshttps
namespace: kube-system
spec:
redirectScheme:
scheme: https
permanent: true
说明:
•redirectScheme: 协议重定向
•scheme: https: HTTP 协议重定向为 HTTPS
•permanent: true: 设置为 true 以应用永久重定向。
启用 HSTS
Traefik CRD Middleware - hsts-header 配置如下:
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: hsts-header
namespace: kube-system
spec:
headers:
customResponseHeaders:
Strict-Transport-Security: 'max-age=63072000'
•customResponseHeaders 应用于响应头的名称和值。
•Strict-Transport-Security: 'max-age=63072000': 即 「HTTP严格传输安全」响应头,收到该响应头的浏览器会在 63072000s(约 2 年)的时间内,只要访问该网站,即使输入的是 http,浏览器会自动跳转到 https。(HSTS 是浏览器端的跳转,之前的「HTTP 重定向到 HTTPS」是服务器端的跳转)
具体域名配置
以上的所有配置,包括:
1.TLS 版本限定在 TLS 1.3
2.证书
3.HTTP 重定向到 HTTPS
4.启用 HSTS
都是全局的配置,接下来针对具体的域名 - 这里是 example.ewhisper.cn 进行配置。
使用 Traefik 的 CRD - IngressRoute[5] 配置如下:
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: example
namespace: cert-manager
spec:
entryPoints:
- websecure
- web
routes:
- match: Host(`example.ewhisper.cn`)
kind: Rule
middlewares:
- name: hsts-header
namespace: kube-system
- name: redirectshttps
namespace: kube-system
services:
- name: example
namespace: default
port: 8080
tls: {}
说明:
•entryPoints: EntryPoints 是进入 Traefik 的网络入口点。它们定义将接收数据包的端口,以及是否侦听 TCP 或 UDP。如下图所示:
entryPoints
这里 entryPoints 是静态配置,是直接静态配置在 Traefik Deployment 中的,如下图:
Traefik Deployment arg
•entryPoint - traefik 地址端口是::9000/tcp
•entryPoint - web 地址端口是::8000/tcp
•entryPoint - websecure 地址端口是::8443/tcp,且 tls 为 true
•然后,再通过 Serivce Type: LoadBalancer 暴露到公网的: 80 和 443 端口(至于entryPoint - traefik 则还没有通过 SVC 暴露,所以即使配了 IngressRoute 也无法访问),如下:
Traefik LoadBalancer SVC
•websecure 即:example.ewhisper.cn 可以通过 https://example.ewhisper.cn:443 访问;
•web 即:example.ewhisper.cn 可以通过 http://example.ewhisper.cn:80 访问;
•kind: Rule Rule 是一组配置了值的匹配器(即 match),它决定一个特定的请求是否匹配特定的条件。如果规则经过验证,Route 就会成为活动的,调用中间件,然后将请求转发给服务。
•match: Host(`example.ewhisper.cn`): 这里是检查请求域名(host 报头值)是否以给定域之一(即example.ewhisper.cn)为目标。
•middlewares: 连接到 Route 的中间件是在请求被发送到你的服务之前(或者在服务的回答被发送到客户端之前)对请求进行调整的一种方法。在trafik中有几种可用的中间件,一些可以修改请求、报头,一些负责重定向,一些添加身份验证,等等。使用相同协议的中间件可以组合成链,以适应每个场景。中间件作用如下图所示:
middlewares•name: hsts-header 启用 HSTS 的中间件(可以复用)
•name: redirectshttps 启用 HTTP 重定向到 HTTPS 的中间件(可以复用)
•services... 转发到 K8S default NameSpace 下的 example Service 的 8080 端口。
•tls: {} 配置为空,会使用默认的 TLSStore 中的证书。
配置生效
假设以上配置都放在 ./traefik-sec 目录下,执行如下命令生效:
kubectl apply -f ./traefik-sec
验证
浏览器访问
直接浏览器访问 http://example.ewhisper.cn 域名,跳转到 http://example.ewhisper.cn, 并且证书已生效。
HTTP 重定向到 HTTPS 已生效
通过 SSL Labs 验证
在 SSL Labs 的 SSL Server Test[6] 下进行验证。验证结果如下:
A
评分为 A,且 HSTS 已启用
证书信息
证书为 *.ewhisper.cn 合法证书
TLS 协议
TLS 协议只支持 TLS 1.3
参考资料
•使用 cert-manager 为 dnspod 的域名签发免费证书 | 作者 roc[7]
•Traefik 官方文档[8]
•Traefik2.3.x 使用大全(更新版) | 作者 阳明
•Mozilla SSL Configuration Generator[9]
References
[1] Traefik: https://traefik.io/ [2] TLSOption: https://doc.traefik.io/traefik/routing/providers/kubernetes-crd/#kind-tlsoption [3] TLSStore: https://doc.traefik.io/traefik/routing/providers/kubernetes-crd/#kind-tlsstore [4] Middleware: https://doc.traefik.io/traefik/middlewares/overview/ [5] IngressRoute: https://doc.traefik.io/traefik/providers/kubernetes-crd/ [6] SSL Labs 的 SSL Server Test: https://www.ssllabs.com/ssltest/ [7] 使用 cert-manager 为 dnspod 的域名签发免费证书 | 作者 roc: https://imroc.cc/k8s/trick/cert-manager-webhook-dnspod/ [8] Traefik 官方文档: https://doc.traefik.io/traefik/ [9] Mozilla SSL Configuration Generator: https://ssl-config.mozilla.org/
版权声明
本文为[东风微鸣]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1985815
边栏推荐
- Saving notes 20: transforming notebook computer (ASUS a555l)
- Redis系列三:thinkphp 使用 redis
- 跨域处理的几种方案
- 【深入理解TcaplusDB技术】删除列表所有数据接口说明——[List表]
- 【图】207. 课程表
- TcaplusDB君 · 行业新闻汇编(三)
- JVM garbage collection parnew and CMS introduction
- 【深入理解TcaplusDB技术】删除数据示例代码——[Generic表]
- 如何通过云效Projex项目协同提高团队更高效的协作能力
- 【深入理解TcaplusDB技术】读取列表指定位置数据示例代码——[List表]
猜你喜欢

TcaplusDB君 · 行业新闻汇编(四)

【深入理解TcaplusDB技术】示例代码——数据分批返回
![[Mysql] CHAR_ Length function](/img/1c/57bc70f35429d0add2dbee99e06c1c.png)
[Mysql] CHAR_ Length function

小心间谍出没!维护国家安全,你我共同守护
![【深入理解TcaplusDB技术】删除数据示例代码——[Generic表]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
【深入理解TcaplusDB技术】删除数据示例代码——[Generic表]

分布式任務調度平臺XXL-JOB安裝及使用

学校固定资产管理系统由谁来做,云呐RFID固定资产管理系统
![[in depth understanding of tcallusdb technology] description of data interface for batch deletion of specified location in list - [list table]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcallusdb technology] description of data interface for batch deletion of specified location in list - [list table]
![[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]

Development trend of C in 2022
随机推荐
ROS communication mechanism III - parameter server
Android的UI---ZoomControls放大缩小图片,android面试简历模板
学校固定资产管理系统由谁来做,云呐RFID固定资产管理系统
How to select current clamp or current probe
职场PUA,管理者的五宗罪
When our son was 18 years old and went to college, we were already 60. What financial products are the most reliable for him to buy?
2022危险化学品经营单位主要负责人考试题模拟考试题库模拟考试平台操作
如何选择电流钳或电流探头
【无标题】
2022茶艺师(中级)考试题及答案
C语言的基本练习(001-1)
[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]
ROS通信机制三---参数服务器
【深入理解TcaplusDB技术】示例代码——数据分批返回
redis 获取 list 中的所有元素
Goldfish rhca memoirs: rh358 managing DNS and DNS server -- troubleshooting DNS problems
微服务技术概览
TcaplusDB君 · 行业新闻汇编(三)
WxWidgets learning notes (I): Interpretation of code:: blocks template project source code
人脸识别 (5) 基于MCTNN人脸检测(Pytorch)