IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> dockerfile编写haproxy镜像 -> 正文阅读

[系统运维]dockerfile编写haproxy镜像

dockerfile结构

[root@localhost ~]# tree haproxy/
haproxy/
├── Dockerfile
└── files
    ├── entrypoint.sh
    ├── haproxy-1.7.8.tar.gz
    ├── haproxy.cfg
    └── install.sh

1 directory, 5 files

dockerfile

[root@localhost ~]# cat haproxy/Dockerfile 
FROM centos

ENV version 1.7.8
ENV PATH /usr/local/harpoxy/sbin:$PATH

ADD files/haproxy-${version}.tar.gz /usr/src/
ADD files/install.sh /tmp/
ADD files/entrypoint.sh /

EXPOSE 8189 80

RUN ["/bin/bash","-c","/tmp/install.sh"]

WORKDIR /usr/local/haproxy/

CMD ["/entrypoint.sh"]

entrypoint.sh

[root@localhost ~]# cat haproxy/files/entrypoint.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
Snu=1
for rs_ip in $(cat /tmp/RSe.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg <<EOF
    server web$Snu $rs_ip:80 check inter 2000 fall 5
EOF
let Snu++
done

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

/bin/bash

install.sh

[root@localhost ~]# 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

mkdir -p /usr/local/haproxy/conf
yum -y remove make gcc gcc-c++ && \
rm -rf /tmp/* /var/cache/*

?做镜像

[root@localhost ~]# docker build -t yangyonghu/haproxy:v0.3 haproxy/
Sending build context to Docker daemon  1.754MB
Step 1/10 : FROM centos
 ---> 5d0da3dc9764
Step 2/10 : ENV version 1.7.8
 ---> Running in affae3ffac91
Removing intermediate container affae3ffac91
 ---> 139159f97ad8
Step 3/10 : ENV PATH /usr/local/harpoxy/sbin:$PATH
 ---> Running in ca335770ef74
Removing intermediate container ca335770ef74
 ---> d2d16324b306
Step 4/10 : ADD files/haproxy-${version}.tar.gz /usr/src/
 ---> 8300658bbf39
Step 5/10 : ADD files/install.sh /tmp/
 ---> 31bf0c1dd3e3
Step 6/10 : ADD files/entrypoint.sh /
 ---> 7b6b4d3400f6
Step 7/10 : EXPOSE 8189 80
 ---> Running in 5858d95a4a58
Removing intermediate container 5858d95a4a58
 ---> 722c7a2eaecb
Step 8/10 : RUN ["/bin/bash","-c","/tmp/install.sh"]
 ---> b2e87962da48
Step 9/10 : WORKDIR /usr/local/haproxy/
 ---> Running in b22715a19eee
Removing intermediate container b22715a19eee
 ---> d6fa8993457f
Step 10/10 : CMD ["entrypoint.sh"]
 ---> Running in 5096deab9936
Removing intermediate container 5096deab9936
 ---> 2d2ae019decf
Successfully built 2d2ae019decf
Successfully tagged yangyonghu/haproxy:v0.3
[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      2d2ae019decf   2 minutes ago   240MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB

创建一个容器来运行haproxy镜像

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   5 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB
[root@localhost ~]# docker run -dit --name haproxy -v /config/:/tmp -p 80:80 -p 8189:8189 d75fa67c9f17
ded46827fd7c4f590a80f52131065d8dc0b8bd02ddc90175dfabe1c81d59cf09
[root@localhost ~]#  docker exec -it haproxy /bin/bash
[root@ded46827fd7c haproxy]# 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@ded46827fd7c 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
15: eth0@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:01 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.1/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever

另起终端查看主机上的端口号

[root@localhost ~]# ss -antl
State               Recv-Q              Send-Q                           Local Address:Port                             Peer Address:Port              
LISTEN              0                   128                                    0.0.0.0:8189                                  0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:111                                   0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:80                                    0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:22                                    0.0.0.0:*                 
LISTEN              0                   128                                       [::]:8189                                     [::]:*                 
LISTEN              0                   128                                       [::]:111                                      [::]:*                 
LISTEN              0                   128                                       [::]:80                                       [::]:*                 
LISTEN              0                   128                                       [::]:22                                       [::]:*         

创建两个新的容器使用apache镜像来测试haproxy

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   9 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB
[root@localhost ~]# docker run -dit --name httpd --rm 677843bb4e15
46a043a66ce7353e7c58d207ea6f9c9d8f4e432763afc05de5c707ff340e991d
[root@localhost ~]# docker exec -it httpd /bin/bash
[root@46a043a66ce7 src]# 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@46a043a66ce7 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
17: eth0@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.2/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
[root@46a043a66ce7 src]# vi /usr/local/apache/htdocs/index.html
[root@46a043a66ce7 src]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>RS1</h1></body></html>
[root@46a043a66ce7 src]# 

第二个

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   13 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago       703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago       550MB
busybox              latest    d23834f29b38   12 days ago      1.24MB
centos               latest    5d0da3dc9764   2 months ago     231MB
[root@localhost ~]# docker run -dit --name apache --rm 677843bb4e15
de053e2dc3055614936ccdbd61158fdea33818e44a064e00e01d3dbc0c3c938f
[root@localhost ~]# docker exec -it apache /bin/bash
[root@de053e2dc305 src]# 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@de053e2dc305 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
19: eth0@if20: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.3/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever

[root@de053e2dc305 src]# vi /usr/local/apache/htdocs/index.html
[root@de053e2dc305 src]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>RS2</h1></body></html>

访问页面做测试

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-12-13 13:16:28  更:2021-12-13 13:17:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 5:50:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码