安装traefik ingress

Ingress简介

简单的说,ingress就是从kubernetes集群外访问集群的入口,将用户的URL请求转发到不同的service上。Ingress相当于nginx、apache等负载均衡方向代理服务器,其中还包括规则定义,即URL的路由信息,路由信息得的刷新由Ingress controller来提供。

Ingress Controller 实质上可以理解为是个监视器,Ingress Controller 通过不断地跟 kubernetes API 打交道,实时的感知后端 service、pod 等变化,比如新增和减少 pod,service 增加与减少等;当得到这些变化信息后,Ingress Controller 再结合下文的 Ingress 生成配置,然后更新反向代理负载均衡器,并刷新其配置,达到服务发现的作用。

部署Traefik

介绍traefik

Traefik是一款开源的反向代理与负载均衡工具。它最大的优点是能够与常见的微服务系统直接整合,可以实现自动化动态配置。目前支持Docker, Swarm, Mesos/Marathon, Mesos, Kubernetes, Consul, Etcd, Zookeeper, BoltDB, Rest API等等后端模型。

以下配置文件可以在Traefik GitHub仓库中的examples/k8s/traefik-rbac.yaml找到。

创建ingress-rbac.yaml

将用于service account验证。

  1. ---
  2. kind: ClusterRole
  3. apiVersion: rbac.authorization.k8s.io/v1beta1
  4. metadata:
  5. name: traefik-ingress-controller
  6. rules:
  7. - apiGroups:
  8. - ""
  9. resources:
  10. - services
  11. - endpoints
  12. - secrets
  13. verbs:
  14. - get
  15. - list
  16. - watch
  17. - apiGroups:
  18. - extensions
  19. resources:
  20. - ingresses
  21. verbs:
  22. - get
  23. - list
  24. - watch
  25. ---
  26. kind: ClusterRoleBinding
  27. apiVersion: rbac.authorization.k8s.io/v1beta1
  28. metadata:
  29. name: traefik-ingress-controller
  30. roleRef:
  31. apiGroup: rbac.authorization.k8s.io
  32. kind: ClusterRole
  33. name: traefik-ingress-controller
  34. subjects:
  35. - kind: ServiceAccount
  36. name: traefik-ingress-controller
  37. namespace: kube-system
  1. kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/traefik-rbac.yaml

创建Depeloyment

  1. ---
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: traefik-ingress-controller
  6. namespace: kube-system
  7. ---
  8. kind: Deployment
  9. apiVersion: extensions/v1beta1
  10. metadata:
  11. name: traefik-ingress-controller
  12. namespace: kube-system
  13. labels:
  14. k8s-app: traefik-ingress-lb
  15. spec:
  16. replicas: 1
  17. selector:
  18. matchLabels:
  19. k8s-app: traefik-ingress-lb
  20. template:
  21. metadata:
  22. labels:
  23. k8s-app: traefik-ingress-lb
  24. name: traefik-ingress-lb
  25. spec:
  26. serviceAccountName: traefik-ingress-controller
  27. terminationGracePeriodSeconds: 60
  28. containers:
  29. - image: traefik
  30. name: traefik-ingress-lb
  31. args:
  32. - --web
  33. - --kubernetes
  34. ---
  35. kind: Service
  36. apiVersion: v1
  37. metadata:
  38. name: traefik-ingress-service
  39. spec:
  40. selector:
  41. k8s-app: traefik-ingress-lb
  42. ports:
  43. - protocol: TCP
  44. port: 80
  45. name: web
  46. - protocol: TCP
  47. port: 8080
  48. name: admin
  49. type: NodePort
  1. # 使用deployment部署
  2. kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/traefik-deployment.yaml
  3. # 也可以使用daemonset来部署
  4. # kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/traefik-ds.yaml

注意我们这里用的是Deploy类型,没有限定该pod运行在哪个主机上。Traefik的端口是8580。

创建名为traefik-ingress的ingress,文件名traefik.yaml

  1. apiVersion: extensions/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: traefik-ingress
  5. spec:
  6. rules:
  7. - host: traefik.nginx.io
  8. http:
  9. paths:
  10. - path: /
  11. backend:
  12. serviceName: nginx
  13. servicePort: 80
  14. - host: traefik.frontend.io
  15. http:
  16. paths:
  17. - path: /
  18. backend:
  19. serviceName: frontend
  20. servicePort: 80

这其中的backend中要配置default namespace中启动的service名字。path就是URL地址后的路径,如traefik.frontend.io/path,service将会接受path这个路径,host最好使用service-name.filed1.filed2.domain-name这种类似主机名称的命名方式,方便区分服务。

根据你自己环境中部署的service的名字和端口自行修改,有新service增加时,修改该文件后可以使用kubectl replace -f traefik.yaml来更新。

Traefik UI

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: traefik-web-ui
  5. namespace: kube-system
  6. spec:
  7. selector:
  8. k8s-app: traefik-ingress-lb
  9. ports:
  10. - port: 80
  11. targetPort: 8080
  12. ---
  13. apiVersion: extensions/v1beta1
  14. kind: Ingress
  15. metadata:
  16. name: traefik-web-ui
  17. namespace: kube-system
  18. annotations:
  19. kubernetes.io/ingress.class: traefik
  20. spec:
  21. rules:
  22. - host: traefik-ui.nginx.io
  23. http:
  24. paths:
  25. - backend:
  26. serviceName: traefik-web-ui
  27. servicePort: 80

配置完成后就可以启动treafik ingress了。

  1. kubectl create -f .

我查看到traefik的pod在172.20.0.115这台节点上启动了。

访问该地址http://172.20.0.115:8580/将可以看到dashboard。

kubernetes-dashboard

左侧黄色部分部分列出的是所有的rule,右侧绿色部分是所有的backend。

测试

在集群的任意一个节点上执行。假如现在我要访问nginx的”/“路径。

  1. $ curl -H Host:traefik.nginx.io http://172.20.0.115/
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Welcome to nginx!</title>
  6. <style>
  7. body {
  8. width: 35em;
  9. margin: 0 auto;
  10. font-family: Tahoma, Verdana, Arial, sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>Welcome to nginx!</h1>
  16. <p>If you see this page, the nginx web server is successfully installed and
  17. working. Further configuration is required.</p>
  18. <p>For online documentation and support please refer to
  19. <a href="http://nginx.org/">nginx.org</a>.<br/>
  20. Commercial support is available at
  21. <a href="http://nginx.com/">nginx.com</a>.</p>
  22. <p><em>Thank you for using nginx.</em></p>
  23. </body>
  24. </html>

如果你需要在kubernetes集群以外访问就需要设置DNS,或者修改本机的hosts文件。

在其中加入:

  1. 172.20.0.115 traefik.nginx.io
  2. 172.20.0.115 traefik.frontend.io

所有访问这些地址的流量都会发送给172.20.0.115这台主机,就是我们启动traefik的主机。

Traefik会解析http请求header里的Host参数将流量转发给Ingress配置里的相应service。

修改hosts后就就可以在kubernetes集群外访问以上两个service,如下图:

traefik-nginx

traefik-guestbook

参考文档