使用dockerfile编写haproxy镜像 目录结构
[root@docker /]# tree /haproxy/
/haproxy/
├── dockerfile
└── files
├── haproxy-2.4.0.tar.gz
├── haproxy-2.5.0.tar.gz
├── haproxy.cfg
├── install.sh
└── start.sh
install.sh
[root@docker /]# cat haproxy/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 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
start.sh
[root@docker /]# cat /haproxy/files/start.sh
#!/bin/sh
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
/bin/bash
haproxy.cfg
cat /etc/haproxy/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.2:80 check inter 2000 fall 5
server web0w 172.17.0.4:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
dockerfile
[root@docker /]# cat /haproxy/dockerfile
FROM centos
LABEL MAINTAINER='aoman xx@163.com'
ENV version 2.5.0
ADD files/haproxy-${version}.tar.gz /usr/src/
ADD files/install.sh /tmp/
ADD files/haproxy.cfg /etc/haproxy/haproxy.cfg
ADD files/start.sh /start.sh
RUN ["/bin/bash","-c","/tmp/install.sh"]
WORKDIR /usr/local/haproxy
EXPOSE 8189 80
CMD ["/bin/bash","/start.sh"]
生成镜像
[root@docker ~]# docker build -t xm17671855780/haproxy:v0.1 /haproxy/
Sending build context to Docker daemon 7.406MB
Step 1/11 : FROM centos
---> 5d0da3dc9764
Step 2/11 : LABEL MAINTAINER='aoman xx@163.com'
---> Using cache
---> 900e2c5549fa
Step 3/11 : ENV version 2.5.0
---> Using cache
---> 8642325d3599
Step 4/11 : ADD files/haproxy-${version}.tar.gz /usr/src/
---> Using cache
---> b97d40827572
Step 5/11 : ADD files/install.sh /tmp/
---> 295c8364ca80
Step 6/11 : ADD files/haproxy.cfg /etc/haproxy/haproxy.cfg
---> 3f51bbf06e33
Step 7/11 : ADD files/start.sh /start.sh
---> 5e6d3a2685d2
Step 8/11 : RUN ["/bin/bash","-c","/tmp/install.sh"]
---> Running in b4f6c76e47ef
........省略n行
Removing intermediate container b4f6c76e47ef
---> 086d5f2fc88d
Step 9/11 : WORKDIR /usr/local/haproxy
---> Running in 709bf26bc926
Removing intermediate container 709bf26bc926
---> 403b776ee89a
Step 10/11 : EXPOSE 80 8189
---> Running in 53925d4a4cf3
Removing intermediate container 53925d4a4cf3
---> cfc7eab2f7f5
Step 11/11 : CMD ["/bin/bash","/start.sh"]
---> Running in f57d11c067b2
Removing intermediate container f57d11c067b2
---> a4baafa95841
Successfully built a4baafa95841
Successfully tagged xm17671855780/haproxy:v0.1
使用新创建的镜像来运行一个haproxy容器,并映射80端口
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xm17671855780/haproxy v0.1 a4baafa95841 11 minutes ago 489MB
xm17671855780/file_httpd v0.2 a563454c4a5f 2 days ago 701MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker ~]# docker run -dit --name haproxy -p 80:80 a4baafa95841
5d6cb0e1b29ecb3bdc1e3d9f60536778e2714ad02861ed3ea6781e04d4648202
[root@docker ~]# docker exec -it haproxy /bin/bash
[root@5d6cb0e1b29e haproxy]# ss -anlt
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:*
[root@5d6cb0e1b29e haproxy]# 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
50: eth0@if51: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.5/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
使用httpd镜像运行两个httpd
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xm17671855780/haproxy v0.1 a4baafa95841 11 minutes ago 489MB
xm17671855780/file_httpd v0.2 a563454c4a5f 2 days ago 701MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker files]# docker run -dit --name httpd1 a563454c4a5f
[root@docker ~]# docker exec -it httpd1 /bin/bash
[root@25932175f666 src]# 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
44: eth0@if45: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@25932175f666 src]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>It works!</h1></body></html>
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xm17671855780/haproxy v0.1 a4baafa95841 11 minutes ago 489MB
xm17671855780/file_httpd v0.2 a563454c4a5f 2 days ago 701MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@docker files]# docker run -dit --name httpd2 a563454c4a5f
92e0ca6adc15eacf3b7a1aef54bfd4bd79b7c7e148df7a0f534dc224169ba09f
[root@docker files]# docker exec -it httpd2 /bin/bash
[root@92e0ca6adc15 src]# ls
apr-1.7.0 apr-util-1.6.1 debug httpd-2.4.48 kernels
[root@92e0ca6adc15 src]# cd /usr/local/apache/htdocs/
[root@92e0ca6adc15 htdocs]# ls
index.html
[root@92e0ca6adc15 htdocs]# vim index.html
bash: vim: command not found
[root@92e0ca6adc15 htdocs]# echo "web test!" > index.html
[root@92e0ca6adc15 htdocs]# cat index.html
web test!
[root@92e0ca6adc15 htdocs]# 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
46: eth0@if47: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
浏览器访问
|