认识
该笔记将记录:在 Kubernetes 中,与 Traefik Ingress Controller 相关的安装、使用、配置、管理,及常见问题解决方案。
构建
参考 Traefik/Kubernetes Ingress Controller 文档,足以完成 Traefik Ingress Controller 的部署
系统环境
CentOS Linux release 7.5.1804 (Core)
Kubernetes v1.14.0, Docker version 19.03.2, build 6a30dfc, Helm v3.0.0-beta.3
在云环境中,部署 Traefik Ingress Controller 组件
这里的云环境是指阿里云,其他的云环境也是类似的。
# kubectl apply -f ./01-traefik-rbac-v1.7.yaml
# kubectl apply -f ./02-traefik-deployment-v1.7.yaml
使用云环境的优势是:当 Service 为 LoadBalancer 时,会自动创建负载均衡,并在公网监听 80 443 端口。而使用自建集群,则需要进行特殊处理。
在自建集群中,部署 Traefik Ingress Controller 组件
在集群中,如果未部署 LB 组件(MetaLB、OpenELB),则需要进行特殊处理:
1)修改 hostNetwork: true,使 Pod 监听节点的 80 443 端口;
2)通过使用 nodeSelector 选择节点,使 Pod 固定调度在某个节点上;
3)此时,不再需要部署 Service 来映射端口;
如果有其他类型的负载均衡,则能够通过 Service NodePort 来监听端口,然后再通过负载均衡转发到该端口上。
在集群中,如果部署 MetaLB、OpenLB 等组件,则能够向云环境那样,定义 Service 为 LoadBalacher 类型;
访问验证
定义 Deploymnent、Service、Ingress 资源:
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: web
namespace: default
labels:
run: web
spec:
replicas: 1
selector:
matchLabels:
run: web
template:
metadata:
creationTimestamp: null
labels:
run: web
spec:
containers:
- name: web
image: 'httpd:alpine'
ports:
- containerPort: 80
protocol: TCP
imagePullPolicy: Always
restartPolicy: Always
dnsPolicy: ClusterFirst
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
-----
kind: Service
apiVersion: v1
metadata:
name: web
namespace: default
labels:
run: web
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
run: web
clusterIP: 10.108.28.108
type: NodePort
externalTrafficPolicy: Cluster
-----
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: web
namespace: default
annotations:
kubernetes.io/ingress.class: traefik
spec:
rules:
- host: hello-world.info
http:
paths:
- path: /
backend:
serviceName: web
servicePort: 80
如果有必要则先绑定 HOST 条目,然后在浏览器中访问 http://hello-world.info 地址,如果返回 It works! 则表示正常。
参考
Traefik/Kubernetes Ingress Controller
Kubernetes Traefik Installation (helm)
GitHub/helm/charts/stable/traefik
Bare-metal considerations – NGINX Ingress Controller
初试 Kubernetes 集群中使用 Traefik 反向代理
Traefik 一个反向代理的新工具