目录[-]
系统环境:
地址:
Traefik 最新推出了 v2.1 版本,这里将 Traefik 升级到最新版本,简单的介绍了下如何在 Kubernetes 环境下安装 Traefik v2.1,下面将介绍如何在 Kubernetes 环境下部署并配置 Traefik v2.1。
注意:这里 Traefik 是部署在 Kube-system Namespace 下,如果不是需要修改下面部署文件中的 Namespace 属性。
在 traefik v2.1 版本后,开始使用 CRD(Custom Resource Definition)来完成路由配置等,所以需要提前创建 CRD 资源。
创建 traefik-crd.yaml 文件
## IngressRoute
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:name: ingressroutes.traefik.containo.us
spec:scope: Namespacedgroup: traefik.containo.usversion: v1alpha1names:kind: IngressRouteplural: ingressroutessingular: ingressroute
---
## IngressRouteTCP
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:name: ingressroutetcps.traefik.containo.us
spec:scope: Namespacedgroup: traefik.containo.usversion: v1alpha1names:kind: IngressRouteTCPplural: ingressroutetcpssingular: ingressroutetcp
---
## Middleware
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:name: middlewares.traefik.containo.us
spec:scope: Namespacedgroup: traefik.containo.usversion: v1alpha1names:kind: Middlewareplural: middlewaressingular: middleware
---
## TLSOption
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:name: tlsoptions.traefik.containo.us
spec:scope: Namespacedgroup: traefik.containo.usversion: v1alpha1names:kind: TLSOptionplural: tlsoptionssingular: tlsoption
---
## TraefikService
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:name: traefikservices.traefik.containo.us
spec:scope: Namespacedgroup: traefik.containo.usversion: v1alpha1names:kind: TraefikServiceplural: traefikservicessingular: traefikservice
创建 Traefik CRD 资源
$ kubectl apply -f traefik-crd.yaml
Kubernetes 在 1.6 版本中引入了基于角色的访问控制(RBAC)策略,方便对 Kubernetes 资源和 API 进行细粒度控制。Traefik 需要一定的权限,所以这里提前创建好 Traefik ServiceAccount 并分配一定的权限。
创建 traefik-rbac.yaml 文件
## ServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:namespace: kube-systemname: traefik-ingress-controller
---
## ClusterRole
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:name: traefik-ingress-controller
rules:- apiGroups: [""]resources: ["services","endpoints","secrets"]verbs: ["get","list","watch"]- apiGroups: ["extensions"]resources: ["ingresses"]verbs: ["get","list","watch"]- apiGroups: ["extensions"]resources: ["ingresses/status"]verbs: ["update"]- apiGroups: ["traefik.containo.us"]resources: ["middlewares"]verbs: ["get","list","watch"]- apiGroups: ["traefik.containo.us"]resources: ["ingressroutes"]verbs: ["get","list","watch"]- apiGroups: ["traefik.containo.us"]resources: ["ingressroutetcps"]verbs: ["get","list","watch"]- apiGroups: ["traefik.containo.us"]resources: ["tlsoptions"]verbs: ["get","list","watch"]- apiGroups: ["traefik.containo.us"]resources: ["traefikservices"]verbs: ["get","list","watch"]
---
## ClusterRoleBinding
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:name: traefik-ingress-controller
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: traefik-ingress-controller
subjects:- kind: ServiceAccountname: traefik-ingress-controllernamespace: kube-system
创建 Traefik RBAC 资源
$ kubectl apply -f traefik-rbac.yaml -n kube-system
由于 Traefik 配置很多,通过 CLI 定义不是很方便,一般时候选择将其配置选项放到配置文件中,然后存入 ConfigMap,将其挂入 traefik 中。
创建 traefik-config.yaml 文件
kind: ConfigMap
apiVersion: v1
metadata:name: traefik-config
data:traefik.yaml: |-ping: "" ## 启用 PingserversTransport:insecureSkipVerify: true ## Traefik 忽略验证代理服务的 TLS 证书api:insecure: true ## 允许 HTTP 方式访问 APIdashboard: true ## 启用 Dashboarddebug: false ## 启用 Debug 调试模式metrics:prometheus: "" ## 配置 Prometheus 监控指标数据,并使用默认配置entryPoints:web:address: ":80" ## 配置 80 端口,并设置入口名称为 webwebsecure:address: ":443" ## 配置 443 端口,并设置入口名称为 websecureproviders:kubernetesCRD: "" ## 启用 Kubernetes CRD 方式来配置路由规则kubernetesIngress: "" ## 启动 Kubernetes Ingress 方式来配置路由规则log:filePath: "" ## 设置调试日志文件存储路径,如果为空则输出到控制台level: error ## 设置调试日志级别format: json ## 设置调试日志格式accessLog:filePath: "" ## 设置访问日志文件存储路径,如果为空则输出到控制台format: json ## 设置访问调试日志格式bufferingSize: 0 ## 设置访问日志缓存行数filters:#statusCodes: ["200"] ## 设置只保留指定状态码范围内的访问日志retryAttempts: true ## 设置代理访问重试失败时,保留访问日志minDuration: 20 ## 设置保留请求时间超过指定持续时间的访问日志fields: ## 设置访问日志中的字段是否保留(keep 保留、drop 不保留)defaultMode: keep ## 设置默认保留访问日志字段names: ## 针对访问日志特别字段特别配置保留模式ClientUsername: drop headers: ## 设置 Header 中字段是否保留defaultMode: keep ## 设置默认保留 Header 中字段names: ## 针对 Header 中特别字段特别配置保留模式User-Agent: redactAuthorization: dropContent-Type: keep
创建 Traefik ConfigMap 资源
$ kubectl apply -f traefik-config.yaml -n kube-system
由于是 Kubernetes DeamonSet 这种方式部署 Traefik,所以需要提前给节点设置 Label,这样当程序部署时 Pod 会自动调度到设置 Label 的节点上。
节点设置 Label 标签
$ kubectl label nodes k8s-node-2-12 IngressProxy=true
查看节点是否设置 Label 成功
$ kubectl get nodes --show-labelsNAME STATUS ROLES VERSION LABELS
k8s-master-2-11 Ready master v1.16.3 kubernetes.io/hostname=k8s-master-2-11,node-role.kubernetes.io/master=
k8s-node-2-12 Ready
k8s-node-2-13 Ready
k8s-node-2-14 Ready
如果想删除标签,可以使用
kubectl label nodes k8s-node-2-12 IngressProxy-
命令
一样,用 DaemonSet 方式部署,便于在多服务器间扩展,用 hostport 方式占用服务器 80、443 端口,方便流量进入。
创建 traefik 部署文件 traefik-deploy.yaml
apiVersion: v1
kind: Service
metadata:name: traefik
spec:ports:- name: webport: 80- name: websecureport: 443- name: adminport: 8080selector:app: traefik
---
apiVersion: apps/v1
kind: DaemonSet
metadata:name: traefik-ingress-controllerlabels:app: traefik
spec:selector:matchLabels:app: traefiktemplate:metadata:name: traefiklabels:app: traefikspec:serviceAccountName: traefik-ingress-controllerterminationGracePeriodSeconds: 1containers:- image: traefik:v2.1.2name: traefik-ingress-lbports:- name: webcontainerPort: 80hostPort: 80 ## 将容器端口绑定所在服务器的 80 端口- name: websecurecontainerPort: 443hostPort: 443 ## 将容器端口绑定所在服务器的 443 端口- name: admincontainerPort: 8080 ## Traefik Dashboard 端口resources:limits:cpu: 2000mmemory: 1024Mirequests:cpu: 1000mmemory: 1024MisecurityContext:capabilities:drop:- ALLadd:- NET_BIND_SERVICEargs:- --configfile=/config/traefik.yamlvolumeMounts:- mountPath: "/config"name: "config"volumes:- name: configconfigMap:name: traefik-config tolerations: ## 设置容忍所有污点,防止节点被设置污点- operator: "Exists"nodeSelector: ## 设置node筛选器,在特定label的节点上启动IngressProxy: "true"
Kubernetes 部署 Traefik
$ kubectl apply -f traefik-deploy.yaml -n kube-system
到此 Traefik v2.1 应用已经部署完成。
Traefik 应用已经部署完成,但是想让外部访问 Kubernetes 内部服务,还需要配置路由规则,这里开启了 Traefik Dashboard 配置,所以首先配置 Traefik Dashboard 看板的路由规则,使外部能够访问 Traefik Dashboard。
创建 Traefik Dashboard 路由规则文件 traefik-dashboard-route.yaml
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:name: traefik-dashboard-route
spec:entryPoints:- webroutes:- match: Host(`traefik.mydlq.club`)kind: Ruleservices:- name: traefikport: 8080
创建 Traefik Dashboard 路由规则对象
$ kubectl apply -f traefik-dashboard-route.yaml -n kube-system
接下来配置 Hosts,客户端想通过域名访问服务,必须要进行 DNS 解析,由于这里没有 DNS 服务器进行域名解析,所以修改 hosts 文件将 Traefik 指定节点的 IP 和自定义 host 绑定。打开电脑的 Hosts 配置文件,往其加入下面配置:
192.168.2.12 traefik.mydlq.club
配置完成后,打开浏览器输入地址:http://traefik.mydlq.club 打开 Traefik Dashboard。
这里我们创建 Kubernetes 的 Dashboard 看板,它是 Https 协议方式,由于它是需要使用 Https 请求,所以我们配置基于 Https 的路由规则并指定证书。
创建证书文件
# 创建自签名证书
$ openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=cloud.mydlq.club"# 将证书存储到 Kubernetes Secret 中
$ kubectl create secret generic cloud-mydlq-tls --from-file=tls.crt --from-file=tls.key -n kube-system
创建 Traefik Dashboard 路由规则文件 kubernetes-dashboard-route.yaml
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:name: kubernetes-dashboard-route
spec:entryPoints:- websecuretls:secretName: cloud-mydlq-tlsroutes:- match: Host(`cloud.mydlq.club`) kind: Ruleservices:- name: kubernetes-dashboardport: 443
创建 Kubernetes Dashboard 路由规则对象
$ kubectl apply -f kubernetes-dashboard-route.yaml -n kube-system
跟上面一样,配置 Hosts 文件
192.168.2.12 cloud.mydlq.club
配置完成后,打开浏览器输入地址:https://cloud.mydlq.club 打开 Dashboard Dashboard。
到此文章结束,可以访问我的 Github 下载 部署文件,别忘点颗 Start!!