Dockerfile部署Haproxy负载均衡

软件包下载
[root@Aimmi files]# wget https://www.haproxy.org/download/2.4/src/haproxy-2.4.0.tar.gz
--2021-12-10 23:26:50--  https://www.haproxy.org/download/2.4/src/haproxy-2.4.0.tar.gz
Resolving www.haproxy.org (www.haproxy.org)... 51.15.8.218, 2001:bc8:35ee:100::1
Connecting to www.haproxy.org (www.haproxy.org)|51.15.8.218|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3570069 (3.4M) [application/x-tar]
Saving to: 'haproxy-2.4.0.tar.gz'

haproxy-2.4.0.tar.gz  100%[======================>]   3.40M   642KB/s    in 5.4s    

2021-12-10 23:26:57 (642 KB/s) - 'haproxy-2.4.0.tar.gz' saved [3570069/3570069]

结构目录
[root@Aimmi ~]# tree 
.
|-- anaconda-ks.cfg
|-- haproxy
   |-- Dockerfile
   `-- files
       |-- haproxy-2.4.0.tar.gz
       |-- install.sh
       `-- start.sh

[root@Aimmi ~]# cd haproxy/files/
[root@Aimmi files]# ls
haproxy-2.4.0.tar.gz  install.sh  start.sh
[root@Aimmi files]# chmod +x install.sh start.sh 
[root@Aimmi files]# ll
total 3488
-rw-r--r--. 1 root root 3570069 May 14  2021 haproxy-2.4.0.tar.gz
-rwxr-xr-x. 1 root root       0 Dec 10 23:19 install.sh
-rwxr-xr-x. 1 root root       0 Dec 10 23:20 start.sh

编写Dockerfile
[root@Aimmi haproxy]# vim Dockerfile 
[root@Aimmi haproxy]# cat Dockerfile 
FROM centos
LABEL MAINTAINER='aimmi 123@qq.com'

ENV version 2.4.0
ENV PATH /usr/local/haproxy/sbin:$PATH
COPY files/haproxy-${version}.tar.gz /usr/src/
COPY files/install.sh /usr/src/
COPY files/start.sh /scripts/
RUN ["/bin/bash","-c","/usr/src/install.sh"]

EXPOSE 80 8189
WORKDIR /usr/local/haproxy
CMD /bin/bash /scripts/start.sh $ip1 $ip2

编写安装脚本
[root@Aimmi files]# vim install.sh 
[root@Aimmi 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-$(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 clean all && yum makecache 
echo "alias ls='ls --color'" >> ~/.bashrc
source ~/.bashrc
yum -y install make gcc gcc-c++ 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 $(nproc)  \
    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
mkdir  /etc/haproxy
mkdir /scripts/
echo 'local0.* /var/log/haproxy.log' >>  /etc/rsyslog.conf
yum -y remove gcc gcc-c++ make
rm -rf /usr/src/*  /var/cache/*

编写开启脚本
[root@Aimmi files]# vim start.sh 
[root@Aimmi files]# cat start.sh 
#!/bin/bash
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
    cookie SESSION_COOKIE insert indirect nocache
    server web01 ${1}:80 check inter 2000 fall 5
    server web02 ${2}:80 check inter 2000 fall 5
EOF
/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid

[root@Aimmi ~]# docker build -t aimmi/haproxy:v0.1 haproxy
Sending build context to Docker daemon  3.577MB
Step 1/11 : FROM centos
 ---> 5d0da3dc9764
Step 2/11 : LABEL MAINTAINER='aimmi 123@qq.com'
 ---> Using cache
 ---> c67320bf42d0
Step 3/11 : ENV version 2.4.0
 ---> Using cache
 ---> 875f056b8963
Step 4/11 : ENV PATH /usr/local/haproxy/sbin:$PATH
 ---> Using cache
 ---> 4e307b5ad75a
Step 5/11 : COPY files/haproxy-${version}.tar.gz /usr/src/
 ---> Using cache
 ---> 5adeaf5df338
Step 6/11 : COPY files/install.sh /usr/src/
 ---> Using cache
 ---> 2e3d4e727566
Step 7/11 : COPY files/start.sh /scripts/
 ---> Using cache
 ---> af1c6670aaa0
Step 8/11 : RUN ["/bin/bash","-c","/usr/src/install.sh"]
 ---> Using cache
 ---> 8328c012893b
Step 9/11 : EXPOSE 80 8189
 ---> Using cache
 ---> 6aa3d38d345e
Step 10/11 : WORKDIR /usr/local/haproxy
 ---> Using cache
 ---> 994836a1fe28
Step 11/11 : CMD /bin/bash /scripts/start.sh $ip1 $ip2
 ---> Using cache
 ---> 8b1026365f88
Successfully built 8b1026365f88
Successfully tagged aimmi/haproxy:v0.1
[root@Aimmi ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
aimmi/haproxy   v0.1      8b1026365f88   3 minutes ago   381MB
aimmi/haproxy   v0.2      8b1026365f88   3 minutes ago   381MB

创建两台装容器(一台httpd,一台nginx,用来测试)
[root@Aimmi ~]# docker run --name web01 -d httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
e5ae68f74026: Already exists 
bc36ee1127ec: Pull complete 
d3576f2b6317: Pull complete 
f1aa5f54b226: Pull complete 
aa379c0cedc2: Pull complete 
Digest: sha256:fba8a9f4290180ceee5c74638bb85ff21fd15961e6fdfa4def48e18820512bb1
Status: Downloaded newer image for httpd:latest
12569dbb69a24ca2bd38ed094d3bfb3be4250542ce48284818ed6ff4784f66f1
[root@Aimmi ~]# docker inspect web01
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null

[root@Aimmi ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>

[root@Aimmi ~]# docker run --name web03 -dit nginx 
260105e6e8f97a5bbfbe27ba6f3d84de66514ec61a9f664eab2961b5b61d8acc
[root@Aimmi ~]# docker inspect web03
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
[root@Aimmi ~]# curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

基于新镜像创建haproxy容器
[root@Aimmi ~]# docker run --name haproxy02 -e ip1=172.17.0.2 -e ip2=172.17.0.3  -dit -p 80:80 -p 8189:8189  aimmi/haproxy:v0.2
36286830624d8837532a86e011d6813771f5de46870862327ba7d6fec096ba8a
[root@Aimmi ~]# docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED         STATUS         PORTS                                                                          NAMES
36286830624d   aimmi/haproxy:v0.2   "/bin/sh -c '/bin/ba…"   4 seconds ago   Up 3 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp   haproxy02
260105e6e8f9   nginx                "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes   80/tcp                                                                         web03
12569dbb69a2   httpd                "httpd-foreground"       4 minutes ago   Up 4 minutes   80/tcp                                                                         web01
[root@Aimmi ~]# docker exec -it haproxy02 /bin/bash
[root@36286830624d haproxy]# 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:*                
上一篇:git pull错误:error: Your local changes to the following files would be overwritten by merge


下一篇:Java开发环境安装配置