Dockerfile制作haproxy镜像
树形结构
[root@localhost ~]# tree haproxy/
haproxy/
├── Dockerfile
└── files
├── haproxy-2.4.0.tar.gz
├── install.sh
├── run_haproxy.sh
└── sysctl.conf
1 directory, 5 files
Dockerfile内容
[root@localhost ~]# cat /haproxy/Dockerfile
[root@localhost haproxy]# cat Dockerfile
FROM centos
LABEL MAINTAINER="syblyw0806 1234567890@qq.com"
ENV version 2.4.0
WORKDIR /usr/local/
ADD files/haproxy-${version}.tar.gz /usr/src
ADD files/install.sh /usr/local
ADD files/run_haproxy.sh /usr/local
ADD files/sysctl.conf /etc/
RUN ["/bin/bash","-c","/usr/local/install.sh"]
EXPOSE 80
CMD ["/usr/local/run_haproxy.sh"]
其他文件的内容
run_haproxy.sh
[root@localhost haproxy]# cat files/run_haproxy.sh
#!/bin/sh
cat > /etc/haproxy/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
by=1
for RS in $(cat /opt/RS.text);do
cat >> /etc/haproxy/haproxy.cfg << EOF
server web$by $RS:80 check inter 2000 fall 5
EOF
let by++
done
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
/bin/bash
install.sh
[root@localhost 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==2{print $2}' /etc/os-release).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/sbin/
yum -y remove make gcc gcc-c++
rm -rf /usr/src/haproxy-2.4.0
mkdir -p /etc/haproxy
sysctl.conf
[root@localhost files]# ls
haproxy-2.4.0.tar.gz install.sh sysctl.conf run_haproxy.sh
[root@localhost files]# cat sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
创建RS文件
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
containerd RS.text
[root@localhost opt]# cat RS.text
172.17.0.3
172.17.0.4
制作haproxy镜像
[root@localhost ~]# docker build -t haproxy:v2.0 /haproxy/
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v2.0 f1d1ab17759f 24 minutes ago 578MB
syblyw0806/httpd latest da89d66d3223 2 days ago 702MB
syblyw0806/httpd v1.0 da89d66d3223 2 days ago 702MB
syblyw0806/httpd v2.0 da89d66d3223 2 days ago 702MB
busybox latest ffe9d497c324 2 days ago 1.24MB
nginx latest f652ca386ed1 8 days ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
映射端口并进入容器查看端口是否起来
[root@localhost ~]# docker run -itd --name haproxy --rm -p 2021:80 -v /opt:/opt haproxy:2.0
4e962023cc112efae843e2df95a7938a36da5335eabc65f3b9597eae2bbb1ff4
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e962023cc11 haproxy:v2.0 "/usr/local/run_hapr…" 11 seconds ago Up 10 seconds 0.0.0.0:2021->80/tcp, :::2021->80/tcp haproxy
[root@localhost ~]# docker exec -it haproxy /bin/bash
[root@4e962023cc11 local]# ss -anlt
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:*
在已有的镜像中运行一个httpd镜像和nginx镜像
[root@localhost ~]# docker run -itd --name httpd syblyw0806/httpd:v2.0
378e9a5ac482ea38c7a5ffb0c3c612b79e37f849b82365cd7beb26346293adfb
[root@localhost ~]# docker run -itd --name nginx nginx
3244c338802f0fd2fc009c32a4244a835e886b6124fd9c062c2fc2d3f3654ed7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3244c338802f nginx "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 80/tcp nginx
378e9a5ac482 syblyw0806/httpd:v2.0 "/usr/local/apache/b…" 14 minutes ago Up 14 minutes 80/tcp httpd
4e962023cc11 haproxy:v2.0 "/usr/local/run_hapr…" 20 minutes ago Up 20 minutes 0.0.0.0:2021->80/tcp, :::2021->80/tcp haproxy
访问测试
ip+端口号(我映射的端口号是2021)