Confd+Etcd+endpoint+haproxy

一:规则说明:
通过confd关联etcd构建haproxy模板,并重启haproxy过程,在默认的namespace上。
二:安装confd:
wget https://github.com/kelseyhightower/confd/releases/download/v0.11.0/confd-0.11.0-linux-amd64
[root@201 data]# mv confd-0.11.0-linux-amd64 /usr/local/bin/confd
[root@201 data]# chmod +x /usr/local/bin/confd
[root@201 data]# confd -version
confd 0.11.0
confd要与haproxy安装在同一台主机上,以便能生成给Haproxy用的配置。创建confd的默认配置存放路径,confd会根据该文件找到要修改的haproxy文件路径
mkdirp/etc/confd/templates vim /etc/confd/conf.d/haproxy.toml
[template]
src = “haproxy.cfg.tmpl”
dest = “/etc/haproxy/haproxy.cfg”
keys = [
#”/services/zookeeper/”,
#”/services/web”
#以此key值为默认的根路径,可以设置多个路径
“/registry/services/endpoints/default/”
]
reload_cmd = “/etc/init.d/haproxy reload” #可以填写你自己的重启haproxy的命令
三:示例:
1.创建两个service:

 {
    "kind": "Service",
    "apiVersion": "v1",
    "metadata": {
        "name": "tomcat1",
        "labels": {
            "name": "tomcat1",
            "haproxyport":"15246",
            "type":"APP"
        }
    },
    "spec": {
        "ports": [
            {
                "name": "tomcat1",
                "protocol": "TCP",
                "port": 8080,
                "targetPort": 8080,

            }
        ],
        "selector": {
            "name": "tomcat1"
        },
        "clusterIP": "172.17.78.85",
        "type": "ClusterIP"
     },
    "status": {
        "loadBalancer": {}
    }
}

b.

{
    "kind": "Service",
    "apiVersion": "v1",
    "metadata": {
        "name": "ipaas1",
        "labels": {
            "name": "ipaas1",
            "haproxyport":"15246",
            "type":"IPAAS"
        }
    },
    "spec": {
        "ports": [
            {
                "name": "ipaas1",
                "protocol": "TCP",
                "port": 8081,
                "targetPort": 9090
            }
        ],
        "selector": {
            "name": "ipaas1"
        }
     },
    "status": {
        "loadBalancer": {}
    }
}

2.创建RC(省略)这里必须要有Service所关联的label相对应运行的pod,才能生成endpoints对应的关系数据。
创建haproxy.cfg的模板文件,以便confd能根据模板生成配置:
vim /etc/confd/templates/haproxy.cfg.tmpl
global
log 127.0.0.1 local3
maxconn 5000
uid 99
gid 99
daemon

defaults
log 127.0.0.1 local3
mode http
option dontlognull
retries 3
option redispatch
maxconn 2000
timeout connect 5000
timeout client 50000
timeout server 50000
Confd+Etcd+endpoint+haproxy_第1张图片
说明:上面定义的规则首先通过range循环所有在default命令空间下的Service,获取每个Service在label中定义的Type值来判断是生成ipaas的还是APP的haproxy配置,因为这里用了两套haproxy基于Ipaas,Iaas,然后在循环获取这个Service下所关联的pod并生成对应的配置
启动confd:
confd -interval 5 -node ‘etcd:2379’ -confdir /etc/confd > /var/log/confd.log &
–注 interval 为设置多个秒检查一次
生成的配置(只截取动态生成的数据):
Confd+Etcd+endpoint+haproxy_第2张图片
Confd+Etcd+endpoint+haproxy_第3张图片

你可能感兴趣的:(kubernetes)