参考之前写的一篇文档:
https://blog.51cto.com/michaelkang/1918192
traefik:v1.7
k8s:v1.15.1
Ingress是自kubernetes1.1版本后引入的资源类型。必须要部署Ingress controller才能创建Ingress资源,Ingress controller是以一种插件的形式提供。
反向代理负载均衡器
Ingress Controller
Ingress
反向代理负载均衡器很简单,类似nginx,haproxy;在集群中反向代理负载均衡器可以自由部署,可以使用 Replication Controller、Deployment、DaemonSet 等等,推荐DaemonSet 的方式部署
Ingress Controller 实质上可以理解为是个监视器,Ingress Controller 通过不断地跟 kubernetes API 打交道,实时的感知后端 service、pod 等变化,比如新增和减少 pod,service 增加与减少等;当得到这些变化信息后,Ingress Controller 再结合下文的 Ingress 生成配置,然后更新反向代理负载均衡器,并刷新其配置,达到服务发现的作用
Ingress 简单理解就是个规则定义;比如说某个域名对应某个 service,即当某个域名的请求进来时转发给某个 service;这个规则将与 Ingress Controller 结合,然后 Ingress Controller 将其动态写入到负载均衡器配置中,从而实现整体的服务发现和负载均衡
如果您不熟悉Kubernetes中的Ingresses,您可能需要阅读Kubernetes用户指南.
一个正常工作Kubernetes集群。可以是 minikube 集群。
[root@kubm-02 traefik]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP
kubm-01 Ready master 13d v1.15.1 172.20.101.157
kubm-02 Ready master 13d v1.15.1 172.20.101.164
kubm-03 Ready master 13d v1.15.1 172.20.101.165
kubnode-01 Ready <none> 13d v1.15.1 172.20.101.160
kubnode-02 Ready <none> 13d v1.15.1 172.20.101.166
kubnode-03 Ready <none> 13d v1.15.1 172.20.101.167
Kubernetes在1.6+中引入了基于角色的访问控制(RBAC),以允许对Kubernetes资源和API进行细粒度控制。
如果您的群集配置了RBAC,则需要授权Traefik使用Kubernetes API。有两种方法可以设置适当的权限:通过特定于命名空间的RoleBindings或单个全局ClusterRoleBinding。
每个命名空间的RoleBinding可以限制授予权限,只有Traefik正在监视的名称空间才能使用,从而遵循最小权限原则。如果Traefik不应该监视所有名称空间,并且名称空间集不会动态更改,那么这是首选方法。否则,必须使用单个ClusterRoleBinding。
ClusterRoleBinding:
---
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
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: traefik-ingress-controller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: traefik-ingress-controller
subjects:
- kind: ServiceAccount
name: traefik-ingress-controller
namespace: kube-system
[root@kubm-02 ~]# kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-rbac.yaml
clusterrole.rbac.authorization.k8s.io/traefik-ingress-controller created
clusterrolebinding.rbac.authorization.k8s.io/traefik-ingress-controller created
Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。
DaemonSet的不同之处在于,每个 Node 上最多只能运行一个副本。
本次采用DaemonSet方式部署
Deployment:
kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-deployment.yaml
DaemonSet :
kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-ds.yaml
[root@kubm-02 traefik]# kubectl get rc,services -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
...
service/traefik-ingress-service ClusterIP 10.245.153.125 <none> 80/TCP,8080/TCP 3m42s
[root@kubm-02 ~]# kubectl get pods --all-namespaces -o wide --selector=k8s-app=traefik-ingress-lb
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system traefik-ingress-controller-4d29d 1/1 Running 0 19m 10.244.3.107 kubnode-01 <none> <none>
kube-system traefik-ingress-controller-mgljm 1/1 Running 0 19m 10.244.5.143 kubnode-03 <none> <none>
kube-system traefik-ingress-controller-wcd5z 1/1 Running 0 19m 10.244.4.126 kubnode-02 <none> <none>
该服务将公开两个允许访问入口和Web界面的NodePort。
80 业务端口,后端服务启动注册到traefik后,写hosts文件或者添加dns 解析才能访问;
8080 traefik 管理页面,访问node节点的IP地址:8080即可访问;
例如:http://172.20.101.167:8080
首先创建一个服务和一个将公开Traefik Web UI的Ingress
部署服务:
apiVersion: v1
kind: Service
metadata:
name: traefik-web-ui
namespace: kube-system
spec:
selector:
k8s-app: traefik-ingress-lb
ports:
- name: web
port: 80
targetPort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: traefik-web-ui
namespace: kube-system
spec:
rules:
- host: traefik-ui.minikube
http:
paths:
- path: /
backend:
serviceName: traefik-web-ui
servicePort: web
kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/ui.yaml
任务执行完成,在/etc/hosts文件中将traefik-ui.minikube指向一个node节点
IP地址,然后浏览器访问:http://traefik-ui.minikube
或者
先打开traefik管理页面,通过curl访问,health 页面可以看到请求,指命令如下:
[root@kubm-02 ~]# curl http://172.20.101.166 --user-agent "Mozilla/5.0" -H "Host:traefik-ui.minikube"
<a href="/dashboard/">Found</a>.
A. htpasswd用于创建包含用户名和MD5编码密码的文件:
yum install httpd -y
htpasswd -c ./auth myusername
系统将提示您输入密码,您必须输入两次密码。 htpasswd将使用以下内容创建一个文件:
[root@kubm-02 traefik]# more auth
myusername:$apr1$3yj4XbDF$4ekQISLfP8HyX9nYH3x9E.
B.现在使用kubectl创建monitoring的文件在命名空间中创建一个秘密htpasswd。
[root@kubm-02 traefik]# kubectl create namespace monitoring
namespace/monitoring created
[root@kubm-02 traefik]# kubectl create secret generic mysecret --from-file auth --namespace=monitoring
secret/mysecret created
注意: Secret必须与Ingress对象位于相同的名称空间中。
C.将以下注释附加到Ingress对象:
traefik.ingress.kubernetes.io/auth-type: "basic"
traefik.ingress.kubernetes.io/auth-secret: "mysecret"
它们指定基本身份验证并引用mysecret包含凭据的Secret 。
以下是基于普罗米修斯的完整Ingress示例:
cat >prometheus-ingress.yaml<<EOF
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: prometheus-dashboard
namespace: monitoring
annotations:
kubernetes.io/ingress.class: traefik
traefik.ingress.kubernetes.io/auth-type: "basic"
traefik.ingress.kubernetes.io/auth-secret: "mysecret"
spec:
rules:
- host: dashboard.prometheus.example.com
http:
paths:
- backend:
serviceName: prometheus
servicePort: 9090
EOF
kubectl create -f prometheus-ingress.yaml -n monitoring
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: stilton
labels:
app: cheese
cheese: stilton
spec:
replicas: 2
selector:
matchLabels:
app: cheese
task: stilton
template:
metadata:
labels:
app: cheese
task: stilton
version: v0.0.1
spec:
containers:
- name: cheese
image: errm/cheese:stilton
ports:
- containerPort: 80
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: cheddar
labels:
app: cheese
cheese: cheddar
spec:
replicas: 2
selector:
matchLabels:
app: cheese
task: cheddar
template:
metadata:
labels:
app: cheese
task: cheddar
version: v0.0.1
spec:
containers:
- name: cheese
image: errm/cheese:cheddar
ports:
- containerPort: 80
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: wensleydale
labels:
app: cheese
cheese: wensleydale
spec:
replicas: 2
selector:
matchLabels:
app: cheese
task: wensleydale
template:
metadata:
labels:
app: cheese
task: wensleydale
version: v0.0.1
spec:
containers:
- name: cheese
image: errm/cheese:wensleydale
ports:
- containerPort: 80
kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/cheese-deployments.yaml
[root@kubm-02 traefik]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cheddar-845749dbd6-vht86 0/1 ContainerCreating 0 64s
cheddar-845749dbd6-z4zn6 1/1 Running 0 64s
curl-6bf6db5c4f-96nhg 1/1 Running 1 46h
stilton-f89c97cdb-dtgbx 1/1 Running 0 64s
stilton-f89c97cdb-nlhn5 1/1 Running 0 64s
wensleydale-7c5ff658b-lq5tn 0/1 ContainerCreating 0 64s
wensleydale-7c5ff658b-ps56g 1/1 Running 0 64s
---
apiVersion: v1
kind: Service
metadata:
name: stilton
spec:
ports:
- name: http
targetPort: 80
port: 80
selector:
app: cheese
task: stilton
---
apiVersion: v1
kind: Service
metadata:
name: cheddar
spec:
ports:
- name: http
targetPort: 80
port: 80
selector:
app: cheese
task: cheddar
---
apiVersion: v1
kind: Service
metadata:
name: wensleydale
annotations:
traefik.backend.circuitbreaker: "NetworkErrorRatio() > 0.5"
spec:
ports:
- name: http
targetPort: 80
port: 80
selector:
app: cheese
task: wensleydale
OR
kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/cheese-services.yaml
[root@kubm-02 traefik]# kubectl get svc --all-namespaces
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default cheddar ClusterIP 10.245.80.192 <none> 80/TCP 50s
default stilton ClusterIP 10.245.177.114 <none> 80/TCP 50s
default wensleydale ClusterIP 10.245.242.131 <none> 80/TCP 50s
注意:我们还通过在服务上设置注释来为其中一个后端设置断路器表达式traefik.backend.circuitbreaker。
kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/cheese-ingress.yaml
任务执行完成可以在traefik 管理页面看到新的域名已经注册到traefik,写hosts文件在浏览器可以访问:
172.20.101.166 stilton.minikube
172.20.101.166 cheddar.minikube
172.20.101.166 wensleydale.minikube
浏览器访问:
http://stilton.minikube
http://cheddar.minikube
http://wensleydale.minikube
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: cheeses
annotations:
kubernetes.io/ingress.class: traefik
traefik.frontend.rule.type: PathPrefixStrip
spec:
rules:
- host: cheeses.minikube
http:
paths:
- path: /stilton
backend:
serviceName: stilton
servicePort: http
- path: /cheddar
backend:
serviceName: cheddar
servicePort: http
- path: /wensleydale
backend:
serviceName: wensleydale
servicePort: http
OR
kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/cheeses-ingress.yaml
#写 host
echo "172.20.101.166 cheeses.minikube" | sudo tee -a /etc/hosts
#访问验证
http://cheeses.minikube/cheddar
http://cheeses.minikube/stilton
http://cheeses.minikube/wensleydale
注意:配置Traefik以使用traefik.frontend.rule.type注释从url路径中去除前缀,以便我们可以使用前一个示例中的容器而无需修改。
有时您需要为入口路由指定优先级,尤其是在处理通配符路由时。这可以通过添加traefik.frontend.priority注释来完成,即:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: wildcard-cheeses
annotations:
traefik.frontend.priority: "1"
spec:
rules:
- host: *.minikube
http:
paths:
- path: /
backend:
serviceName: stilton
servicePort: http
kind: Ingress
metadata:
name: specific-cheeses
annotations:
traefik.frontend.priority: "2"
spec:
rules:
- host: specific.minikube
http:
paths:
- path: /
backend:
serviceName: stilton
servicePort: http
请注意,必须引用优先级值以避免数字解释(对于注释是非法的)。
指定扩展域名时,Traefik会相应地将请求转发给给定主机,并在服务端口与443匹配时使用HTTPS。这仍然需要在服务上从Ingress端口到(外部)服务端口设置正确的端口映射。
禁用传递主机头,默认情况下,Traefik会将传入的主机头传递给上游资源。但是,有时可能不希望出现这种情况。例如,如果您的服务属于需要使用很多域名的类型。
1:全局禁用:
将以下内容添加到TOML配置文件中:
disablePassHostHeaders = true
2:每个Ingress禁用:
要禁用每个入口资源传递主机标头traefik.frontend.passHostHeader,请将入口上的注释设置为"false"。
示例定义:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: example
annotations:
kubernetes.io/ingress.class: traefik
traefik.frontend.passHostHeader: "false"
spec:
rules:
- host: example.com
http:
paths:
- path: /static
backend:
serviceName: static
servicePort: https
以及一个示例服务定义:
apiVersion: v1
kind: Service
metadata:
name: static
spec:
ports:
- name: https
port: 443
type: ExternalName
externalName: static.otherdomain.com
如果您要访问example.com/static该请求,那么将传递给static.otherdomain.com/static,static.otherdomain.com并将收到带有Host头的请求static.otherdomain.com。
注意:
每个入口注释会覆盖设置为全局值的任何内容。所以,你可以设置disablePassHostHeaders到true您的TOML配置文件,然后使通过每一个想要的域名访问。
一般部署两种不同类型的traefik:
面向内部(internal)服务的traefik,建议可以使用deployment的方式
面向外部(external)服务的traefik,建议可以使用daemonset的方式
建议使用traffic-type标签
traffic-type: external
traffic-type: internal
traefik相应地使用labelSelector
traffic-type=internal
traffic-type=external
可以使用服务权重在多个部署之间以细粒度方式拆分Ingress流量。让较新版本的部署将随着时间的推移接收最初较小但不断增加的请求部分。在Traefik中可以这样做的方法是指定应该进入每个部署的请求的百分比。
例如,假设一个应用程序my-app在版本1中运行。较新的版本2即将发布,但对生产中运行的新版本的稳健性和可靠性的信心只能逐渐获得。因此,my-app-canary创建新部署并将其缩放到足以获得1%流量份额的副本计数。与此同时,像往常一样创建一个Service对象。
Ingress规范看起来像这样:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
traefik.ingress.kubernetes.io/service-weights: |
my-app: 99%
my-app-canary: 1%
name: my-app
spec:
rules:
- http:
paths:
- backend:
serviceName: my-app
servicePort: 80
path: /
- backend:
serviceName: my-app-canary
servicePort: 80
path: /
traefik.ingress.kubernetes.io/service-weights注释:它指定引用的后端服务之间的请求分配,my-app以及my-app-canary。根据这一定义,Traefik将99%的请求路由到my-app部署支持的pod ,并将1%的请求路由到支持的pod my-app-canary。随着时间的推移,该比例可能会慢慢转向金丝雀部署,直到它被认为取代之前的主要应用程序,步骤如5%/ 95%,10%/ 90%,50%/ 50%,最后100%/ 0%。
指定服务权重时,出于方便原因,可以省略一个服务。
例如,以下定义显示了如何在金丝雀发布伴随基线部署的情况下拆分请求,以便更轻松地进行指标比较或自动化金丝雀分析:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
traefik.ingress.kubernetes.io/service-weights: |
my-app-canary: 10%
my-app-baseline: 10%
name: app
spec:
rules:
- http:
paths:
- backend:
serviceName: my-app-canary
servicePort: 80
path: /
- backend:
serviceName: my-app-baseline
servicePort: 80
path: /
- backend:
serviceName: my-app-main
servicePort: 80
path: /
此配置my-app-main自动分配80%的流量,从而使用户无需手动完成百分比值。当连续增加金丝雀释放的份额时,这变得很方便。
使用Traefik舵表部署¶
注意
Helm Chart由社区维护,而不是Traefik项目维护人员。
您也可以使用Traefik Helm图表,而不是直接通过Kubernetes对象安装Traefik。
通过以下方式安装Traefik图表:
helm install stable/traefik
使用values.yaml文件安装Traefik图表。
helm install --values values.yaml stable/traefik
dashboard:
enabled: true
domain: traefik-ui.minikube
kubernetes:
namespaces:
kubectl delete -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/cheeses-ingress.yaml
kubectl delete -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/cheese-ingress.yaml
kubectl delete -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/cheese-services.yaml
kubectl delete -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/cheese-deployments.yaml
kubectl delete -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/ui.yaml
kubectl delete -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-ds.yaml
kubectl delete -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-rbac.yaml
https://docs.traefik.io/user-guide/kubernetes/
https://github.com/containous/traefik/tree/v1.7.14
https://juejin.im/post/5b46119ce51d455d94713787
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。