文章目錄
- 創建nginx、httpd容器
- 目錄結構
- Dockerfile檔案
- 安裝腳本
- 啟動服務腳本
- 撰寫組態檔
- 構建haproxy鏡像
- 創建haproxy容器
- 頁面訪問
創建nginx、httpd容器
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd v0.1 3709a35b5387 15 minutes ago 423MB
nginx v0.1 45d2af2e6e0f 27 minutes ago 549MB
centos latest 5d0da3dc9764 2 months ago 231MB
// nginx
[root@localhost ~]# docker run -itd --name nginx nginx:v0.1 /start.sh
31c71361384a121630a4d14b525a0adac65c9c1694a5a0892a0140aad36802a3
[root@localhost ~]# docker exec -it nginx /bin/bash
[root@31c71361384a /]# 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:*
[root@31c71361384a /]# 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
35: eth0@if36: <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
// httpd
[root@localhost opt]# docker run -d --name httpd httpd:v0.1
27e9997db224f1074f2d686cedb7e8be0a53af600b4b833afbf94bad9f0fc16a
[root@localhost opt]# docker exec -it httpd /bin/bash
[root@27e9997db224 apache]# 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:*
[root@27e9997db224 apache]# 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
29: eth0@if30: <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@localhost ~]# tree haproxy/
haproxy/
|-- Dockerfile
`-- files
|-- haproxy-2.4.0.tar.gz
|-- haproxy-2.5.0.tar.gz
|-- haproxy.cfg
|-- install.sh
`-- start.sh
1 directory, 6 files
Dockerfile檔案
[root@localhost ~]# cat haproxy/Dockerfile
// 基礎鏡像
FROM centos
// 作者資訊
LABEL MAINTAINER “gaofan1225 123@qq.com”
// 環境變數
ENV haproxy_version 2.5.0
// 傳輸檔案
ADD files/haproxy-${haproxy_version}.tar.gz /usr/src
ADD files/haproxy.cfg /usr/local/haproxy/conf/haproxy.cfg
ADD files/start.sh /scripts/
ADD files/install.sh /scripts/
// 安裝
RUN ["/bin/bash","-c","/scripts/install.sh"]
// 暴露埠
EXPOSE 80 8189
// 啟動命令
CMD ["/scripts/start.sh"]
安裝腳本
[root@localhost ~]# cd haproxy/files/
[root@localhost files]# ls
haproxy-2.5.0.tar.gz
[root@localhost files]# touch install.sh
[root@localhost files]# chmod +x install.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==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 clean all && yum makecache
yum -y install make gcc gcc-c++ pcre-devel bzip2-devel openssl-devel systemd-devel
useradd -r -M -s /sbin/nologin haproxy
cd /usr/src/haproxy-${haproxy_version}
make clean && \
make -j $(nproc) \
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
rm -rf /usr/src/haproxy-${haproxy_version/ /var/cache/*
yum -y remove make gcc gcc-c++
啟動服務腳本
[root@localhost files]# touch start.sh
[root@localhost files]# chmod +x start.sh
[root@localhost files]# cat start.sh
#!/bin/bash
haproxy -f /usr/local/haproxy/conf/haproxy.cfg
/bin/bash
撰寫組態檔
[root@localhost files]# vim 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 web1 172.17.0.2:80 check inter 2000 fall 5
server web2 172.17.0.3:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
構建haproxy鏡像
[root@localhost ~]# docker build -t haproxy:v0.1 haproxy/
Successfully built afaead465206
Successfully tagged haproxy:v0.1
創建haproxy容器
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v0.1 afaead465206 4 minutes ago 412MB
httpd v0.1 3709a35b5387 15 minutes ago 423MB
nginx v0.1 45d2af2e6e0f 27 minutes ago 549MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -itd --name haproxy -p 80:80 haproxy:v0.1
35179537b8345e14a6d2abb61ad6b1f7655a0e26d04963d51ec39c1eeed17781
[root@localhost ~]# docker exec -it haproxy /bin/bash
[root@35179537b834 /]# 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@35179537b834 /]# 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
41: eth0@if42: <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
頁面訪問


轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/379443.html
標籤:其他
上一篇:兩臺電腦通過網線共享檔案
