树结构
[root@server1 ~]# tree haproxy/
haproxy/
|-- 1
|-- Dockerfile
`-- files
|-- haproxy-2.4.0.tar.gz
|-- haproxy.cfg
|-- install.sh
|-- start.sh
`-- sysctl.conf
编写Dockerfile
[root@server1 ~]# cat haproxy/Dockerfile FROM centos:latest ADD files/haproxy-2.4.0.tar.gz /usr/src ADD files/install.sh /tmp/install.sh ADD files/start.sh /tmp/start.sh RUN ["/bin/bash","-c","/tmp/install.sh"] COPY files/sysctl.conf /etc/sysctl.conf COPY files/haproxy.cfg /etc/haproxy/haproxy.cfg EXPOSE 8189 80 CMD ["/bin/bash","/tmp/start.sh"]
编写install.sh
[root@server1 files]# cat 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-8.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 -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_SYSTEMD=1 make install PREFIX=/usr/local/haproxy
编写start.sh
[root@server1 files]# cat sysctl.conf net.ipv4.ip_nonlocal_bind = 1 net.ipv4.ip_forward = 1
编写haproxy.cfg
[root@server1 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
server web01 192.168.244.144:81 check inter 2000 fall 5
server web02 192.168.244.144:82 check inter 2000 fall 5
#要负载的IP地址
制作镜像
[root@server1 ~]# docker build -t sktystwd/haproxy:v0.4 haproxy/
创建容器
[root@server1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE sktystwd/haproxy v0.4 6e485d503b1b about a minute ago 578MB sktystwd/haproxy v0.3 4b7b7d5a830a 21 minutes ago 578MB sktystwd/haproxy v0.2 01393b9868c2 32 minutes ago 578MB sktystwd/haproxy v0.1 8f51e208fb33 51 minutes ago 578MB nginx latest f652ca386ed1 8 days ago 141MB centos centos8.4.2105 5d0da3dc9764 2 months ago 231MB centos latest 5d0da3dc9764 2 months ago 231MB [root@server1 ~]# docker run -it -P --name haproxy1 6e485d503b1b [root@6f4740949d56 /]# 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:*
在宿主机上查看映射端口
[root@server1 ~]# docker port 6f4740949d56 8189/tcp -> 0.0.0.0:49155 8189/tcp -> :::49155 80/tcp -> 0.0.0.0:49156 80/tcp -> :::49156
访问
192.168.244.144:49155/haproxy_stars
创建镜像
[root@server1 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6f4740949d56 6e485d503b1b "/bin/bash /tmp/star…" 22 minutes ago Up 3 minutes 0.0.0.0:49160->80/tcp, :::49160->80/tcp, 0.0.0.0:49159->8189/tcp, :::49159->8189/tcp haproxy1 92a53979a3a8 centos:latest "/bin/bash" 24 minutes ago Up 24 minutes 0.0.0.0:82->80/tcp, :::82->80/tcp httpd3 8a89a17ef6be centos:latest "/bin/bash" 24 minutes ago Up 24 minutes 0.0.0.0:81->80/tcp, :::81->80/tcp httpd2
访问
192.168.244.144:81
192.168.244.144:82



