目 录CONTENT

文章目录

dockerfile 部署haproxy

ZiChen D
2021-12-11 / 0 评论 / 0 点赞 / 418 阅读 / 3,276 字 / 正在检测是否收录...

使用Dockerfile方式安装haproxy服务

目录结构

[root@localhost haproxy_dockerfile]# tree
.
├── Dockerfile
├── entrypoint.sh
└── files
    ├── haproxy-2.4.0.tar.gz
    └── install.sh

1 directory, 4 files

编写Dockerfile安装haproxy

# This dockerfile uses the centos image

# 基础镜像
FROM centos

# 维护者信息
LABEL MAINTAINER=dzc@163.com

# 设置变量
ENV version 2.4.0
ENV PATH /usr/local/haproxy/sbin:$PATH
ENV RSs ""


# 复制文件
COPY files /usr/src/
COPY entrypoint.sh /

# 执行安装脚本安装
RUN ["/bin/bash","-C","/usr/src/install.sh"]

ENTRYPOINT ["/entrypoint.sh"]

使用entrypoint.sh方式编写haproxy.cfg文件

#!/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
i=1
for rs_ip in $RSs;do
cat >> /usr/local/haproxy/conf/haproxy.cfg <<EOF
    server web$i $rs_ip:80 check inter 2000 fall 5
EOF
let i++
done

haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db		//使haproxy在前台运行

编写install.sh

#!/bin/bash

yum -y install make gcc 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
sysctl  -p

mkdir -p /usr/local/haproxy/conf
rm -rf /usr/src/*

创建镜像

Sending build context to Docker daemon  3.601MB
Step 1/9 : FROM centos
 ---> 5d0da3dc9764
Step 2/9 : LABEL MAINTAINER=dzc@163.com
 ---> Running in 63d08e6306ff
Removing intermediate container 63d08e6306ff
 ---> 6fe198e29a5f
Step 3/9 : ENV version 2.4.0
 ---> Running in 73f10ab73cc6
Removing intermediate container 73f10ab73cc6
 ---> a44e2ed4f5fd
Step 4/9 : ENV PATH /usr/local/haproxy/sbin:$PATH
 ---> Running in 1a52df4d513d
Removing intermediate container 1a52df4d513d
 ---> bef763c2a5d5
Step 5/9 : ENV RSs ""
 ---> Running in 9f16506d17fe
Removing intermediate container 9f16506d17fe
 ---> d83e64b3a046
Step 6/9 : COPY files /usr/src/
 ---> 3616fdc79feb
Step 7/9 : COPY entrypoint.sh /
 ---> 1a5e212c56eb
Step 8/9 : RUN ["/bin/bash","-C","/usr/src/install.sh"]
 ---> Running in 0bf766d78af2
Removing intermediate container 0bf766d78af2
 ---> a01ee329859e
Step 9/9 : ENTRYPOINT ["/entrypoint.sh"]
 ---> Running in 37e77af142a3
Removing intermediate container 37e77af142a3
 ---> 2dfe24866907
Successfully built 2dfe24866907
Successfully tagged haproxy:v0.1

使用镜像创建容器

[root@localhost haproxy_dockerfile]# docker run -d --name haproxy -p 80:80 -e RSs="172.17.0.3 172.17.0.4" haproxy:v0.1
491577b03445811f3c9c9840150f005ee3b359e24abc756e3048de2c594188f2

[root@localhost haproxy_dockerfile]# docker ps
CONTAINER ID   IMAGE          COMMAND            CREATED          STATUS          PORTS                               NAMES
491577b03445   haproxy:v0.1   "/entrypoint.sh"   13 seconds ago   Up 12 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   haproxy

创建nginx容器与httpd容器进行测试

[root@localhost haproxy_dockerfile]# docker run -d --name httpd httpd
a3c37f83cc21a0e3c80b8d2167a5d5091d1724967417d67d386dd1b1320852fc
[root@localhost haproxy_dockerfile]# docker run -d --name nginx nginx
4f57b2f4389cc25da1c87243d955f2cf0cc5678d3223055c747a9fb4f8b61cad

测试

//访问haproxyIP
[root@localhost haproxy_dockerfile]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>

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

0

评论区