#(1)traefix组件说明

Traefik是一个用Golang开发的轻量级的Http反向代理和负载均衡器。由于可以自动配置和刷新backend节点,目前可以被绝大部分容器平台支持,例如Kubernetes,Swarm,Rancher等。由于traefik会实时与Kubernetes API交互,所以对于Service的节点变化,traefik的反应会更加迅速。总体来说traefik可以在Kubernetes中完美的运行.

#(2)权限资源清单文件

#cat rbac.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
    name: traefik-ingress-controller
rules:
    - apiGroups:
            - ""
        resources:
            - services
            - endpoints
            - secrets
        verbs:
            - get
            - list
            - watch
    - apiGroups:
            - extensions
        resources:
            - ingresses
        verbs:
            - get
            - list
            - watch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
    name: traefik-ingress-controller
roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: traefik-ingress-controller
subjects:
- kind: ServiceAccount
    name: traefik-ingress-controller
    namespace: kube-system

#(3)deployment和service清单文件

# cat traefik.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:
    name: traefik-ingress-controller
    namespace: kube-system
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
    name: traefik-ingress-controller
    namespace: kube-system
    labels:
        k8s-app: traefik-ingress-lb
spec:
    replicas: 1
    selector:
        matchLabels:
            k8s-app: traefik-ingress-lb
    template:
        metadata:
            labels:
                k8s-app: traefik-ingress-lb
                name: traefik-ingress-lb
        spec:
            serviceAccountName: traefik-ingress-controller
            terminationGracePeriodSeconds: 60
            containers:
            - image: traefik
                name: traefik-ingress-lb
                args:
                - --web
                - --kubernetes
                - --web.metrics
                - --web.metrics.prometheus
                - --accesslog
                - --accesslog.filepath=/var/log/traefik_access.log
                - --traefiklog
                - --traefiklog.filepath=/var/log/traefik.log
                - --traefiklogsfile=traefik.log
                - --api
                - --loglevel=INFO
                - --metrics.prometheus

---
kind: Service
apiVersion: v1
metadata:
    name: traefik-ingress-service
    namespace: kube-system
spec:
    selector:
        k8s-app: traefik-ingress-lb
    ports:
        - protocol: TCP
            port: 80
            name: http
            nodePort: 30080
        - protocol: TCP
            port: 8080
            name: admin
            nodePort: 38080
        - protocol: TCP
            port: 443
            name: https
            nodePort: 30443
    type: NodePort

#(4)ui资源清单文件

# cat ui.yaml 
apiVersion: v1
kind: Service
metadata:
    name: traefik-web-ui
    namespace: kube-system
spec:
    selector:
        k8s-app: traefik-ingress-lb
    ports:
    - port: 80
        targetPort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
    name: traefik-web-ui
    namespace: kube-system
    annotations:
        kubernetes.io/ingress.class: traefik
spec:
    rules:
    - host: traefik-ui.minikube
        http:
            paths:
            - backend:
                    serviceName: traefik-web-ui
                    servicePort: 80

#(5)创建资源

kubectl apply -f .

#(5)进入ui

http://192.168.19.130:38080/dashboard/

(九)部署traefix组件_第1张图片

#(6)创建deployment和service资源

kubectl run nginx-test --image=nginx:1.11 --replicas=3
kubectl get pods -o wide
kubectl expose deployment nginx-test --port=88 --target-port=80 --type=NodePort
kubectl get svc 

#(7)创建ingress资源, 发布服务

#cat nginx.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
    name: nginx-basic
    namespace: default
    annotations:
        kubernetes.io/ingress.class: traefik
spec:
    rules:
    - host: www.nginx.com
        http:
            paths:
            - backend:
                    serviceName: nginx-test
                    servicePort: 88

#kubectl apply -f nginx.yaml 

#(8)验证
在traefix的ui界面上, 能看到定义的ingress资源已经关联了nginx-test这个service后端服务;
(九)部署traefix组件_第2张图片

本地hosts文件建立ip和url的映射关系
(九)部署traefix组件

经过测试能通过url进行访问
(九)部署traefix组件_第3张图片