dockerfile部署haproxy

1. 目录结构

[root@localhost haproxy]# tree 
.
├── Dockerfile
└── files
    ├── haproxy-2.5.0.tar.gz
    ├── install.sh
    └── variable.sh

2. 目录内容

[root@localhost haproxy]# cat Dockerfile 
FROM centos

LABEL MAINTAINER jjyy 1@2.com
ENV PATH /usr/local/haproxy/sbin:$PATH
COPY files /usr/src

RUN /usr/src/install.sh
CMD sh /usr/src/variable.sh $ip1 $ip2
EXPOSE 8189 80
WORKDIR /usr/local/haproxy


[root@localhost files]# cat install.sh 
rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F '"' 'NR==5{print $2}' /etc/os-release).repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum cleall
yum makecache

echo "alias ls='ls --color'" >> ~/.bashrc 
if test -f ~/.bashrc;then
	source ~/.bashrc
fi

yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
useradd -r -M -s /sbin/nologin haproxy
cd /usr/src
tar xf haproxy-2.5.0.tar.gz
cd haproxy-2.5.0
make clean 
make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
	TARGET=linux-glibc \
	USE_OPENSSL=1 \
	USE_ZLIB=1 \
	USE_PCRE=1 \
	USE_SYSTEMD=1 && \
make install PREFIX=/usr/local/haproxy && \
echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p

mkdir /etc/haproxy
cat > /usr/lib/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
 
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q   
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 \$MAINPID
 
[Install]
WantedBy=multi-user.target
EOF


yum -y remove gcc gcc-c++ make
rm -rf /var/cache/* /usr/src/*


[root@localhost files]# cat variable.sh 
#!/bin/bash
mkdir -p /etc/haproxy
cat >> /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
 #---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats
    stats realm Haproxy\ Statistics
    stats auth admin:admin
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
#下面两行改为rs1和rs2的IP地址
    server web01 ${ip1} check inter 2000 fall 5
    server web02 ${ip2} check inter 2000 fall 5
EOF

/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg

// 构建镜像
[root@localhost haproxy]# docker build -t haproxy:v2.0 .
[root@localhost haproxy]# docker run -itd -p 82:80 -e ip1=172.17.0.2 -e ip2=172.17.0.3 --name haproxy haproxy:v2.0 /bin/bash

// 启动容器
[root@localhost haproxy]# docker run -itd -p 82:80 -e ip1=172.17.0.2 -e ip2=172.17.0.3 --name haproxy haproxy:v2.0 /bin/bash

// 启动一个apache和nginx容器
[root@localhost haproxy]# docker ps 
CONTAINER ID   IMAGE          COMMAND        CREATED         STATUS         PORTS                                         NAMES
219b4fe67100   haproxy:v2.0   "/bin/bash"    4 minutes ago   Up 4 minutes   8189/tcp, 0.0.0.0:82->80/tcp, :::82->80/tcp   haproxy
6b1d9f2e95fd   nginx:v1.0     "/sbin/init"   13 hours ago    Up 3 hours     0.0.0.0:8080->80/tcp, :::8080->80/tcp         nginx
2eaf3c89658c   76086db2dd52   "/sbin/init"   2 days ago      Up 3 hours     0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp    web03

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐