本文主要是介绍Dockerfile部署haproxy,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录结构
[root@localhost ~]# tree haproxy/
haproxy/
|-- Dockerfile
`-- files
|-- haproxy-2.5.0.tar.gz
|-- haproxy.cfg
|-- install.sh
`-- start.sh
1 directory, 5 files
Dockerfile文件
FROM centos
LABEL MAINTAINER "gaofan1225 123@qq.com"
ENV haproxy_version 2.5.0
ADD files/haproxy-${haproxy_version}.tar.gz /usr/src/
ADD files/haproxy.cfg /usr/local/haproxy/conf/
ADD files/start.sh /tmp/
ADD files/install.sh /tmp/
RUN ["/bin/bash","-c","/tmp/install.sh"]
EXPOSE 80 8189
WORKDIR /usr/local/haproxy
CMD ["bin/bash","/tmp/start.sh"]
安装脚本
[root@localhost ~]# cd haproxy/files/
[root@localhost files]# ls
haproxy-2.5.0.tar.gz
[root@localhost files]# touch install.sh
[root@localhost files]# chmod +x install.sh
[root@localhost 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
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/haproxy-${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
cp haproxy /usr/sbin/
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
rm -rf /var/cache/* /usr/src/haproxy-${haproxy_version}
yum -y remove gcc gcc-c++ make
启动服务脚本
[root@localhost files]# touch start.sh
[root@localhost files]# chmod +x start.sh
[root@localhost files]# cat start.sh
#!/bin/bash
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
/bin/bash
编写配置文件
[root@localhost files]# vim 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 httplogZZZZ
#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 web1 172.17.0.2:80 check inter 2000 fall 5
server web2 172.17.0.3:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
构建haproxy镜像
[root@localhost ~]# docker build -t haproxy:v0.1 haproxy/
Successfully built c23b8c6514b3
Successfully tagged haproxy:v0.1
创建haproxy容器
[root@localhost ~]# docker run -itd --name haproxy -p 80:80 e00d757cd30c
6d101223eceb3be02a2e34718de59482464f9b515927a8409f899399de2f60a8
[root@localhost ~]# docker exec -it haproxy /bin/bash
[root@6d101223eceb 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:*
使用httpd镜像创建两个容器
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v0.1 e00d757cd30c 17 minutes ago 410MB
httpd v0.3 949e5f001032 11 hours ago 423MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -dit --name httpd1 httpd:v0.3
44d6692c3a15e83458544e93b982f27ce7c2c13211ef839e203063fa834f95f3
[root@localhost ~]# docker exec -it httpd1 /bin/bash
[root@44d6692c3a15 apache]# 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
27: eth0@if28: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@44d6692c3a15 apache]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>It works!</h1></body></html>
[root@44d6692c3a15 apache]# echo "amu" > /usr/local/apache/htdocs/index.html
[root@44d6692c3a15 apache]# cat /usr/local/apache/htdocs/index.html
amu
[root@localhost ~]# docker run -dit --name httpd2 httpd:v0.3
0d5f1504f4e4b235543414120d4f0f4822d98734d515d83da959e4ed1fffed80
[root@localhost ~]# docker exec -it httpd2 /bin/bash
[root@0d5f1504f4e4 apache]# 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
29: eth0@if30: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@0d5f1504f4e4 apache]# echo "gf" > /usr/local/apache/htdocs/index.html
[root@0d5f1504f4e4 apache]# cat /usr/local/apache/htdocs/index.html
gf
页面访问
这篇关于Dockerfile部署haproxy的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!