编写haproxy的Dockerfile

编写haproxy的Dockerfile

结构

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

2 directories, 5 files

创建目录结构

[root@localhost ~]# mkdir -p  /haproxy/files/
[root@localhost haproxy]# touch Dockerfile

编写Dockerfile

[root@localhost haproxy]# vim Dockerfile 
#基础镜像
FROM centos

#作者信息
LABEL MAINTAINER bravealove1 123@qq.com

#环境变量
ENV VERSION 2.5.0

#传输文件
ADD files/haproxy-${VERSION}.tar.gz  /usr/src
ADD files/entrypoint.sh  /scripts/
ADD files/install.sh /scripts/

#安装
RUN ["/bin/bash","-c","/scripts/install.sh"]    

#暴露端口
EXPOSE 80 8189

#启动命令
CMD ["/scripts/entrypoint.sh"]

提供Dockerfile所需的文件

安装脚本

[root@localhost haproxy]# cd files/
[root@localhost files]# touch install.sh 
[root@localhost files]# chmod +x install.sh
[root@localhost files]# vim install.sh
#!/bin/bash
RUN 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-${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
mkdir  -p  /usr/local/haproxy/conf/
rm -rf /usr/src/haproxy-${VERSION} /var/cache/*
yum -y remove make gcc gcc-c++

启动脚本

[root@localhost files]# touch entrypoint.sh
[root@localhost files]# chmod +x entrypoint.sh
[root@localhost files]# vim 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

server=1
for RS in `cat /RS/RS.txt`
do
cat >> /usr/local/haproxy/conf/haproxy.cfg  <<EOF
server web$server $RS:80 check inter 2000 fall 5        
EOF
let server++
done


haproxy -f /usr/local/haproxy/conf/haproxy.cfg
/bin/bash

构建haproxy镜像

[root@localhost ~]# docker build -t haproxy:latest /haproxy/
Sending build context to Docker daemon  3.813MB
Step 1/9 : FROM centos
 ---> 5d0da3dc9764
Step 2/9 : LABEL MAINTAINER bravealove1 123@qq.com
 ---> Using cache
 ---> 627936ae2a25
Step 3/9 : ENV VERSION 2.5.0
 ---> Using cache
 ---> 8f898adf989e
Step 4/9 : ADD files/haproxy-${VERSION}.tar.gz  /usr/src
 ---> Using cache
 ---> 78080143ad7e
Step 5/9 : ADD files/entrypoint.sh  /scripts/
 ---> Using cache
 ---> c5e5953d3ca1
Step 6/9 : ADD files/install.sh /scripts/
 ---> Using cache
 ---> 84c67726fd72
Step 7/9 : RUN ["/bin/bash","-c","/scripts/install.sh"]
 ---> Using cache
 ---> 20305a9b9ec4
Step 8/9 : EXPOSE 80 8189
 ---> Using cache
 ---> 1df20f885220
Step 9/9 : CMD ["/scripts/entrypoint.sh"]
 ---> Using cache
 ---> fa8c8d05562f
Successfully built fa8c8d05562f
Successfully tagged haproxy:latest

创建两台装有apache的容器

容器web01

[root@localhost ~]# docker run --name web01 -dit centos /bin/bash 
8990443f0d758cf468071c4ccfbcaed9b13ccf39f0897d9266d83b8efddb54ba
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE       COMMAND               CREATED          STATUS          PORTS                                         NAMES
8990443f0d75   centos      "/bin/bash"           8 minutes ago    Up 8 minutes                                                  web01

[root@localhost ~]# docker exec  -it web01 /bin/bash
[root@8990443f0d75 ~]# 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
67: eth0@if68: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:96:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.150.2/24 brd 192.168.150.255 scope global eth0
       valid_lft forever preferred_lft forever
       
[root@8990443f0d75 /]# yum -y install httpd
[root@8990443f0d75 /]# vi /var/www/html/index.html
web01
[root@8990443f0d75 ~]# pkill httpd
[root@8990443f0d75 ~]# apachectl 

容器web02

[root@localhost ~]# docker run --name web02 -dit centos /bin/bash
148f7e155ffac232b57cd31728265a4d600562c1e37af7a3c4606864201a39fd
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE       COMMAND               CREATED          STATUS          PORTS                                         NAMES
148f7e155ffa   centos      "/bin/bash"           8 minutes ago    Up 8 minutes                                                  web02
[root@localhost ~]# docker exec  -it web02 /bin/bash
[root@148f7e155ffa /]# 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
69: eth0@if70: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:96:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.150.3/24 brd 192.168.150.255 scope global eth0
       valid_lft forever preferred_lft forever
       
[root@148f7e155ffa /]# yum -y install httpd
[root@148f7e155ffa /]# vi /var/www/html/index.html
web02
[root@148f7e155ffa /]# pkill  httpd
[root@148f7e155ffa /]# apachectl 

创建RS文件

[root@localhost ~]# mkdir /RS/
[root@localhost RS]# touch RS.txt 
[root@localhost RS]# vim  RS.txt 
192.168.150.2
192.168.150.3

基于镜像创建haproxy容器

[root@localhost ~]# docker run --name haproxy -dit -p 80:80 -p 8189:8189 -v /RS:/RS haproxy:latest
082123f011e3299c0781f4282de23850c79e2673efac4b081cf27c9489ffbb55
[root@localhost ~]# docker exec -it haproxy  /bin/bash
[root@082123f011e3 /]# 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@082123f011e3 /]# 

访问测试
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

使用alpine作为基础镜像编译安装haproxy

结构

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

1 directory, 4 files

创建目录结构

[root@localhost ~]# mkdir -p  /haproxy/files/
[root@localhost haproxy]# touch Dockerfile

编写Dockerfile

#基础镜像
FROM alpine

#作者信息
LABEL MAINTAINER bravealove1 123@qq.com

#环境变量
ENV VERSION 2.5.0

#传输文件
ADD files/haproxy-${VERSION}.tar.gz  /usr/src
ADD files/entrypoint.sh  /scripts/
ADD files/install.sh /scripts/

#安装
RUN  /scripts/install.sh   

#暴露端口
EXPOSE 80 8189

#启动命令
CMD /scripts/entrypoint.sh

提供Dockerfile所需的文件

安装脚本

[root@localhost haproxy]# cd files/
[root@localhost files]# touch install.sh 
[root@localhost files]# chmod +x install.sh
[root@localhost files]# vim install.sh
#!/bin/sh
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories
apk update
adduser -S -H -s /sbin/nologin haproxy
addgroup haproxy
apk add --no-cache -U make gcc pcre-dev bzip2-dev openssl-dev elogind-dev libc-dev dahdi-tools  dahdi-tools-dev  libexecinfo libexecinfo-dev ncurses-dev zlib-dev zlib
cd /usr/src/haproxy-${VERSION} 
make  TARGET=linux-musl  USE_OPENSSL=1  USE_ZLIB=1  USE_PCRE=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/
rm -rf /usr/src/haproxy-${VERSION} /var/cache/*
apk del  gcc make

启动脚本

[root@localhost files]# touch entrypoint.sh 
[root@localhost files]# chmod +x entrypoint.sh
[root@localhost files]# vim entrypoint.sh 
#!/bin/sh

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

server=1
for RS in `cat /RS/RS.txt`
do
cat >> /usr/local/haproxy/conf/haproxy.cfg  <<EOF
server web$server $RS:80 check inter 2000 fall 5        
EOF
let server++
done


haproxy -f /usr/local/haproxy/conf/haproxy.cfg
/bin/sh

构建haproxy镜像

[root@localhost ~]# docker build -t haproxy:latest /haproxy/
Sending build context to Docker daemon  3.824MB
Step 1/9 : FROM alpine
 ---> c059bfaa849c
Step 2/9 : LABEL MAINTAINER bravealove1 123@qq.com
 ---> Using cache
 ---> 0e36d605dfa4
Step 3/9 : ENV VERSION 2.5.0
 ---> Using cache
 ---> 03992bf7269c
Step 4/9 : ADD files/haproxy-${VERSION}.tar.gz  /usr/src
 ---> Using cache
 ---> 91a660f6154b
Step 5/9 : ADD files/entrypoint.sh  /scripts/
 ---> Using cache
 ---> 19306a6f23f7
Step 6/9 : ADD files/install.sh /scripts/
 ---> Using cache
 ---> e48a3cf1e014
Step 7/9 : RUN  /scripts/install.sh
 ---> Using cache
 ---> ac689256590a
Step 8/9 : EXPOSE 80 8189
 ---> Using cache
 ---> 9e7a29d6f692
Step 9/9 : CMD /scripts/entrypoint.sh
 ---> Using cache
 ---> 0d99a68e406d
Successfully built 0d99a68e406d
Successfully tagged haproxy:latest

创建两台装有apache的容器

容器web01

[root@localhost ~]# docker run --name web01 -dit centos /bin/bash 
8990443f0d758cf468071c4ccfbcaed9b13ccf39f0897d9266d83b8efddb54ba
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE       COMMAND               CREATED          STATUS          PORTS                                         NAMES
8990443f0d75   centos      "/bin/bash"           8 minutes ago    Up 8 minutes                                                  web01

[root@localhost ~]# docker exec  -it web01 /bin/bash
[root@8990443f0d75 ~]# 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
67: eth0@if68: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:96:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.150.2/24 brd 192.168.150.255 scope global eth0
       valid_lft forever preferred_lft forever
       
[root@8990443f0d75 /]# yum -y install httpd
[root@8990443f0d75 /]# vi /var/www/html/index.html
web01
[root@8990443f0d75 ~]# pkill httpd
[root@8990443f0d75 ~]# apachectl 

容器web02

[root@localhost ~]# docker run --name web02 -dit centos /bin/bash
148f7e155ffac232b57cd31728265a4d600562c1e37af7a3c4606864201a39fd
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE       COMMAND               CREATED          STATUS          PORTS                                         NAMES
148f7e155ffa   centos      "/bin/bash"           8 minutes ago    Up 8 minutes                                                  web02
[root@localhost ~]# docker exec  -it web02 /bin/bash
[root@148f7e155ffa /]# 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
69: eth0@if70: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:96:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.150.3/24 brd 192.168.150.255 scope global eth0
       valid_lft forever preferred_lft forever
       
[root@148f7e155ffa /]# yum -y install httpd
[root@148f7e155ffa /]# vi /var/www/html/index.html
web02
[root@148f7e155ffa /]# pkill  httpd
[root@148f7e155ffa /]# apachectl 

创建RS文件

[root@localhost ~]# mkdir /RS/
[root@localhost RS]# touch RS.txt 
[root@localhost RS]# vim  RS.txt 
192.168.150.2
192.168.150.3

基于新镜像创建haproxy容器


[root@localhost ~]#docker run  --name haproxy -dit -p 80:80 -p 8189:8189 -v /RS:/RS haproxy:latest 
1ab0c59be58f5cd4027c28961db152f4f82ce4a0d0eef0b594a535b528f6fcb9
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE       COMMAND               CREATED         STATUS         PORTS                                         NAMES
1ab0c59be58f   haproxy:latest   "/scripts/entrypoint.sh"   3 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 8189/tcp   haproxy

[root@localhost ~]# docker exec  -it haproxy /bin/sh
/ # ss -antl
/bin/sh: ss: not found
/ # apk add iproute2
/ # 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:*              

访问测试

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值