[root@docker files]# tree /haproxy/ /haproxy/ ├── Dockerfile └── files ├── haproxy-2.5.0.tar.gz ├── haproxy.cfg ├── install.sh └── start.sh 1 directory, 5 files [root@docker files]#2 Dockerfile编写
[root@docker haproxy]# cat Dockerfile # 基础镜像 FROM centos # 镜像维护者信息 LABEL MAINTAINER='xixi 1@2.com' #变量 ENV version 2.5.0 #传文件到容器 ADD files/haproxy-${version}.tar.gz /usr/src/ ADD files/install.sh /tmp/ ADD files/start.sh /tmp/ ADD files/haproxy.cfg /etc/haproxy/ #镜像 *** 作指令 RUN ["/bin/bash","-c","/tmp/install.sh"] # 工作中目录 WORKDIR /usr/local/haproxy #暴露端口 EXPOSE 80 8189 #启动命令 CMD ["/bin/bash","/tmp/start.sh"]3 执行脚本编写
root@docker 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-$(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 -j $(grep 'processor' /proc/cpuinfo |wc -l) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1 && make install PREFIX=/usr/local/haproxy cp haproxy /usr/src echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf yum -y remove gcc make rm -rf /var/cache/* rm -rf /usr/src/haproxy-${version} [root@docker haproxy]#4 启动脚本编写
[root@docker haproxy]# cat files/start.sh #!/bin/sh /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg /bin/bash [root@docker haproxy]#5 haproxy配置文件
[root@docker haproxy]# cat files/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 192.168.10.1:80 check inter 2000 fall 5 server web02 192.168.10.2:80 check inter 2000 fall 5 #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5 [root@docker haproxy]#6 构建镜像
// 创建haproxy镜像 [root@Docker ~]# docker build -t haproxy:v2.0 haproxy/ [root@docker haproxy]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE haproxy v2.0 0a48c089ff0b 53 minutes ago 410MB nginx latest f652ca386ed1 8 days ago 141MB busybox latest d23834f29b38 10 days ago 1.24MB httpd latest ad17c88403e2 3 weeks ago 143MB centos latest 5d0da3dc9764 2 months ago 231MB7 创建容器访问测试
[root@docker ~]# docker run -itd --name haproxy -P --rm haproxy:v2.0 bcfd0e7d144447a633f8ca4e0035aa040ef1e675970a25ae54542b727c9b1c66 [root@docker ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bcfd0e7d1444 haproxy:v2.0 "/bin/bash /tmp/star…" 18 seconds ago Up 17 seconds 0.0.0.0:49158->80/tcp, :::49158->80/tcp, 0.0.0.0:49157->8189/tcp, :::49157->8189/tcp haproxy [root@docker ~]# docker exec -it haproxy /bin/bash [root@bcfd0e7d1444 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:* ## 创建一个nginx和httpd容器用haproxy来做调度器 [root@docker ~]# docker run -itd --name nginx nginx [root@docker ~]# docker run -itd --name httpd httpd [root@docker ~]# docker images bcfd0e7d1444 haproxy:v2.0 "/bin/bash /tmp/star…" 18 seconds ago Up 17 seconds 0.0.0.0:49158->80/tcp, :::49158->80/tcp, 0.0.0.0:49157->8189/tcp, :::49157->8189/tcp haproxy d86bbe61db41 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 80/tcp a2 81142316f963 httpd "httpd-foreground" 2 hours ago Up 2 hours 80/tcp a1 [root@docker ~]# curl 192.168.10.3 It works! [root@docker ~]# curl 192.168.10.3Welcome to nginx! html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
[root@docker ~]#
用本机IP加映射到本机的端口号来到web上访问
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)