Dockerfile部署Haproxy

Dockerfile部署Haproxy

[root@docker haproxy]# tree
├── Dockerfile
└── files
    ├── haproxy-2.4.0.tar.gz
    ├── install.sh
    └── sysctl.conf

// 编写Dockerfile文件
[root@docker haproxy]# vim Dockerfile 
FROM centos
LABEL MAINTAINER='mkf@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@docker haproxy]# vim 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@docker haproxy]# chmod +x files/install.sh

// 宿主机上提供haproxy配置文件
[root@docker haproxy]# mkdir /etc/haproxy
[root@docker haproxy]# vim /etc/haproxy/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
    cookie SESSION_COOKIE insert indirect nocache
    server web01 ${1}:80 check inter 2000 fall 5
    server web02 ${2}:80 check inter 2000 fall 5

// 构建haproxy镜像
[root@localhost ~]# docker build -t 1450146910/haproxy:v0.1 haproxy

// 创建两台容器测试
[root@localhost ~]# docker run --name web01 -d httpd
[root@localhost ~]# docker inspect web01
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

[root@localhost ~]# docker run --name web02 -d nginx 
[root@localhost ~]# docker inspect web02
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# 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@localhost ~]#  docker run --name haproxy01 -e ip1=172.17.0.2 -e ip2=172.17.0.3  -dit -p 80:80 -p 8189:8189  1450146910/haproxy:v0.1
00787d187c1f0db316f37eabbb2f412a934d2fc1582355f92fe128da310d6062

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值