在k8s-60.host.com上

  1. [root@k8s-60 k8s-yaml]# docker pull traefik:v1.7.2-alpine
  2. [root@k8s-60 k8s-yaml]# docker images|grep traefik
  3. [root@k8s-60 k8s-yaml]# docker tag add5fac61ae5 harbor.od.com/public/traefik:v1.7.2
  4. [root@k8s-60 k8s-yaml]# docker push harbor.od.com/public/traefik:v1.7.2

2.准备资源配置清单

在k8s-60.host.com上

  1. [root@k8s-60 k8s-yaml]# mkdir traefik
  2. [root@k8s-60 k8s-yaml]# cd traefik/

rbac.yaml

  1. [root@k8s-60 traefik]# vi rbac.yaml
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: traefik-ingress-controller
  6. namespace: kube-system
  7. ---
  8. apiVersion: rbac.authorization.k8s.io/v1beta1
  9. kind: ClusterRole
  10. metadata:
  11. name: traefik-ingress-controller
  12. rules:
  13. - apiGroups:
  14. - ""
  15. resources:
  16. - services
  17. - endpoints
  18. - secrets
  19. verbs:
  20. - get
  21. - list
  22. - watch
  23. - apiGroups:
  24. - extensions
  25. resources:
  26. - ingresses
  27. verbs:
  28. - get
  29. - list
  30. - watch
  31. ---
  32. kind: ClusterRoleBinding
  33. apiVersion: rbac.authorization.k8s.io/v1beta1
  34. metadata:
  35. name: traefik-ingress-controller
  36. roleRef:
  37. apiGroup: rbac.authorization.k8s.io
  38. kind: ClusterRole
  39. name: traefik-ingress-controller
  40. subjects:
  41. - kind: ServiceAccount
  42. name: traefik-ingress-controller
  43. namespace: kube-system

ds.yaml

  1. [root@k8s-60 traefik]# vi ds.yaml
  2. apiVersion: extensions/v1beta1
  3. kind: DaemonSet
  4. metadata:
  5. name: traefik-ingress
  6. namespace: kube-system
  7. labels:
  8. k8s-app: traefik-ingress
  9. spec:
  10. template:
  11. metadata:
  12. labels:
  13. k8s-app: traefik-ingress
  14. name: traefik-ingress
  15. spec:
  16. serviceAccountName: traefik-ingress-controller
  17. terminationGracePeriodSeconds: 60
  18. containers:
  19. - image: harbor.od.com/public/traefik:v1.7.2
  20. name: traefik-ingress
  21. ports:
  22. - name: controller
  23. containerPort: 80
  24. hostPort: 81 # 把容器内的80端口映射到宿主机的81端口
  25. - name: admin-web
  26. containerPort: 8080
  27. securityContext:
  28. capabilities:
  29. drop:
  30. - ALL
  31. add:
  32. - NET_BIND_SERVICE
  33. args:
  34. - --api
  35. - --kubernetes
  36. - --logLevel=INFO
  37. - --insecureskipverify=true
  38. - --kubernetes.endpoint=https://192.168.10.10:7443 # VIP地址
  39. - --accesslog
  40. - --accesslog.filepath=/var/log/traefik_access.log
  41. - --traefiklog
  42. - --traefiklog.filepath=/var/log/traefik.log
  43. - --metrics.prometheus

svc.yaml

  1. [root@k8s-60 traefik]# vi svc.yaml
  2. kind: Service
  3. apiVersion: v1
  4. metadata:
  5. name: traefik-ingress-service
  6. namespace: kube-system
  7. spec:
  8. selector:
  9. k8s-app: traefik-ingress
  10. ports:
  11. - protocol: TCP
  12. port: 80
  13. name: controller
  14. - protocol: TCP
  15. port: 8080
  16. name: admin-web

ingress.yaml

  1. [root@k8s-60 traefik]# vi ingress.yaml
  2. apiVersion: extensions/v1beta1
  3. kind: Ingress
  4. metadata:
  5. name: traefik-web-ui
  6. namespace: kube-system
  7. annotations:
  8. kubernetes.io/ingress.class: traefik
  9. spec:
  10. rules:
  11. - host: traefik.od.com
  12. http:
  13. paths:
  14. - path: /
  15. backend:
  16. serviceName: traefik-ingress-service
  17. servicePort: 8080

3.应用资源配置清单

  1. [root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/traefik/rbac.yaml
  2. [root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/traefik/ds.yaml
  3. [root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/traefik/svc.yaml
  4. [root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/traefik/ingress.yaml

4.检查创建资源

  1. [root@k8s-40 ~]# kubectl get pods -n kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. coredns-6b6c4f9648-8fzsf 1/1 Running 0 8m35s
  4. traefik-ingress-96th7 1/1 Running 0 21s
  5. traefik-ingress-b6rkv 1/1 Running 0 21s
  6. [root@k8s-40 ~]# netstat -luntp | grep 81
  7. tcp6 0 0 :::81 :::* LISTEN 61367/docker-proxy

5.解析域名

  1. [root@k8s-20 ~]# vi /var/named/od.com.zone
  2. $ORIGIN od.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.od.com. dnsadmin.od.com. (
  5. 2020042601 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.od.com.
  12. $TTL 60 ; 1 minute
  13. dns A 192.168.10.20
  14. harbor A 192.168.10.60
  15. k8s-yaml A 192.168.10.60
  16. traefik A 192.168.10.10
  17. [root@k8s-20 ~]# systemctl restart named

6.配置7层反向代理

  1. 泛域名的流量调度
  2. k8s-20k8s-30两台都要做
  3. [root@k8s-20 ~]# vi /etc/nginx/conf.d/od.com.conf
  4. upstream default_backend_traefik {
  5. server 192.168.10.40:81 max_fails=3 fail_timeout=10s;
  6. server 192.168.10.50:81 max_fails=3 fail_timeout=10s;
  7. }
  8. server {
  9. server_name *.od.com;
  10. location / {
  11. proxy_pass http://default_backend_traefik;
  12. proxy_set_header Host $http_host;
  13. proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
  14. }
  15. }
  16. [root@k8s-20 ~]# nginx -t
  17. [root@k8s-20 ~]# nginx -s reload
  18. # 把流量全部丢给ingress处理

7.浏览器访问

  1. http://traefik.od.com/