在k8s-60.host.com上
[root@k8s-60 k8s-yaml]# docker pull traefik:v1.7.2-alpine[root@k8s-60 k8s-yaml]# docker images|grep traefik[root@k8s-60 k8s-yaml]# docker tag add5fac61ae5 harbor.od.com/public/traefik:v1.7.2[root@k8s-60 k8s-yaml]# docker push harbor.od.com/public/traefik:v1.7.2
2.准备资源配置清单
在k8s-60.host.com上
[root@k8s-60 k8s-yaml]# mkdir traefik[root@k8s-60 k8s-yaml]# cd traefik/
rbac.yaml
[root@k8s-60 traefik]# vi rbac.yamlapiVersion: v1kind: ServiceAccountmetadata:name: traefik-ingress-controllernamespace: kube-system---apiVersion: rbac.authorization.k8s.io/v1beta1kind: ClusterRolemetadata:name: traefik-ingress-controllerrules:- apiGroups:- ""resources:- services- endpoints- secretsverbs:- get- list- watch- apiGroups:- extensionsresources:- ingressesverbs:- get- list- watch---kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1beta1metadata:name: traefik-ingress-controllerroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: traefik-ingress-controllersubjects:- kind: ServiceAccountname: traefik-ingress-controllernamespace: kube-system
ds.yaml
[root@k8s-60 traefik]# vi ds.yamlapiVersion: extensions/v1beta1kind: DaemonSetmetadata:name: traefik-ingressnamespace: kube-systemlabels:k8s-app: traefik-ingressspec:template:metadata:labels:k8s-app: traefik-ingressname: traefik-ingressspec:serviceAccountName: traefik-ingress-controllerterminationGracePeriodSeconds: 60containers:- image: harbor.od.com/public/traefik:v1.7.2name: traefik-ingressports:- name: controllercontainerPort: 80hostPort: 81 # 把容器内的80端口映射到宿主机的81端口- name: admin-webcontainerPort: 8080securityContext:capabilities:drop:- ALLadd:- NET_BIND_SERVICEargs:- --api- --kubernetes- --logLevel=INFO- --insecureskipverify=true- --kubernetes.endpoint=https://192.168.10.10:7443 # VIP地址- --accesslog- --accesslog.filepath=/var/log/traefik_access.log- --traefiklog- --traefiklog.filepath=/var/log/traefik.log- --metrics.prometheus
svc.yaml
[root@k8s-60 traefik]# vi svc.yamlkind: ServiceapiVersion: v1metadata:name: traefik-ingress-servicenamespace: kube-systemspec:selector:k8s-app: traefik-ingressports:- protocol: TCPport: 80name: controller- protocol: TCPport: 8080name: admin-web
ingress.yaml
[root@k8s-60 traefik]# vi ingress.yamlapiVersion: extensions/v1beta1kind: Ingressmetadata:name: traefik-web-uinamespace: kube-systemannotations:kubernetes.io/ingress.class: traefikspec:rules:- host: traefik.od.comhttp:paths:- path: /backend:serviceName: traefik-ingress-serviceservicePort: 8080
3.应用资源配置清单
[root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/traefik/rbac.yaml[root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/traefik/ds.yaml[root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/traefik/svc.yaml[root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/traefik/ingress.yaml
4.检查创建资源
[root@k8s-40 ~]# kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcoredns-6b6c4f9648-8fzsf 1/1 Running 0 8m35straefik-ingress-96th7 1/1 Running 0 21straefik-ingress-b6rkv 1/1 Running 0 21s[root@k8s-40 ~]# netstat -luntp | grep 81tcp6 0 0 :::81 :::* LISTEN 61367/docker-proxy
5.解析域名
[root@k8s-20 ~]# vi /var/named/od.com.zone$ORIGIN od.com.$TTL 600 ; 10 minutes@ IN SOA dns.od.com. dnsadmin.od.com. (2020042601 ; serial10800 ; refresh (3 hours)900 ; retry (15 minutes)604800 ; expire (1 week)86400 ; minimum (1 day))NS dns.od.com.$TTL 60 ; 1 minutedns A 192.168.10.20harbor A 192.168.10.60k8s-yaml A 192.168.10.60traefik A 192.168.10.10[root@k8s-20 ~]# systemctl restart named
6.配置7层反向代理
泛域名的流量调度k8s-20和k8s-30两台都要做[root@k8s-20 ~]# vi /etc/nginx/conf.d/od.com.confupstream default_backend_traefik {server 192.168.10.40:81 max_fails=3 fail_timeout=10s;server 192.168.10.50:81 max_fails=3 fail_timeout=10s;}server {server_name *.od.com;location / {proxy_pass http://default_backend_traefik;proxy_set_header Host $http_host;proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;}}[root@k8s-20 ~]# nginx -t[root@k8s-20 ~]# nginx -s reload# 把流量全部丢给ingress处理
7.浏览器访问
http://traefik.od.com/
