结构
[root@docker ~]# tree /haproxy/ /haproxy/ ├── dockerfile └── file ├── haproxy-2.4.0.tar.gz └── harpoxy.cfg
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.66.2:80 check inter 2000 fall 5 server web02 192.168.66.3:80 check inter 2000 fall 5
//创建dockerfile存放目录 [root@docker ~]# mkdir -p /haproxy/file [root@docker ~]# cd /haproxy/ [root@docker haproxy]# mv /root/haproxy-2.4.0.tar.gz ./file/ //编写dockerfile [root@docker haproxy]# cat dockerfile FROM centos LABEL MAINTAINER='487021296@qq.com' ENV verison 2.4.0 ADD file/haproxy-${verison}.tar.gz /usr/src COPY file/haproxy.cfg /etc/haproxy/haproxy.cfg RUN 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 -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_SYSTEMD=1 && make install PREFIX=/usr/local/haproxy && \ cp /usr/local/haproxy/sbin/haproxy /usr/sbin && \ echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf && \ echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf && \ yum -y remove make gcc && \ rm -rf /var/cache/* && \ rm -rf /usr/src/haproxy-${version} EXPOSE 80 8189 WORKDIR /usr/local/haproxy ENTRYPOINT ["/usr/local/haproxy/sbin/haproxy"] CMD ["-db","-f","/etc/haproxy/haproxy.cfg","-p","/var/run/haproxy.pid"] //创建镜像 [root@docker haproxy]# docker build -t haproxy:v0.1 /haproxy [root@docker haproxy]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE haproxy v0.1 44ff2a438db0 3 minutes ago 461MB //创建haproxy容器 [root@docker ~]# docker run -d --rm --name haproxy -p 80:80 haproxy:v0.1 40c18d1c3aa7cd78526c2a27699d1a17276ad94df7334a48c2676c0f8c3d5446 [root@docker ~]# docker exec -it haproxy /bin/bash [root@40c18d1c3aa7 haproxy]# 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 69: eth0@if70: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:42:01 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.66.1/24 brd 192.168.66.255 scope global eth0 valid_lft forever preferred_lft forever
创建一个nginx容器
[root@docker ~]# docker run -d --rm --name web1 linlusama/centos-nginx:v1.20.1 7622549fd19d53557ac5beabea5a0cef2b9ade338d029abe079ecb3285432fb9 [root@docker ~]# docker exec -it web1 /bin/bash [root@7622549fd19d /]# 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 71: eth0@if72: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:42:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.66.2/24 brd 192.168.66.255 scope global eth0 valid_lft forever preferred_lft forever
创建一个httpd容器
[root@docker ~]# docker run -d --rm --name web2 linlusama/apache:latest e74dbf195ec2e617db8656e528939afd3991192e468f640f84362242013656e7 [root@docker ~]# docker exec -it web2 /bin/bash [root@e74dbf195ec2 src]# 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 73: eth0@if74: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:42:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.66.3/24 brd 192.168.66.255 scope global eth0 valid_lft forever preferred_lft forever
访问
[root@docker ~]# for i in $(seq 10);do curl 192.168.200.150;done 我是徐猛他爹 <html><body><h1>It works!</h1></body></html> 我是徐猛他爹 <html><body><h1>It works!</h1></body></html> 我是徐猛他爹 <html><body><h1>It works!</h1></body></html> 我是徐猛他爹 <html><body><h1>It works!</h1></body></html> 我是徐猛他爹 <html><body><h1>It works!</h1></body></html>