Dockerfile制作haproxy镜像
结构目录
[root@localhost ~]# tree haproxy/
haproxy/
├── Dockerfile
└── files
├── haproxy-2.4.0.tar.gz
├── haproxy.cfg
├── install.sh
├── run_haproxy.sh
└── sysctl.conf
1 directory, 6 files
[root@localhost ~]#
Dockerfile文件内容
[root@localhost ~]# cd haproxy
[root@localhost haproxy]# cat Dockerfile
FROM centos
ENV version 2.4.0
WORKDIR /usr/local
ADD files/haproxy${version}.tar.gz /usr/local/
ADD files/haproxy.sh /usr/local
ADD files/haproxy.cfg /etc/haproxy/
ADD files/run_haproxy.sh /usr/local
ADD files/sysctl.conf /etc/
RUN ["/bin/bash","-c","/usr/local/haproxy.sh"]
EXPOSE 80
CMD ["/usr/local/run_haproxy.sh"]
查看结构目录内容
[root@localhost ~]# cd haproxy/files/
[root@localhost files]# ls
haproxy-2.4.0.tar.gz install.sh sysctl.conf
haproxy.cfg run_haproxy.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==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
[root@localhost files]#
[root@localhost files]# ls
haproxy-2.4.0.tar.gz install.sh sysctl.conf
haproxy.cfg 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
[root@localhost files]#
[root@localhost files]# ls
haproxy-2.4.0.tar.gz install.sh sysctl.conf
haproxy.cfg run_haproxy.sh
[root@localhost files]# cat 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 172.17.0.3:80 check inter 2000 fall 5
server web02 172.17.0.4:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
[root@localhost files]#
[root@localhost files]# cat run_haproxy.sh
#!/bin/sh
haproxy -f /etc/haproxy/haproxy.cfg
/bin/bash
[root@localhost files]#
制作haproxy镜像
[root@localhost haproxy]# docker build -t haproxy:v0.1 .
[root@localhost haproxy]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v0.1 833130f07d74 4 seconds ago 578MB
1225514226/httpd v0.4 37f8ea813545 47 hours ago 702MB
1225514226/httpd v0.2 026478daf0c7 3 days ago 702MB
1225514226/nginx v0.1 c98f0d7db627 6 days ago 579MB
busybox latest d23834f29b38 10 days ago 1.24MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost haproxy]#
运行haproxy容器并映射端口
[root@localhost haproxy]# docker run -itd --name haproxy -p 1111:80 833130f07d74
03df66f57ac99f21891c5c14e992bed7e68da2869611a86e089c4b95dd794053
[root@localhost haproxy]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03df66f57ac9 833130f07d74 "/usr/local/run_hapr…" 4 seconds ago Up 3 seconds 0.0.0.0:1111->80/tcp, :::1111->80/tcp haproxy
[root@localhost haproxy]#
[root@localhost haproxy]# docker exec -it haproxy /bin/bash
[root@03df66f57ac9 local]# 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@03df66f57ac9 local]#
运行一个httpd镜像和nginx镜像
[root@localhost ~]# docker run -d --name httpd 1225514226/httpd:v0.4
636b41c7296b518a13b23b18b03a82887e2fa355b69e0a442e70a85f98d158b7
[root@localhost ~]# docker run -d --name nginx 1225514226/nginx:v0.1
199524481ed4e279bc2cc605b305be71bb7828ce4568efcaace59e149c8248ac
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
199524481ed4 c98f0d7db627 "bin/bash start.sh" About a minute ago Exited (0) About a minute ago nginx
636b41c7296b 37f8ea813545 "/usr/local/apache/b…" About a minute ago Up About a minute 80/tcp, 443/tcp httpd
03df66f57ac9 833130f07d74 "/usr/local/run_hapr…" 6 minutes ago Up 6 minutes 0.0.0.0:1111->80/tcp, :::1111->80/tcp haproxy
[root@localhost ~]#
访问测试