kubernetes 实战 2:Traefik 的安装和使用

目前经常使用的k8s经常使用服务暴露方式有 LoadBalancer、NodePort、Ingress 三种;node

  • LoadBalancer 依赖云服务商的环境,须要使用云服务商提供的底层和资源的支持,好比阿里云的 SLB。
  • NodePort 我的感受更适合我的学习、测试、或者某些特殊场景使用,由于服务一旦多了很难管理。
  • Ingress 主要提供反向代理和负载均衡功能,管理方便,能够做为集群服务的统一流量入口,目前有 traefik 和 nginx ingress,为何这里咱们要写 Traefik? 由于用的人多啊O.o,群众的眼睛是雪亮的...,Traefik 目前比 nginx ingress 要活跃不少,并且对后端节点的变化反应更迅速;nginx ingress 以前接触的时候还在beta版本,如今貌似是正式版了,也支持了grpc,后面有机会再尝试一下作个对比。

环境信息:

  • kubernetes version: v1.15.1
  • traefik version: v1.7

一. 安装Traefik

  安装方式有两种 Deployment 和 DaemonSet,官网比较有详细的解释,我就直接贴上了了:nginx

  • The scalability can be much better when using a Deployment, because you will have a Single-Pod-per-Node model when using a DaemonSet, whereas you may need less replicas based on your environment when using a Deployment.
  • DaemonSets automatically scale to new nodes, when the nodes join the cluster, whereas Deployment pods are only scheduled on new nodes if required.
  • DaemonSets ensure that only one replica of pods run on any single node. Deployments require affinity settings if you want to ensure that two pods don't end up on the same node.
  • DaemonSets can be run with the NET_BIND_SERVICE capability, which will allow it to bind to port 80/443/etc on each host. This will allow bypassing the kube-proxy, and reduce traffic hops. Note that this is against the Kubernetes Best Practices Guidelines, and raises the potential for scheduling/scaling issues. Despite potential issues, this remains the choice for most ingress controllers.
  • If you are unsure which to choose, start with the Daemonset.

  这里咱们听官方的,先用 DaemonSet 吧,主要考虑两个缘由:git

  1. 咱们目前是测试环境,用 DaemonSet 能够直接使用80和443端口,比较方便。
  2. 流量不用走 kube-proxy,效率更高一些

  1. 建立 RBAC 受权: 

kubectl apply -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-rbac.yaml

  2. 建立 DaemonSet

  • 这里能够把这个文件下载下来,咱们要略做修改github

wget https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-ds.yaml
  • 官方的 example 只启用了http,这里咱们要打开https;添加args参数
# 先添加一个https 的 containerPort
- name: https
  containerPort: 443
  hostPort: 443

# 添加args
- --defaultentrypoints=http,https
- --entrypoints=Name:https Address::443 TLS
- --entrypoints=Name:http Address::80

  • 建立
kubectl create -f traefik-ds.yaml
  • 这个yaml文件下面还有建立 service 声明,这里注意下
  • 若是你在测试环境使用,那这里基本不用变更什么
  • 若是你是在生产环境使用,而且集群是云服务商提供的,这里修改一下 service 的 type 为 LoadBalancer,这样集群会向云服务商申请一个外网的负载均衡器,经过负载均衡器将 Traefik 暴露到外网,这样咱们就能够建立 ingress 并把域名解析到这个 LB 的公网地址,由 Traefik 做为集群的一个入口网关
  • 这里我是在本地环境测试的,我尝试修改了一下 type 为 LoadBalancer,看下图你就会发现,由于我本地获取不到 LB 的资源, service 一直在 pending 状态

  • 若是在云环境成功下成功获取到了 LB 资源,集群会作什么呢?其实上面的图上能够看出来,集群内该服务会以 NodePort 方式暴露出来(3078八、3270五、32537), 集群请求到 LB 资源后会将 LB 的 80 负载均衡到全部 worker node 的 30788 端口,443 负载均衡到 32705,8080 负载均衡到 32537,这样就基本保证了 Traefik 的可用性

  3. 检查pod

kubectl get pod -n kube-system
  • 官方镜像是在 dockerhub 的,若是 pull 不下来就去阿里云找一个,将上面的yaml文件下载下来,替换一下image地址docker

  4. 建立 service和ingress 暴露 Traefik Web UI

  • 这里官方例子是使用 traefik-ui.minikube 这个域名,这里有须要的话能够自行修改域名,好比我这里改为了 k8s.mytest.com后端

wget https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/ui.yaml

 

  • 建立ingress,而后在咱们本地绑定一下/etc/hostsbash

kubectl create -f ui.yaml
# 添加 hosts (若是是生产环境,直接添加域名的DNS解析就行)
sudo echo "192.168.199.204  k8s.mytest.com" >> /etc/hosts
  • 访问本身绑定的域名,这里已经能看到ui了app

  5. 给Web Ui 添加一个 基础认证

  • 建立包含用户名密码的认证文件,建立完检查一下,文件内容应该是 用户名:密码(md5)负载均衡

htpasswd -c ./auth admin
  •  建立 secretless

kubectl create secret generic traefik-secret --from-file auth --namespace=kube-system
  • 修改 Traefik Web UI 的 ingress,这里继续修改前面下面的ui.yaml,添加:

annotations:
  kubernetes.io/ingress.class: traefik
  traefik.ingress.kubernetes.io/auth-type: basic
  traefik.ingress.kubernetes.io/auth-secret: traefik-secret

 

  • 更新 ingress

kubectl apply -f ui.yaml
  • 从新访问 Traefik Web UI,使用刚才建立的用户名密码登陆便可

  简单的安装和使用就写到这里,上面暴露 Traefik Web UI 其实就是一个暴露服务的例子,Traefik还有不少更高级的配置,好比使用 acme 自动申请和管理 let's encrypt 证书,你们能够从官网了解一下。

 

二.  导入并使用 SSL 证书

  • 首先生成证书,你们若是有域名的话能够申请一个免费的DV证书,阿里云上就能够免费申请
  • 使用证书文件建立 secret
kubectl create secret tls mytest-secret --cert=mytest.crt --key=mytest.key -n kube-system
  • 在 ingress 中使用证书,编辑 ingress,在spec.rules 下添加便可
tls:
  - secretName: mytest

 

参考:官方文档

相关文章
相关标签/搜索