1 制作基础haproxy 镜像
1.1 镜像部署整体结构
[root@docker files]# tree /haproxy/
/haproxy/
├── Dockerfile
└── files
├── haproxy-2.5.0.tar.gz
├── haproxy.cfg
├── install.sh
└── start.sh
1 directory, 5 files
[root@docker files]#
1.2 Dockerfile编写
[root@docker haproxy]# cat Dockerfile
# 基础镜像
FROM centos
# 镜像维护者信息
LABEL MAINTAINER='xixi 1@2.com'
#变量
ENV version 2.5.0
#传文件到容器
ADD files/haproxy-${version}.tar.gz /usr/src/
ADD files/install.sh /tmp/
ADD files/start.sh /tmp/
ADD files/haproxy.cfg /etc/haproxy/
#镜像操作指令
RUN ["/bin/bash","-c","/tmp/install.sh"]
# 工作中目录
WORKDIR /usr/local/haproxy
#暴露端口
EXPOSE 80 8189
#启动命令
CMD ["/bin/bash","/tmp/start.sh"]
1.3 执行脚本编写
root@docker haproxy]# cat 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 -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
useradd -r -M -s /sbin/nologin haproxy
cd /usr/src/haproxy-${version}
make clean
make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
TARGET=linux-glibc \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 \
USE_SYSTEMD=1 && \
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/src
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
yum -y remove gcc make
rm -rf /var/cache/*
rm -rf /usr/src/haproxy-${version}
[root@docker haproxy]#
1.4 启动脚本编写
[root@docker haproxy]# cat files/start.sh
#!/bin/sh
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
/bin/bash
[root@docker haproxy]#
1.5 haproxy配置文件
[root@docker haproxy]# cat files/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 192.168.10.1:80 check inter 2000 fall 5
server web02 192.168.10.2:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
[root@docker haproxy]#
1.6 构建镜像
// 创建haproxy镜像
[root@Docker ~]# docker build -t haproxy:v2.0 haproxy/
[root@docker haproxy]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v2.0 0a48c089ff0b 53 minutes ago 410MB
nginx latest f652ca386ed1 8 days ago 141MB
busybox latest d23834f29b38 10 days ago 1.24MB
httpd latest ad17c88403e2 3 weeks ago 143MB
centos latest 5d0da3dc9764 2 months ago 231MB
1.7 创建容器访问测试
[root@docker ~]# docker run -itd --name haproxy -P --rm haproxy:v2.0
bcfd0e7d144447a633f8ca4e0035aa040ef1e675970a25ae54542b727c9b1c66
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bcfd0e7d1444 haproxy:v2.0 "/bin/bash /tmp/star…" 18 seconds ago Up 17 seconds 0.0.0.0:49158->80/tcp, :::49158->80/tcp, 0.0.0.0:49157->8189/tcp, :::49157->8189/tcp haproxy
[root@docker ~]# docker exec -it haproxy /bin/bash
[root@bcfd0e7d1444 haproxy]# 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:*
## 创建一个nginx和httpd容器用haproxy来做调度器
[root@docker ~]# docker run -itd --name nginx nginx
[root@docker ~]# docker run -itd --name httpd httpd
[root@docker ~]# docker images
bcfd0e7d1444 haproxy:v2.0 "/bin/bash /tmp/star…" 18 seconds ago Up 17 seconds 0.0.0.0:49158->80/tcp, :::49158->80/tcp, 0.0.0.0:49157->8189/tcp, :::49157->8189/tcp haproxy
d86bbe61db41 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 80/tcp a2
81142316f963 httpd "httpd-foreground" 2 hours ago Up 2 hours 80/tcp a1
[root@docker ~]# curl 192.168.10.3
<html><body><h1>It works!</h1></body></html>
[root@docker ~]# curl 192.168.10.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>
[root@docker ~]#
用本机IP加映射到本机的端口号来到web上访问
2 优化haproxy镜像(使用ENV定义变量,用docker run -e 外面传递变量值)
由于镜像制作出来太大,还有最重要的一点还是我们调度的RS只能是我们写入镜像的两台RS的IP而不能根据随环境需求来进行改变,所以我们来对我们编写的Dockerfile进行一个优化改进;
2.1 改良后的一个整体结构
[root@docker ~]# tree /haproxy
/haproxy
├── Dockerfile
├── entrypoint.sh
└── files
├── haproxy-2.5.0.tar.gz
└── install.sh
1 directory, 4 files
[root@docker ~]#
2.2 Dockerfile的编写
[root@docker haproxy]# cat Dockerfile
FROM centos
LABEL MAINTAINER='xixi 1@2.com'
ENV PATH /usr/local/haproxy/sbin:$PATH
ENV RSs ""
ENV version 2.5.0
COPY files /usr/src/
COPY entrypoint.sh /
RUN ["/bin/bash","-c","/usr/src/install.sh"]
WORKDIR /usr/local/haproxy
EXPOSE 80 8189
ENTRYPOINT ["/entrypoint.sh"]
[root@docker haproxy]#
2.3 运行脚本的编写
[root@docker haproxy]# cat 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 -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 $(grep 'processor' /proc/cpuinfo |wc -l) \
TARGET=linux-glibc \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 \
USE_SYSTEMD=1 && \
make install PREFIX=/usr/local/haproxy
#cp haproxy /usr/src
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
yum -y remove gcc make
rm -rf /tmp/* /var/cache/*
[root@docker haproxy]#
2.4 运行脚本编写
//我们直接把配置文件需要修改的变量直接追加进配置文件,
//通过去启动容器时通过数据卷映射的方式来直接在本机上修改变量来指定变量的值
[root@docker haproxy]# cat 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
count=1
for rs_ip in $(cat /tmp/RSs.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg <<EOF
server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
haproxy -f /usr/local/haproxy/conf/haproxy.cfg
/bin/bash
[root@docker haproxy]#
2.5 通过映射在本机上控制变量的值
[root@docker ~]# cd /haproxy_config/
[root@docker haproxy_config]# ls
RSs.txt
[root@docker haproxy_config]# cat RSs.txt
192.168.10.2
192.168.10.3
192.168.10.4
192.168.10.5
[root@docker haproxy_config]#
2.6 运行容器
//构建镜像
[root@docker ~]# docker build -t haproxy:v0.4 /haproxy/
//生成容器
[root@docker haproxy]# docker run -itd --name haproxy -p 80:80 -p 8189:8189 -v /haproxy_config/:/tmp haproxy:v0.4
3ee518c24cdd988ce14d5e231380bd9623459dac55b4080912d079ba5c2b838d
[root@docker haproxy]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ee518c24cdd haproxy:v0.4 "/entrypoint.sh" 3 seconds ago Up 1 second 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy
[root@docker haproxy]# docker exec -it haproxy /bin/bash
[root@3ee518c24cdd haproxy]# 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:*
2.7 访问测试
//我们启动两个容器来作为我们的两个RS,再通过访问haproxy容器IP来查看效果
[root@docker ~]# docker run -idt --name web1 httpd
4d1c9fd60643d4159815e4a64a2dd418c2a5ab79c9e36d0508b8e7dce27f3d35
[root@docker ~]# docker run -idt --name web2 nginx
90f4d5158f807e39edda445fc2868a2737f639ec08e91e779a6421ab5e3c3fef
[root@docker ~]# curl 192.168.10.1
<html><body><h1>It works!</h1></body></html>
[root@docker ~]# curl 192.168.10.1
<!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>
[root@docker ~]#
浏览器是测试访问