//结构
[root@localhost haproxy]# tree
.
├── Dockerfile
└── files
├── haproxy-2.4.0.tar.gz
├── haproxy.cfg
├── haproxy.sh
└── install.sh
//dockerfile
[root@localhost haproxy]# cat Dockerfile
FROM centos
LABEL MAINTAINER aaa xaw@qq.com
ENV version 2.4.0
COPY files/haproxy-${version}.tar.gz /usr/src
COPY files/haproxy.cfg /etc/haproxy/haproxy.cfg
COPY files/install.sh /scripts/
COPY files/haproxy.sh /scripts/
RUN ["/bin/bash","-c","/scripts/install.sh"]
EXPOSE 80 8189
CMD ["/scripts/haproxy.sh"]
[root@localhost files]# cat haproxy.cfg
#--------------全局配置----------------
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
cookie SESSION_cookie insert indirect nocache
server web01 172.17.0.3:80 check inter 2000 fall 5
server web02 172.17.0.4:80 check inter 2000 fall 5
#server web01 192.168.47.163:80 cookie web01 check inter 2000 fall 5
//记得给X权限 [root@localhost files]# cat haproxy.sh #!/bin/bash haproxy -f /etc/haproxy/haproxy.cfg tail -f /etc/hosts
//记得给X权限
[root@localhost files]# cat install.sh
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-${version}.tar.gz
cd haproxy-$version
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
cp haproxy /usr/sbin/
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
rm -rf /usr/src/* /var/cache/*
yum -y remove gcc gcc-c++
构建镜像
[root@localhost haproxy]# docker build -t haproxy:0.3 /haproxy/
Sending build context to Docker daemon 3.601MB
Step 1/10 : FROM centos
---> 5d0da3dc9764
Step 2/10 : LABEL MAINTAINER aaa xaw@qq.com
---> Using cache
---> 03221ef080e2
Step 3/10 : ENV version 2.4.0
---> Using cache
---> fc5e35f83c81
Step 4/10 : COPY files/haproxy-${version}.tar.gz /usr/src
---> Using cache
---> e3dd5bc495cf
Step 5/10 : COPY files/haproxy.cfg /etc/haproxy/haproxy.cfg
---> Using cache
---> 62adf2331711
Step 6/10 : COPY files/install.sh /scripts/
---> Using cache
---> 4a9e4a685d16
Step 7/10 : COPY files/haproxy.sh /scripts/
---> 31e5eba0b11e
Step 8/10 : RUN ["/bin/bash","-c","/scripts/install.sh"]
---> Running in 6c5856b292a9
............................................................................
Removing intermediate container 7548ea512bb3
---> 82045ccfc9ca
Step 9/10 : EXPOSE 80 8189
---> Running in cd4fc2d437ab
Removing intermediate container cd4fc2d437ab
---> ec6e4ece6dbe
Step 10/10 : CMD ["/scripts/haproxy.sh"]
---> Running in 0b1dd64883de
Removing intermediate container 0b1dd64883de
---> a5dc3c62ca7c
Successfully built a5dc3c62ca7c
Successfully tagged haproxy:0.4
//创建容器 [root@localhost ~]# docker run --name haproxy888 -dit -p 8080:80 haproxy:0.7 72b42a58a8ec2cf5e58882d6bb2d88de26555070ec572e571f77b3ce8dc2d775 [root@localhost ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 72b42a58a8ec haproxy:0.7 "/scripts/haproxy.sh" 9 seconds ago Up 7 seconds 8189/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp haproxy888 [root@localhost ~]# docker exec -it haproxy888 /bin/bash [root@72b42a58a8ec /]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:8189 0.0.0.0:* [root@72b42a58a8ec /]#
//创建两个容器实现负载均衡,一台nginx一台httpd [root@localhost ~]# docker run -it --rm --name httpdddddd centos [root@cad86319e1f3 /]# ip a 1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 12: eth0@if13: mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever [root@cad86319e1f3 /]# yum -y install httpd //这是nginx [root@localhost ~]# docker run -itd --name nginxww nginx 26cbe7129e10a49f021577ecf938366b1adae670c78bf6ef10004aa12b815300 [root@localhost ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 26cbe7129e10 nginx "/docker-entrypoint.…" 12 seconds ago Up 11 seconds 80/tcp nginxww 72b42a58a8ec haproxy:0.7 "/scripts/haproxy.sh" 3 minutes ago Up 3 minutes 8189/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp haproxy888 //查看容器 [root@localhost haproxy]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f106173e3b69 haproxyyyy "/scripts/haproxy.sh" about a minute ago Up about a minute 8189/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp haproxy999 cad86319e1f3 centos "/bin/bash" 22 minutes ago Up 22 minutes 80/tcp httpdddddd 26cbe7129e10 nginx "/docker-entrypoint.…" 30 minutes ago Up 30 minutes 80/tcp nginxww



