【Kubernetes】————————Traefik Ingress Controller

目录

一、Traefik介绍

二、traefik 部署在 k8s 上的方式

2.1daemonset

2.2deployment

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

2.4官网地址

三、部署 nginx-ingress-controller

3.1获取配置文件

3.2依次执行

3.3访问 Traefik UI

四、Ingress HTTP代理访问

4.1创建 deployment、Service、Ingress Yaml 资源

4.2测试访问

4.3本地 host 添加域名解析

4.4模拟外部访问

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


一、Traefik介绍

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

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

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

二、traefik 部署在 k8s 上的方式

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

2.1daemonset

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

2.2deployment

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

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

  • 面向内部(internal)服务的 traefik,建议可以使用 deployment 的方式。
  • 面向外部(external)服务的 traefik,建议可以使用 daemonset 的方式。
建议使用 traffic-type 标签
traffic-type: external
traffic-type: internal

traefik 相应地使用 labelSelector
traffic-type=internal
traffic-type=external

2.4官网地址

官方网址: https://docs.traefik.io/
下载源码:git clone https://github.com/containous/traefik.git

三、部署 nginx-ingress-controller

3.1获取配置文件

官方下载地址:
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

国内的 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
mkdir /opt/traefik
cd /opt/traefik

3.2依次执行

#启用RBAC
kubectl apply -f traefik-rbac.yaml
 
#部署 Traefik 到 Kubernetes 集群,为外部访问创建 NodePorts
kubectl apply -f traefik-deployment.yaml
 
#部署 Traefik Web UI
kubectl apply -f ui.yaml
 
#查看结果
kubectl get svc -o wide -n kube-system | grep traefik

3.3访问 Traefik UI

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

四、Ingress HTTP代理访问

cd /opt/ingress-nodeport

4.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.benet.com
      http:
        paths:
        - path: /
          pathType: Prefix
          backend:
            service: 
              name: nginx-svc
              port:
                number: 80
kubectl apply -f ingress-nginx.yaml
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>


kubectl exec -it pod/nginx-app-65d7b99f6b-nnw7q bash   #进入容器
 # cd /usr/share/nginx/html/
 # echo 'this is web1' >> index.html   #写入页面

kubectl exec -it pod/nginx-app-65d7b99f6b-x47l8 bash  #进入容器
 # cd /usr/share/nginx/html/
 # echo 'this is web2' >> index.html   #写入页面

4.2测试访问

curl 10.96.89.181

查看

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

4.3本地 host 添加域名解析

vim /etc/hosts
192.168.246.10 master
192.168.246.11 node01
192.168.246.12 node02
192.168.246.12 www.kgc.com www.benet.com

4.4模拟外部访问

curl http://www.benet.com:32383

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

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

​http://192.168.246.11:32133/dashboard/

悲莫悲兮生别离,乐莫乐兮新相知。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值