haproxy

来源:互联网 发布:矩阵的阶数是什么 编辑:程序博客网 时间:2024/06/05 05:52

公司kubernates对外暴露服务,开了一个haproxy的service,所有请求都必须经过这个service。都由这个haproxy service控制。

服务创建:服务创建通过调用k8s的ingress接口进行数据插入,这里host必须使用域名,所以创建只能一种形式,ip是不行的。

ingress接口访问  /apis/extensions/v1beta1/ingresses

 "spec": {
        "rules": [
          {
            "host": "garyapp1.harmonycloud.cn",
            "http": {
              "paths": [
                {
                  "path": "/app1",
                  "backend": {
                    "serviceName": "test1",
                    "servicePort": 80
                  }
                }
              ]
            }
          }
        ]

根据用户需求,服务访问有2种,一种是通过域名访问,另一种是通过ip访问。所谓的ip访问也就是haproxy service所在的nodeip+nodeport,实际指向对外服务的pod的ip和port

目前域名访问实现是在haproxy容器中运行一个app,不停的监控ingress,获取到变化就插入到haproxy模板,重新生成haproxy.cfg,然后haproxy reload。

acl url_acl_cca path_beg /cca 
    acl host_nginx.harmonycloud.cn hdr_beg(host)  -i nginx.harmonycloud.cn
    
        use_backend nginx.harmonycloud.cn_cca if url_acl_cca

backend nginx.harmonycloud.cn_cca

    option  httplog
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

    balance roundrobin
    # TODO: Make the path used to access a service customizable.
    reqrep ^([^\ :]*)\ /cca[/]?(.*) \1\ /\2

    # create a stickiness table using client IP address as key
    # http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#stick-table
    stick-table type ip size 100k expire 30m
    stick on src
    server xx xx.xx.xx.xx:80 check port 80 inter 5

定义2个acl规则,根据path和域名访问pod。

定义一个path规则,也可以用ip访问,但是可能对外服务出现path重名的情况,为解决这个情况,我们在路径前加一个serviceName,然后出去的时候通过haproxy重命名再将serviceName去除,由于不同pod,path相同也不会出现问题。

#reqrep ^([^\ ]*)\ /luoy/(.*)     \1\ /\2
reqrep ^GET\ /luoy\ HTTP/1.1 GET\ /\ HTTP/1.1

前一种经过尝试不行访问,path依然带过去了。后一种将/luoy替换成/出去,可以。后台java代码插入ingress依然是/serviceName/path,获取ingress,插入规则acl是/serviceName,假定path是luoy重定向到index.html是  reqrep ^GET\ /luoy\ HTTP/1.1 GET\ /index.html\ HTTP/1.1


关于重定向不是很清晰的话可以访问http://www.4byte.cn/question/1071243/haproxy-and-reqrep-rewriting-configuration.html看一下

0 0
原创粉丝点击