使用dockerfile编写haproxy镜像
[root@localhost ~]# tree haproxy/
haproxy/
├── Dockerfile
└── files
├── haproxy-2.4.0.tar.gz
├── haproxy.cfg
├── install.sh
└── start.sh
Dockerfile
[root@localhost ~]# cat haproxy/Dockerfile
FROM centos
LABEL MAINTAINER='xxkk 1@2.com'
ADD files/haproxy-2.4.0.tar.gz /usr/src/
ADD files/install.sh /tmp/
ADD files/haproxy.cfg /etc/haproxy/haproxy.cfg
ADD files/start.sh /start.sh
EXPOSE 8189 80
RUN ["/bin/bash","-c","/tmp/install.sh"]
CMD ["/bin/bash","/start.sh"]
install.sh脚本
[root@localhost ~]# cat haproxy/files/install.sh
#!/bin/bash
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 -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
useradd -r -M -s /sbin/nologin haproxy
cd /usr/src/haproxy-2.4.0
make clean && \
make 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
启动脚本
[root@docker /]# cat /haproxy/files/start.sh
#!/bin/sh
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
/bin/bash
haproxy配置文件
[root@localhost ~]# cat haproxy/files/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 192.168.100.3:80 check inter 2000 fall 5
server web02 192.168.100.4:80 check inter 2000 fall 5
制作镜像
[root@localhost ~]# docker build -t haproxy:v0.3 haproxy
Sending build context to Docker daemon 3.6MB
Step 1/7 : FROM centos
---> 5d0da3dc9764
Step 2/7 : LABEL MAINTAINER='xxkk 1@2.com'
---> Using cache
---> 87a719477c29
Step 3/7 : ADD files/haproxy-2.4.0.tar.gz /usr/src/
---> Using cache
启动容器
[root@localhost ~]# docker run -tid --name haproxy 267563f33047
f483ae6b77a1958317a9b64b313c8cee705eabce9b97fee0ef0d7db1feb19df5
[root@localhost ~]# docker exec -it haproxy /bin/bash
[root@f483ae6b77a1 /]# /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
[root@f483ae6b77a1 /]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
编写web1网页文件
[root@637db4367007 /]# vi /var/www/html/index.html
[root@637db4367007 /]# cat /var/www/html/index.html
web1
[root@637db4367007 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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
93: eth0@if94: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:c0:a8:64:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.100.3/24 brd 192.168.100.255 scope global eth0
valid_lft forever preferred_lft forever
编写web2网页文件
[root@d17bc9125eb8 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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
95: eth0@if96: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:c0:a8:64:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.100.4/24 brd 192.168.100.255 scope global eth0
valid_lft forever preferred_lft forever
[root@d17bc9125eb8 /]# vi /var/www/html/index.html
[root@d17bc9125eb8 /]# cat /var/www/html/index.html
web2
测试访问
[root@f483ae6b77a1 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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
91: eth0@if92: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:c0:a8:64:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.100.2/24 brd 192.168.100.255 scope global eth0
valid_lft forever preferred_lft forever
[root@f483ae6b77a1 /]# for i in $(seq 10);do curl 192.168.100.2;done
web1
web2
web1
web2
web1
web2
web1
web2
web1
web2