├── Dockerfile ├── README.md ├── docker-entrypoint.sh ├── haproxy.cfg └── start-haproxy.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM haproxy:1.8.13 2 | COPY docker-entrypoint.sh / 3 | COPY haproxy.cfg /usr/local/etc/haproxy/ 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HAProxy for Kubernetes cluster setup 2 | HAProxy for Kubernetes cluster setup 3 | 4 | 使用方法: 5 | 6 | 编辑start-haproxy.sh文件,修改Kubernetes Master节点IP地址为实际Kubernetes集群所使用的值(Master Port如果按标准Breeze安装方法,则6443不用修改): 7 | 8 | 例如: 9 | ``` 10 | #!/bin/bash 11 | MasterIP1=192.168.9.11 12 | MasterIP2=192.168.9.12 13 | MasterIP3=192.168.9.13 14 | MasterPort=6443 15 | 16 | docker run -d --restart=always --name HAProxy-K8S -p 6444:6444 -e MasterIP1=$MasterIP1 -e MasterIP2=$MasterIP2 -e MasterIP3=$MasterIP3 -e MasterPort=$MasterPort wise2c/haproxy-k8s 17 | ``` 18 | 19 | 在三个节点运行start-haproxy.sh脚本 20 | ``` 21 | bash start-haproxy.sh 22 | ``` 23 | -------------------------------------------------------------------------------- /docker-entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | /bin/sed -i "s/{{MasterIP1}}/${MasterIP1}/g" /usr/local/etc/haproxy/haproxy.cfg 5 | /bin/sed -i "s/{{MasterIP2}}/${MasterIP2}/g" /usr/local/etc/haproxy/haproxy.cfg 6 | /bin/sed -i "s/{{MasterIP3}}/${MasterIP3}/g" /usr/local/etc/haproxy/haproxy.cfg 7 | /bin/sed -i "s/{{MasterPort}}/${MasterPort}/g" /usr/local/etc/haproxy/haproxy.cfg 8 | 9 | 10 | # first arg is `-f` or `--some-option` 11 | if [ "${1#-}" != "$1" ]; then 12 | set -- haproxy "$@" 13 | fi 14 | 15 | if [ "$1" = 'haproxy' ]; then 16 | shift # "haproxy" 17 | # if the user wants "haproxy", let's add a couple useful flags 18 | # -W -- "master-worker mode" (similar to the old "haproxy-systemd-wrapper"; allows for reload via "SIGUSR2") 19 | # -db -- disables background mode 20 | set -- haproxy -W -db "$@" 21 | fi 22 | 23 | exec "$@" 24 | -------------------------------------------------------------------------------- /haproxy.cfg: -------------------------------------------------------------------------------- 1 | global 2 | log 127.0.0.1 local0 3 | log 127.0.0.1 local1 notice 4 | maxconn 4096 5 | #chroot /usr/share/haproxy 6 | #user haproxy 7 | #group haproxy 8 | daemon 9 | 10 | defaults 11 | log global 12 | mode http 13 | option httplog 14 | option dontlognull 15 | retries 3 16 | option redispatch 17 | timeout connect 5000 18 | timeout client 50000 19 | timeout server 50000 20 | 21 | frontend stats-front 22 | bind *:8081 23 | mode http 24 | default_backend stats-back 25 | 26 | frontend fe_k8s_6444 27 | bind *:6444 28 | mode tcp 29 | timeout client 1h 30 | log global 31 | option tcplog 32 | default_backend be_k8s_6443 33 | acl is_websocket hdr(Upgrade) -i WebSocket 34 | acl is_websocket hdr_beg(Host) -i ws 35 | 36 | backend stats-back 37 | mode http 38 | balance roundrobin 39 | stats uri /haproxy/stats 40 | stats auth pxcstats:secret 41 | 42 | backend be_k8s_6443 43 | mode tcp 44 | timeout queue 1h 45 | timeout server 1h 46 | timeout connect 1h 47 | log global 48 | balance roundrobin 49 | server k8smaster01 {{MasterIP1}}:{{MasterPort}} 50 | server k8smaster02 {{MasterIP2}}:{{MasterPort}} 51 | server k8smaster03 {{MasterIP3}}:{{MasterPort}} 52 | -------------------------------------------------------------------------------- /start-haproxy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | MasterIP1=192.168.9.11 3 | MasterIP2=192.168.9.12 4 | MasterIP3=192.168.9.13 5 | MasterPort=6443 6 | 7 | docker run -d --restart=always --name HAProxy-K8S -p 6444:6444 -e MasterIP1=$MasterIP1 -e MasterIP2=$MasterIP2 -e MasterIP3=$MasterIP3 -e MasterPort=$MasterPort wise2c/haproxy-k8s 8 | --------------------------------------------------------------------------------