前文已经介绍过了k8s ingress的实现原理和配置,本文将介绍替代方案traefik。在开始配置之前我们先回顾一下ingress发布k8s服务的实现方案,ingress方案需要使用下列的组件:
1、反向代理负载均衡器
2、ingress control
3、ingress
其中ingress control负责同apiserver进行通信,监测pod和service的变化,并更新反向代理负载均衡器的配置,同时让反向代理负载均衡器重载配置。
接下来我们来看看traefik,官网地址:https://traefik.io/
Traefik (pronounced like traffic) is a modern HTTP reverse proxy and load balancer made to deploy microservices with ease. It supports several backends (Docker,Swarm mode,Kubernetes,Marathon, Consul,Etcd,Rancher,Amazon ECS, and a lot more) to manage its configuration automatically and dynamically.
Traefik本质上是一个http的方向代理和负载均衡,可以支持Docker, Swarm mode, Kubernetes, Marathon, Consul, Etcd, Rancher, Amazon ECS等后端服务,动态的管理这些服务的配置文件(我们可以理解为自动发现这些后端服务的配置变更,并重新加载服务的配置)
下图是traefik的工作原理示意图:
Traefik的主要特点:
1、Go语音编写、无需安装其他依赖包、速度快
2、支持Rest API、多后端类型支持
3、支持配置文件热加载,不需要重启app进程
4、支持Round Robin, rebalancer load-balancers等负载均衡策略
5、自带AngularJS Web UI图形化界面
6、支持https、自动更新https证书
7、支持websocket、HTTP/2, GRPC、高可用集群等
8、支持网络错误重试、后端自动熔断(当后端应用错误数过多的时候,可以自动熔断)
总而言之,在k8s集群中服务发布的方案选择,Traefik可用作为ingress的替代解决方案,traefik的特点足够说服我们将ingress替换成Traefik,下面我们开始在k8s 1.5.2集群环境中使用Traefik来发布服务。
一、通过yaml文件已daemonset方式运行traefik
# mkdir traefik
# cd traefik
# docker pullk docker.io/traefik# cat traefik.ds.yaml
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: traefik-ingress-lb
namespace: default
labels:
k8s-app: traefik-ingress-lb
spec:
template:
metadata:
labels:
k8s-app: traefik-ingress-lb
name: traefik-ingress-lb
spec:
terminationGracePeriodSeconds: 60
hostNetwork: true
restartPolicy: Always
containers:
- image: docker.io/traefik
name: traefik-ingress-lb
resources:
limits:
cpu: 200m
memory: 30Mi
requests:
cpu: 100m
memory: 20Mi
ports:
- name: http
containerPort: 80
hostPort: 80
- name: admin
containerPort: 8081
args:
- --web
- --web.address=:8081
- --kubernetes
- --kubernetes.endpoint=http://192.168.115.5:8080
二、通过yaml文件创建webui的service、ingress
# cat ui.yaml
apiVersion: v1
kind: Service
metadata:
name: traefik-web-ui
namespace: default
spec:
selector:
k8s-app: traefik-ingress-lb
ports:
- name: web
port: 80
targetPort: 8081
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: traefik-web-ui
namespace: default
spec:
rules:
- host: traefik-ui.local
http:
paths:
- path: /
backend:
serviceName: traefik-web-ui
servicePort: web
三、通过yaml文件创建 kubernetes-dashboard和frontend的ingress
# cat traefik.ing.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: traefik-ingress
spec:
rules:
- host: k8s.webui
http:
paths:
- path: /
backend:
serviceName: kubernetes-dashboard
servicePort: 80
- host: k8s.frontend
http:
paths:
- path: /
backend:
serviceName: frontend
servicePort: 80
三、通过上述yaml配置文件创建pod和ingress
# cd kubernetes/traefik/
# kubectl get pod
# kubectl get svc
# kubectl create -f .
# kubectl get pod
# kubectl get svc
# kubectl get ingress