dockerfile构建haproxy镜像
结构
[root@docker haproxy]# tree
.
├── dockerfile
├── entrypoint.sh
└── file
├── entrypoint.sh
├── haproxy-2.4.0.tar.gz
└── install.sh
//创建dockerfile存放目录
[root@docker ~]# mkdir -p /haproxy/file
[root@docker ~]# cd /haproxy/
[root@docker haproxy]# mv /root/haproxy-2.4.0.tar.gz ./file/
entrypoint.sh
[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
EOF
count=1
for rs_ip in $RS;do
cat >> /usr/local/haproxy/conf/haproxy.cfg <<EOF
server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
/usr/local/haproxy/sbin/haproxy -db -f /usr/local/haproxy/conf/haproxy.cfg
/bin/bash
dockerfile
[root@docker haproxy]# cat dockerfile
FROM alpine
LABEL MAINTAINER='487021296@qq.com'
ENV PATH /usr/local/haproxy/sbin:$PATH
ENV version 2.4.0
ENV RS ""
ADD file /tmp
COPY entrypoint.sh /
RUN ["/bin/sh","-c","/tmp/install.sh"]
EXPOSE 80 8189
ENTRYPOINT ["/entrypoint.sh"]
安装脚本
[root@docker haproxy]# cat file/install.sh
#! /bin/sh
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories &&\
apk update && \
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 && \
addgroup haproxy
adduser -S -H -s /sbin/nologin haproxy && \
cd /tmp
tar xf haproxy-${version}.tar.gz
cd haproxy-${version}
make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_SYSTEMD=1
make install PREFIX=/usr/local/haproxy
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 /tmp/* /var/cache/*
yum -y remove gcc make gcc-c++
创建镜像
[root@docker haproxy]# docker build -t haproxy:v0.1 /haproxy
[root@docker haproxy]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v0.1 041d53695f6c 42 seconds ago 77MB
[root@docker haproxy]# docker run -d --name haproxy -p 80:80 -e "RS=192.168.66.3 192.168.66.4" haproxy:v0.1
bf194ecec92141c7372c3bdd3c72de1f7130b74874fa2d02a9de756a41f0d47b
[root@docker haproxy]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
428acb38032a haproxy:v0.1 "/entrypoint.sh" 4 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 8189/tcp haproxy
创建一个nginx容器
[root@docker haproxy]# docker run -d --rm --name web1 linlusama/centos-nginx:v1.20.1
[root@docker haproxy]# docker exec -it web1 /bin/bash
[root@ce4dbe2d8db3 /]# cat /usr/local/nginx/html/index.html
我是徐猛他爹
创建一个httpd容器
[root@docker ~]# docker exec -it web2 /bin/bash
[root@36fc60453e4f src]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>It works!</h1></body></html>
访问
[root@docker haproxy]# for i in $(seq 10);do curl 192.168.200.150;done
我是徐猛他爹
<html><body><h1>It works!</h1></body></html>
我是徐猛他爹
<html><body><h1>It works!</h1></body></html>
我是徐猛他爹
<html><body><h1>It works!</h1></body></html>
我是徐猛他爹
<html><body><h1>It works!</h1></body></html>
我是徐猛他爹
<html><body><h1>It works!</h1></body></html>