dockerfile制作haproxy镜像
树结构
[root@server1 Dockerfile]# tree haproxy/
haproxy/
|-- Dockerfile
|-- config #创建这个目录把bs文件映射到容器里
| `-- bs.txt
`-- files
|-- ex.sh
|-- haproxy-2.4.0.tar.gz
|-- haproxy.cfg
`-- install.sh
编写Dockerfile
[root@server1 haproxy]# cat Dockerfile
FROM centos:latest
ADD files/haproxy-2.4.0.tar.gz /usr/src
COPY files/install.sh /install.sh
COPY files/ex.sh /ex.sh
ENV PATH /usr/local/haproxy/sbin:$PATH
ENV bs ""
RUN ["/bin/bash","-c","/install.sh"]
EXPOSE 8189 80
ENTRYPOINT ["/ex.sh"]
编写install.sh
[root@server1 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-8.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-2.4.0
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 /usr/local/haproxy/conf
rm -rf /usr/src
编写ex.sh
[root@server1 files]# cat ex.sh
#!/bin/bash
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 /config/bs.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 -db #启动程序
添加IP地址 bs.txt
[root@server1 config]# cat bs.txt
172.17.0.2
172.17.0.3
172.17.0.4
172.17.0.5
制作镜像
[root@server1 Dockerfile]# docker build -t haproxy:v0.4 haproxy/
创建容器
[root@server1 Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v0.4 c28b6e001795 2 minutes ago 506MB
nginx latest f652ca386ed1 10 days ago 141MB
centos centos8.4.2105 5d0da3dc9764 2 months ago 231MB
centos latest 5had0da3dc9764 2 months ago 231MB
[root@server1 Dockerfile]# docker run -d -p 80:80 -p 8189:8189 -v /opt/Dockerfile/haproxy/config:/config --name haproxy1 haproxy:v0.4
724d07f3def1ae10e749a9036a31658c99af6319ecc03a8d117b8d6f69eca782
[root@server1 Dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
724d07f3def1 haproxy:v0.4 "/ex.sh" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy1
[root@server1 Dockerfile]# docker exec -it 724d07f3def1 /bin/bash
[root@724d07f3def1 /]# ls
bin ex.sh lib64 opt sbin usr
config home lost+found proc srv var
dev install.sh media root sys
etc lib mnt run tmp
[root@724d07f3def1 /]# cd config/ #文件映射过来了
[root@724d07f3def1 config]# ls
bs.txt
在宿主机上查看映射端口
[root@server1 haproxy]# docker port 724d07f3def1
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
8189/tcp -> 0.0.0.0:8189
8189/tcp -> :::8189
创建两台以上nginx容器
[root@server1 Dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
724d07f3def1 haproxy:v0.4 "/ex.sh" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy1
8a2c8656efc9 centos:latest "/bin/bash" 3 hours ago Up About an hour web2
92c1c2a731e4 centos:latest "/bin/bash" 3 hours ago Up About an hour web1
进入web1容器
[root@92c1c2a731e4 /]# nginx
[root@92c1c2a731e4 /]# 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
24: eth0@if25: <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
[root@92c1c2a731e4 /]# which nginx
/usr/sbin/nginx
[root@92c1c2a731e4 /]# 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 [::]:80 [::]:*
[root@92c1c2a731e4 /]# curl 172.17.0.4
YY!
进入web2容器启动服务
[root@8a2c8656efc9 /]# nginx
[root@8a2c8656efc9 /]# 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
22: eth0@if23: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@8a2c8656efc9 /]# which nginx
/usr/sbin/nginx
[root@8a2c8656efc9 /]# 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 [::]:80 [::]:* httpd2
[root@8a2c8656efc9 /]# curl 172.17.0.3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Page for the Nginx HTTP Server on Red Hat Enterprise Linux</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
/*<![CDATA[*/
body {
background-color: #fff;
color: #000;
font-size: 0.9em;
font-family: sans-serif,helvetica;
margin: 0;
padding: 0;
}
:link {
color: #c00;
}
:visited {
color: #c00;
}
a:hover {
color: #f50;
}
h1 {
text-align: center;
margin: 0;
padding: 0.6em 2em 0.4em;
background-color: #900;
color: #fff;
font-weight: normal;
font-size: 1.75em;
border-bottom: 2px solid #000;
}
h1 strong {
font-weight: bold;
font-size: 1.5em;
}
h2 {
text-align: center;
background-color: #900;
font-size: 1.1em;
font-weight: bold;
color: #fff;
margin: 0;
padding: 0.5em;
border-bottom: 2px solid #000;
}
hr {
display: none;
}
.content {
padding: 1em 5em;
}
.alert {
border: 2px solid #000;
}
img {
border: 2px solid #fff;
padding: 2px;
margin: 2px;
}
a:hover img {
border: 2px solid #294172;
}
.logos {
margin: 1em;
text-align: center;
}
/*]]>*/
</style>
</head>
<body>
<h1>Welcome to <strong>nginx</strong> on Red Hat Enterprise Linux!</h1>
<div class="content">
<p>This page is used to test the proper operation of the
<strong>nginx</strong> HTTP server after it has been
installed. If you can read this page, it means that the
web server installed at this site is working
properly.</p>
<div class="alert">
<h2>Website Administrator</h2>
<div class="content">
<p>This is the default <tt>index.html</tt> page that
is distributed with <strong>nginx</strong> on
Red Hat Enterprise Linux. It is located in
<tt>/usr/share/nginx/html</tt>.</p>
<p>You should now put your content in a location of
your choice and edit the <tt>root</tt> configuration
directive in the <strong>nginx</strong>
configuration file
<tt>/etc/nginx/nginx.conf</tt>.</p>
<p>For information on Red Hat Enterprise Linux, please visit the <a href="http://www.redhat.com/">Red Hat, Inc. website</a>. The documentation for Red Hat Enterprise Linux is <a href="http://www.redhat.com/docs/manuals/enterprise/">available on the Red Hat, Inc. website</a>.</p>
</div>
</div>
<div class="logos">
<a href="http://nginx.net/"><img
src="nginx-logo.png"
alt="[ Powered by nginx ]"
width="121" height="32" /></a>
<a href="http://www.redhat.com/"><img
src="poweredby.png"
alt="[ Powered by Red Hat Enterprise Linux ]"
width="88" height="31" /></a>
</div>
</div>
</body>
</html>