服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

dirtysea 发表于 2020-8-19 11:39:12

kubernetes Traefik ingress配置详解

目录
[*]部署Traefik
[*]部署Locust的Controller和Service
[*]设置边缘节点


正文理解Ingress简单的说,ingress就是从kubernetes集群外访问集群的入口,将用户的URL请求转发到不同的service上。Ingress相当于nginx、apache等负载均衡方向代理服务器,其中还包括规则定义,即URL的路由信息,路由信息得的刷新由Ingress controller来提供。
理解Ingress ControllerIngress Controller 实质上可以理解为是个监视器,Ingress Controller 通过不断地跟 kubernetes API 打交道,实时的感知后端 service、pod 等变化,比如新增和减少 pod,service 增加与减少等;当得到这些变化信息后,Ingress Controller 再结合下文的 Ingress 生成配置,然后更新反向代理负载均衡器,并刷新其配置,达到服务发现的作用。
回到顶部
部署TraefikTraefik是一款开源的反向代理与负载均衡工具。它最大的优点是能够与常见的微服务系统直接整合,可以实现自动化动态配置。目前支持Docker, Swarm, Mesos/Marathon, Mesos, Kubernetes, Consul, Etcd, Zookeeper, BoltDB, Rest API等等后端模型。
配置文件ingress-rbac.yamlhttps://common.cnblogs.com/images/copycode.gif
apiVersion: v1kind: ServiceAccountmetadata:name: ingressnamespace: kube-system---kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1beta1metadata:name: ingresssubjects:- kind: ServiceAccount    name: ingress    namespace: kube-systemroleRef:kind: ClusterRolename: cluster-adminapiGroup: rbac.authorization.k8s.iohttps://common.cnblogs.com/images/copycode.gif


创建名为traefik-ingress的ingress,文件名ingress.yamlhttps://common.cnblogs.com/images/copycode.gif
apiVersion: extensions/v1beta1kind: Ingressmetadata:name: traefik-ingressnamespace: defaultspec:rules:- host: traefik.locust.io    http:      paths:      - path: /      backend:          serviceName: locust-master    #确保此service-name在 上面指定的namespace中          servicePort: 8089https://common.cnblogs.com/images/copycode.gif


这其中的backend中要配置default namespace中启动的service名字,如果你没有配置namespace名字,默认使用default namespace,如果你在其他namespace中创建服务想要暴露到kubernetes集群外部,可以创建新的ingress.yaml文件,同时在文件中指定该namespace,其他配置与上面的文件格式相同。。path就是URL地址后的路径,如traefik.frontend.io/path,service将会接受path这个路径,host最好使用service-name.filed1.filed2.domain-name这种类似主机名称的命名方式,方便区分服务。根据你自己环境中部署的service的名字和端口自行修改,有新service增加时,修改该文件后可以使用kubectl replace -f ingress.yaml来更新。
使用DaemonSet类型来部署Traefik,文件名 treafik.ymlhttps://common.cnblogs.com/images/copycode.gif
apiVersion: extensions/v1beta1kind: DaemonSetmetadata:name: traefik-ingress-lbnamespace: kube-systemlabels:    k8s-app: traefik-ingress-lbspec:template:    metadata:      labels:      k8s-app: traefik-ingress-lb      name: traefik-ingress-lb    spec:      terminationGracePeriodSeconds: 60      hostNetwork: true      restartPolicy: Always      serviceAccountName: ingress      containers:      - image: traefik      name: traefik-ingress-lb      resources:          limits:            cpu: 200m            memory: 30Mi          requests:            cpu: 100m            memory: 20Mi      ports:          #暴露端口      - name: http          containerPort: 80          hostPort: 80      - name: admin          containerPort: 8580          hostPort: 8580      args:      - --web      - --web.address=:8580      - --kubernetes      #nodeSelector:      #edgenode: "true"https://common.cnblogs.com/images/copycode.gif


查看pod信息并访问ui(需要配置)http://10.10.10.12:8580/dashboard/

如下所示https://img2018.cnblogs.com/blog/1045282/201810/1045282-20181019172722239-953772341.png默认情况下是没任何东西的
使用下面的yaml配置来创建Traefik的ingress,文件名 ui.ymlhttps://common.cnblogs.com/images/copycode.gif
apiVersion: v1kind: Servicemetadata:name: traefik-web-uinamespace: kube-systemspec:selector:    k8s-app: traefik-ingress-lbports:- name: web    port: 80    targetPort: 8580---apiVersion: extensions/v1beta1kind: Ingressmetadata:name: traefik-web-uinamespace: kube-systemspec:rules:- host: test.traefik-ingress.io    http:      paths:      - path: /      backend:          serviceName: traefik-web-ui          servicePort: webhttps://common.cnblogs.com/images/copycode.gif


创建kubectl create -f .

回到顶部
部署Locust的Controller和Servicelocust是啥。。自行百度$ git clone https://github.com/rootsongjc/distributed-load-testing-using-kubernetes.git$ cd distributed-load-testing-using-kubernetes/kubernetes-config

修改locust-master-cotnroller中spec.template.spec.containers.env字段中的value为你sample-webapp service的名字(要进行压力测试的服务的地址)。例:- name: TARGET_HOSTvalue: http://jenkins.kube-system.svc.cluster.local:8080

部署locust-master$ kubectl create -f locust-master-controller.yaml$ kubectl create -f locust-master-service.yaml

部署locust-worker$ kubectl create -f locust-worker-controller.yaml

#worker扩容
$ kubectl scale --replicas=20 replicationcontrollers locust-worker

配置Traefikingress.yaml中加入如下配置(上面已经配置):https://common.cnblogs.com/images/copycode.gif
- host: traefik.locust.io    http:      paths:      - path: /      backend:          serviceName: locust-master    #namespace要写对          servicePort: 8089https://common.cnblogs.com/images/copycode.gif


然后执行kubectl replace -f ingress.yaml即可更新traefik。
本地配置host,然后访问traefik.locust.iohttps://img2018.cnblogs.com/blog/1045282/201810/1045282-20181018171259476-2133109329.pngstart swarming就会开始测试
回到顶部
设置边缘节点
首先解释下什么叫边缘节点(Edge Node),所谓的边缘节点即集群内部用来向集群外暴露服务能力的节点,集群外部的服务通过该节点来调用集群内部的服务,边缘节点是集群内外交流的一个Endpoint。
边缘节点要考虑两个问题
[*]边缘节点的高可用,不能有单点故障,否则整个kubernetes集群将不可用
[*]对外的一致暴露端口,即只能有一个外网访问IP和端口

在Kubernetes中添加了service的同时,在DNS中增加一个记录,这条记录需要跟ingress中的host字段相同,IP地址即VIP的地址,本示例中是10.10.10.166,这样集群外部就可以通过service的DNS名称来访问服务了。
选择Kubernetes的两个个node作为边缘节点,并安装keepalived,下图展示了边缘节点的配置,同时展示了向Kubernetes中添加服务的过程。https://img2018.cnblogs.com/blog/1045282/201810/1045282-20181018171534669-160020506.png
配置说明需要对原先的traefik ingress进行改造,从以Deployment方式启动改成DeamonSet。还需要指定一个与node在同一网段的IP地址作为VIP,这里指定成10.10.10.166,配置keepalived前需要先保证这个IP没有被分配。。
[*]Traefik以DaemonSet的方式启动
[*]通过hostPort暴露端口
[*]当前VIP漂移到10.10.10.166上
[*]Traefik根据访问的host和path配置,将流量转发到相应的service上


[*]安装keepalived并配置
yum install keepalived ipvsadm

配置https://common.cnblogs.com/images/copycode.gif
! Configuration File for keepalivedglobal_defs {   notification_email {   root@localhost   }   notification_email_from kaadmin@localhost   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 51    priority 100    advert_int 1    authentication {      auth_type PASS      auth_pass 1111    }    virtual_ipaddress {      10.10.10.166    }}virtual_server 10.10.10.166 80{    delay_loop 6    lb_algo loadbalance    lb_kind DR    nat_mask 255.255.255.0    persistence_timeout 0    protocol TCP    real_server 10.10.10.12 80{      weight 1      TCP_CHECK {      connect_timeout 3      }    }    real_server 10.10.10.162 80{      weight 1      TCP_CHECK {      connect_timeout 3      }    }}https://common.cnblogs.com/images/copycode.gif


Realserver的IP和端口即traefik供外网访问的IP和端口。将以上配置分别拷贝到另外一台node的/etc/keepalived目录下。使用转发效率最高的lb_kind DR直接路由方式转发,使用TCP_CHECK来检测real_server的health。
设置keepalived为开机自启动:chkconfig keepalived on

启动keepalivedsystemctl start keepalived

node都启动了keepalived后,观察eth0的IP,会在两台node的某一台上发现一个VIP是10.10.10.166。

[*]从新配置traefik ingress
由于上面配置了keepalived,此时,本地hosts 文件的解析地址可以写成10.10.10.166,使用基于域名的traefik ingress以上面的locust为例,配置traefik ingress如下https://common.cnblogs.com/images/copycode.gif
apiVersion: extensions/v1beta1kind: Ingressmetadata:name: traefik-ingressnamespace: defaultannotations:                  #必须指定    kubernetes.io/ingress.class: traefik    traefik.frontend.rule.type: PathPrefixStripspec:rules:#- host: traefik.locust.io- host: test.traefik-ingress.io    http:      paths:      - path: /locust      #此处配置了url,上面必须指定annotations指定的配置      backend:          serviceName: locust-master          servicePort: 8089https://common.cnblogs.com/images/copycode.gif

创建好以后,访问 test.traefik-ingress.io/locust
其他服务同理,host可写test.traefik-ingress.io,只配置path用于区分不同的服务,访问的时候访问host/path即可
Traefik在Kubernetes中Annotate的配置https://common.cnblogs.com/images/copycode.gif
通用配置kubernetes.io/ingress.class: traefik       #Ingress声明,这里声明了ingress后端采用traefik实现,而不是nginx的controlleringress.kubernetes.io/whitelist-source-range: "1.2.3.0/24, fe80::/16"     #配置访问白名单,支持ipv4和ipv6ingress.kubernetes.io/auth-type: basic        #http认证模式,此处为basic模式ingress.kubernetes.io/auth-secret: mysecret      #basic认证的对应的username和password,这里对应的traefik所在kubernetes命名空间里的secrets
前端配置traefik.frontend.rule.type: PathPrefixStriptraefik.frontend.priority: "3"      #配置前端的权重,值越高则优先匹配traefik.frontend.passHostHeader: "false"      #关闭传入Heardertraefik.protocol=https        #使用https协议traefik.frontend.entryPoints=http,https      #同时支持http和https后端配置traefik.backend.loadbalancer.method=drr      #后端Service的负载均衡策略,目前traefik支持的策略包括:wrr(加权轮训调度算法)和drr(动态加权循环调度算法)traefik.backend.loadbalancer.stickiness=true       #是否开启负载均衡器的session亲和性traefik.backend.loadbalancer.stickiness.cookieName=NAME    #手动配置后端session亲和性的cookie名称traefik.backend.loadbalancer.sticky=true弃用健康检查traefik.backend.healthcheck.path=/healthtraefik的监控检查路径traefik.backend.healthcheck.interval=5s健康检查的时间间隔traefik.backend.circuitbreaker: "NetworkErrorRatio() > 0.5"监测某台节点上的服务错误率达到50%时,自动下线该节点。traefik.backend.circuitbreaker: "LatencyAtQuantileMS(50.0) > 50"监测某台节点上服务的延时大于50ms时,自动下线该节点。traefik.backend.circuitbreaker: "ResponseCodeRatio(500, 600, 0, 600) > 0.5"监测某台节点上服务返回状态码为在区间占比超过50%时,自动下线该节点。https://common.cnblogs.com/images/copycode.gif



来源: https://www.cnblogs.com/lvcisco/p/10697565.html


页: [1]
查看完整版本: kubernetes Traefik ingress配置详解