用Dockerfile部署Haproxy负载均衡

Dockerfile部署Haproxy

//创建dockerfile文件目录以及脚本文件
[root@localhost ~]# mkdir -p  haproxy/files
[root@localhost ~]# ls haproxy/
conf  files
[root@localhost ~]# touch haproxy/Dockerfile
[root@localhost ~]# touch haproxy/files/install.sh
[root@localhost ~]# touch haproxy/start.sh

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

2 directories, 5 files

//添加脚本权限
[root@localhost ~]# cd haproxy/files/
[root@localhost files]# chmod +x install.sh start.sh 
[root@localhost files]# ll 
total 3520
-rw-r--r--. 1 root root 3593224 Dec 10 21:18 haproxy-2.4.0.tar.gz
-rwxr-xr-x. 1 root root     896 Dec 10 22:13 install.sh
-rwxr-xr-x. 1 root root    1592 Dec 10 21:58 start.sh


//创建两台装容器(一台httpd,一台nginx,用来测试)
[root@localhost ~]# docker run --name web05 -d httpd 
e84f1c748e99d47bbf856a5403ec1cb3bb301e6a98daaa0d1e32f786699823a1

[root@localhost ~]# docker inspect web05
                    "Gateway": "172.17.0.2",
                    "IPAddress": "172.17.0.5",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:05",
                    "DriverOpts": null
                }
            }
        }
    }
]


[root@localhost ~]# docker run --name web06 -dit nginx 
4d9789e30cacde1baa2bbe45d9141a085dc8e7213bb0ee7c33712dd8a645088b
[root@localhost ~]# docker inspect web06

                    "IPAddress": "172.17.0.6",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:06",
                    "DriverOpts": null
                }
            }
        }
    }
]

//测试一下
[root@localhost ~]# curl 172.17.0.5
<html><body><h1>It works!</h1></body></html>


[root@localhost ~]# curl 172.17.0.6
<!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>


//编写dockerfile
[root@localhost ~]# cat haproxy/Dockerfile 
FROM centos
LABEL MAINTAINER='1314444 [email protected]'

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@localhost ~]# cat haproxy/files/install.sh 
#!/bin/bash
[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 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@localhost ~]# cat haproxy/files/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

//构建haproxy镜像
[root@localhost ~]# docker build -t 1314444/haproxy:v0.2 haproxy
Sending build context to Docker daemon  3.601MB
Step 1/11 : FROM centos
 ---> 5d0da3dc9764
Step 2/11 : LABEL MAINTAINER='1314444 [email protected]'
 ---> Using cache
 ---> a8b67caa2102
Step 3/11 : ENV version 2.4.0
 ---> Using cache
 ---> c48a871bda67
Step 4/11 : ENV PATH /usr/local/haproxy/sbin:$PATH
 ---> Using cache
 ---> df0ccbe70aba
Step 5/11 : COPY files/haproxy-${
    
    version}.tar.gz /usr/src/
 ---> Using cache
 ---> 8edb39c3516e
Step 6/11 : COPY files/install.sh /usr/src/
 ---> Using cache
 ---> 2d85ef65b39e
Step 7/11 : COPY files/start.sh /scripts/
 ---> Using cache
 ---> d519b2527fe0
Step 8/11 : RUN ["/bin/bash","-c","/usr/src/install.sh"]
 ---> Using cache
 ---> 7c7e5b80f577
Step 9/11 : EXPOSE 80 8189
 ---> Using cache
 ---> 6d7da4d0e355
Step 10/11 : WORKDIR /usr/local/haproxy
 ---> Using cache
 ---> a4c30b211e44
Step 11/11 : CMD /bin/bash /scripts/start.sh $ip1 $ip2
 ---> Using cache
 ---> f751abad7885
Successfully built f751abad7885
Successfully tagged 1314444/haproxy:v0.2


//查看镜像
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
1314444/haproxy   v0.2      f751abad7885   About a minute ago   381MB
1314444/haproxy   v0.1      baef3d805348   About an hour ago    634MB

//基于新镜像创建haproxy容器
[root@localhost ~]# docker run --name haproxy02 -e ip1=172.17.0.5 -e ip2=172.17.0.6  -dit -p 80:80 -p 8189:8189  1314444/haproxy:v0.2
18338dd912eedc957710103fb323a01e9a77e2facc5a8e9bd7164bf4a2665d21
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                                                                          NAMES
18338dd912ee   1314444/haproxy:v0.2   "/bin/sh -c '/bin/ba…"   5 seconds ago    Up 4 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp   haproxy02
4d9789e30cac   nginx                  "/docker-entrypoint.…"   51 minutes ago   Up 51 minutes   80/tcp                                                                         web06
e84f1c748e99   httpd                  "httpd-foreground"       52 minutes ago   Up 52 minutes   80/tcp                                                                         web05
[root@localhost ~]# docker exec -it haproxy02 /bin/bash
[root@18338dd912ee haproxy]# ss -anlt
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:*         

在这里插入图片描述
在这里插入图片描述

用户:admin
密码:admin
在这里插入图片描述
http://IP:8189/haproxy_stats
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_48289488/article/details/121865065