dockerfile编写haproxy镜像

该博客介绍了如何使用Dockerfile构建一个HAProxy镜像,包括从CentOS基础镜像开始,安装HAProxy,配置负载均衡策略,并通过entrypoint.sh启动HAProxy。镜像中包含了监听80和8189端口的配置,以及通过RSe.txt文件动态添加后端服务器的能力。此外,还展示了如何创建和运行HAProxy容器,以及通过Apache镜像创建两个测试服务器进行负载均衡验证。
摘要由CSDN通过智能技术生成

dockerfile结构

[root@localhost ~]# tree haproxy/
haproxy/
├── Dockerfile
└── files
    ├── entrypoint.sh
    ├── haproxy-1.7.8.tar.gz
    ├── haproxy.cfg
    └── install.sh

1 directory, 5 files

dockerfile

[root@localhost ~]# cat haproxy/Dockerfile 
FROM centos

ENV version 1.7.8
ENV PATH /usr/local/harpoxy/sbin:$PATH

ADD files/haproxy-${version}.tar.gz /usr/src/
ADD files/install.sh /tmp/
ADD files/entrypoint.sh /

EXPOSE 8189 80

RUN ["/bin/bash","-c","/tmp/install.sh"]

WORKDIR /usr/local/haproxy/

CMD ["/entrypoint.sh"]

entrypoint.sh

[root@localhost ~]# cat haproxy/files/entrypoint.sh 
#!/bin/bash

cat > /usr/local/haproxy/conf/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
EOF
Snu=1
for rs_ip in $(cat /tmp/RSe.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg <<EOF
    server web$Snu $rs_ip:80 check inter 2000 fall 5
EOF
let Snu++
done

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

/bin/bash

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-${version}
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

mkdir -p /usr/local/haproxy/conf
yum -y remove make gcc gcc-c++ && \
rm -rf /tmp/* /var/cache/*

 做镜像

[root@localhost ~]# docker build -t yangyonghu/haproxy:v0.3 haproxy/
Sending build context to Docker daemon  1.754MB
Step 1/10 : FROM centos
 ---> 5d0da3dc9764
Step 2/10 : ENV version 1.7.8
 ---> Running in affae3ffac91
Removing intermediate container affae3ffac91
 ---> 139159f97ad8
Step 3/10 : ENV PATH /usr/local/harpoxy/sbin:$PATH
 ---> Running in ca335770ef74
Removing intermediate container ca335770ef74
 ---> d2d16324b306
Step 4/10 : ADD files/haproxy-${version}.tar.gz /usr/src/
 ---> 8300658bbf39
Step 5/10 : ADD files/install.sh /tmp/
 ---> 31bf0c1dd3e3
Step 6/10 : ADD files/entrypoint.sh /
 ---> 7b6b4d3400f6
Step 7/10 : EXPOSE 8189 80
 ---> Running in 5858d95a4a58
Removing intermediate container 5858d95a4a58
 ---> 722c7a2eaecb
Step 8/10 : RUN ["/bin/bash","-c","/tmp/install.sh"]
 ---> b2e87962da48
Step 9/10 : WORKDIR /usr/local/haproxy/
 ---> Running in b22715a19eee
Removing intermediate container b22715a19eee
 ---> d6fa8993457f
Step 10/10 : CMD ["entrypoint.sh"]
 ---> Running in 5096deab9936
Removing intermediate container 5096deab9936
 ---> 2d2ae019decf
Successfully built 2d2ae019decf
Successfully tagged yangyonghu/haproxy:v0.3
[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      2d2ae019decf   2 minutes ago   240MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB

创建一个容器来运行haproxy镜像

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   5 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB
[root@localhost ~]# docker run -dit --name haproxy -v /config/:/tmp -p 80:80 -p 8189:8189 d75fa67c9f17
ded46827fd7c4f590a80f52131065d8dc0b8bd02ddc90175dfabe1c81d59cf09
[root@localhost ~]#  docker exec -it haproxy /bin/bash
[root@ded46827fd7c haproxy]# 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@ded46827fd7c 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
15: eth0@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:01 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.1/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever

另起终端查看主机上的端口号

[root@localhost ~]# ss -antl
State               Recv-Q              Send-Q                           Local Address:Port                             Peer Address:Port              
LISTEN              0                   128                                    0.0.0.0:8189                                  0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:111                                   0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:80                                    0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:22                                    0.0.0.0:*                 
LISTEN              0                   128                                       [::]:8189                                     [::]:*                 
LISTEN              0                   128                                       [::]:111                                      [::]:*                 
LISTEN              0                   128                                       [::]:80                                       [::]:*                 
LISTEN              0                   128                                       [::]:22                                       [::]:*         

创建两个新的容器使用apache镜像来测试haproxy

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   9 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB
[root@localhost ~]# docker run -dit --name httpd --rm 677843bb4e15
46a043a66ce7353e7c58d207ea6f9c9d8f4e432763afc05de5c707ff340e991d
[root@localhost ~]# docker exec -it httpd /bin/bash
[root@46a043a66ce7 src]# 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:*                                
[root@46a043a66ce7 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
17: eth0@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.2/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
[root@46a043a66ce7 src]# vi /usr/local/apache/htdocs/index.html
[root@46a043a66ce7 src]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>RS1</h1></body></html>
[root@46a043a66ce7 src]# 

第二个

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   13 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago       703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago       550MB
busybox              latest    d23834f29b38   12 days ago      1.24MB
centos               latest    5d0da3dc9764   2 months ago     231MB
[root@localhost ~]# docker run -dit --name apache --rm 677843bb4e15
de053e2dc3055614936ccdbd61158fdea33818e44a064e00e01d3dbc0c3c938f
[root@localhost ~]# docker exec -it apache /bin/bash
[root@de053e2dc305 src]# 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:*                                
[root@de053e2dc305 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
19: eth0@if20: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.3/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever

[root@de053e2dc305 src]# vi /usr/local/apache/htdocs/index.html
[root@de053e2dc305 src]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>RS2</h1></body></html>

访问页面做测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值