• K8s-Traefik Ingress Controller


    Traefik Ingress Controller

    Traefik 是一个为了让部署微服务更加便捷而诞生的现代 HTTP 反向代理、负载均衡工具。traefik 本身设计的就能够实时跟 kubernetes api 交互,感知后端 service,pod 等的变化,自动更新配置并重载。

    traefik 是一个前端负载均衡器,对于微服务架构尤其是 kubernetes 等编排工具具有良好的支持;

    同 nginx 等相比,traefik 能够自动感知后端容器变化,从而实现自动服务发现。

    traefik 部署在 k8s 上分为 daemonset 和 deployment 两种方式,各有优缺点:

    daemonset 能确定有哪些 Node 在运行 traefik,所以可以确定的知道后端 ip,但是不能方便的伸缩。
    deployment 可以更方便的伸缩,但是不能确定有哪些 Node 在运行 traefik 所以不能确定的知道后端 ip。

    一般部署两种不同类型的 traefik:

    面向内部(internal)服务的 traefik,建议可以使用 deployment 的方式。
    面向外部(external)服务的 traefik,建议可以使用 daemonset 的方式。
    
    • 1
    • 2

    建议使用 traffic-type 标签

    traffic-type: external
    traffic-type: internal
    
    • 1
    • 2

    traefik 相应地使用 labelSelector

    traffic-type=internal
    traffic-type=external
    
    • 1
    • 2
    官方网址: https://docs.traefik.io/
    下载源码:git clone https://github.com/containous/traefik.git
    
    • 1
    • 2

    //部署 nginx-ingress-controller
    1、获取配置文件

    mkdir /opt/traefik
    cd /opt/traefik
    
    • 1
    • 2

    官方下载地址:

    wget  https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-rbac.yaml
    wget  https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-deployment.yaml
    wget  https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-ds.yaml
    wget  https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/ui.yaml
    
    • 1
    • 2
    • 3
    • 4

    国内的 gitee:

    wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-deployment.yaml
    wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-rbac.yaml
    wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-ds.yaml
    wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/ui.yaml
    
    • 1
    • 2
    • 3
    • 4

    2、依次执行
    //启用RBAC

    kubectl apply -f traefik-rbac.yaml
    
    • 1

    //部署 Traefik 到 Kubernetes 集群,为外部访问创建 NodePorts

    kubectl apply -f traefik-deployment.yaml
    
    • 1

    //部署 Traefik Web UI

    kubectl apply -f ui.yaml
    
    • 1

    //查看结果

    kubectl get svc -o wide -n kube-system | grep traefik
    traefik-ingress-service   NodePort    10.96.241.13   <none>        80:32383/TCP,8080:32133/TCP   103m   k8s-apptraefik-ingress-lb
    traefik-web-ui            ClusterIP   10.96.67.119   <none>        80/TCP                        101m   k8s-apptraefik-ingress-lb
    
    • 1
    • 2
    • 3

    //访问 Traefik UI,浏览器访问 http://Nodeip:NodePort/dashboard/

    http://192.168.80.14:32133/dashboard/
    
    • 1

    //Ingress HTTP 代理访问

    cd /opt/ingress-nodeport
    
    • 1

    #创建 deployment、Service、Ingress Yaml 资源

    vim ingress-nginx.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-app
    spec:
      replicas: 2
      selector:
        matchLabels:
          name: nginx
      template:
        metadata:
          labels:
            name: nginx
        spec:
          containers:
            - name: nginx
              image: nginx
              imagePullPolicy: IfNotPresent
              ports:
                - containerPort: 80
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-svc
    spec:
      ports:
        - port: 80
          targetPort: 80
          protocol: TCP
      selector:
        name: nginx
    ---
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: nginx-test
    spec:
      rules:
        - host: www.my.com
          http:
            paths:
            - path: /
              pathType: Prefix
              backend:
                service: 
                  name: nginx-svc
                  port:
                    number: 80
    
    
    kubectl apply -f ingress-nginx.yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    kubectl get svc,pods -o wide
    
    NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE   SELECTOR
    service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP   23d   <none>
    service/nginx-svc    ClusterIP   10.96.89.181   <none>        80/TCP    88m   name=nginx
    
    NAME                             READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
    pod/nginx-app-65d7b99f6b-nnw7q   1/1     Running   0          88m   10.244.2.4   node02   <none>           <none>
    pod/nginx-app-65d7b99f6b-x47l8   1/1     Running   0          88m   10.244.1.5   node01   <none>           <none>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    kubectl exec -it pod/nginx-app-65d7b99f6b-nnw7q bash
     # cd /usr/share/nginx/html/
     # echo 'this is web1' >> index.html 
    
    • 1
    • 2
    • 3
    kubectl exec -it pod/nginx-app-65d7b99f6b-x47l8 bash
     # cd /usr/share/nginx/html/
     # echo 'this is web2' >> index.html
    
    • 1
    • 2
    • 3

    #测试访问

    curl 10.96.89.181
    
    • 1
    kubectl get svc -o wide -n kube-system | grep traefik
    traefik-ingress-service   NodePort    10.96.241.13   <none>        80:32383/TCP,8080:32133/TCP   103m   k8s-apptraefik-ingress-lb
    traefik-web-ui            ClusterIP   10.96.67.119   <none>        80/TCP                        101m   k8s-apptraefik-ingress-lb
    
    • 1
    • 2
    • 3

    #本地 host 添加域名解析

    vim /etc/hosts
    192.168.80.10 master
    192.168.80.11 node01
    192.168.80.12 node02
    192.168.80.12 www.xiaoma.com www.my.com
    
    • 1
    • 2
    • 3
    • 4
    • 5

    #模拟外部访问

    curl http://www.my.com:32383
    
    • 1

    #再刷新查看 Traefik UI 界面,会生成刚创建的集群信息

    http://192.168.80.14:32133/dashboard/
    
    • 1
  • 相关阅读:
    FreeRTOS操作系统中,断言输出 Error:..\..\FreeRTOS\portable\RVDS\ARM_CM4F\port.c,766 原因
    js函数调用的方式有几种
    Codeforces Round #804 (Div. 2)【比赛记录】
    智慧应急解决方案-最新全套文件
    五、原型模式
    家常菜中的黄焖鸡
    前端培训丁鹿学堂:vue基础之v-model使用详情
    【正点原子FPGA连载】第二十四章 双路高速DA实验 摘自【正点原子】DFZU2EG/4EV MPSoC 之FPGA开发指南V1.0
    网攻西北工业大学的美国安局人员真实身份锁定!
    【Linux---04】虚拟机的三种网络模式:桥接模式、NAT模式、仅主机模式
  • 原文地址:https://blog.csdn.net/m0_56509725/article/details/134391041