Docker-compose搭建nginx负载均衡

Posted 9uhome

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker-compose搭建nginx负载均衡相关的知识,希望对你有一定的参考价值。

1.系统环境:

# cat /etc/redhat-release

Rocky Linux release 8.5 (Green Obsidian)

# docker-compose version

docker-compose version 1.29.2, build 5becea4c

docker-py version: 5.0.0

CPython version: 3.7.10

OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

2.配置文件:

# cat Dockerfile

FROM centos:latest

RUN mkdir -p /var/log/nginx

RUN touch /var/log/nginx/error.log

RUN yum install -y cmake automake autoconf libtool make pcre pcre-devel  \\

   openssl openssl-devel gd-devel gcc gcc-c++ wget curl && \\

   yum clean all && \\

   rm -rf /var/cache/yum/*

RUN wget https://nginx.org/download/nginx-1.16.1.tar.gz

RUN tar -zxvf nginx-1.16.1.tar.gz

WORKDIR nginx-1.16.1

RUN ./configure --prefix=/usr/local/nginx \\

               --with-http_image_filter_module \\

               --with-http_ssl_module \\

               --with-http_v2_module \\

               --with-http_stub_status_module \\

               --error-log-path=/var/log/nginx/error.log \\

               --http-log-path=/var/log/nginx/access.log \\

               --pid-path=/var/run/nginx/nginx.pid

RUN make && make install && \\

   rm -rf /usr/local/nginx/html/* && \\

   echo "hello everyone !" > /usr/local/nginx/html/index.html && \\

   rm -rf nginx*

ENV LOG_DIR /var/log/nginx

ENV PATH $PATH:/usr/local/nginx/sbin

EXPOSE 80

WORKDIR /usr/local/nginx/sbin

CMD ["nginx","-g","daemon off;"]

# cat nginx.conf

server

 listen  80 default_server;

 location /

   proxy_pass http://nginx:80;

 

# cat nginx.yml

version: "3.7"


services:

 nginx:

   build:

     context: /root/Docker

     dockerfile: /root/Docker/Dockerfile

   image: centos:latest

   ports:

     - 80

   volumes:

     - /root/Docker/itec:/usr/local/nginx/html

   networks:

     - bridge


 nginx-server:

   container_name: Nginx-Server

   image: nginx:stable-alpine

   ports:

     - 8080:80

   volumes:

     - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro

     - ./var/log/nginx:/var/log/nginx

   depends_on:

     - nginx

   networks:

     - bridge


networks:

 bridge:

3.启动容器

# docker-compose -f nginx.yml up -d --scale nginx=3

以上是关于Docker-compose搭建nginx负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

Docker系列- Docker-compose使用与负载均衡

Docker Compose 部署Nginx服务实现负载均衡

KVM虚拟化搭建nginx负载均衡 和lamp 架构(三 nginx负载均衡)

使用nginx做 tcp/udp 负载均衡

搭建Nginx+Tomcat 负载均衡

docker 搭建 nginx负载均衡