Nginx Load Balancing and using with Docker

· 13 mins read

Load Balancing Architecture Diagram

11

Start

main.go Get environment variables to run the server

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
)

func main() {
	r := gin.Default()
	port := os.Getenv("env_port")
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello world "+port)
	})

	r.Run(":" + port)
}

dockerfile Package a Golang project as an image

FROM golang:alpine as builder

ENV GO111MODULE=on

WORKDIR /build

COPY . .

RUN go mod init hello && go get
RUN go build -o app .

FROM alpine

COPY --from=builder /build/app /

ENTRYPOINT [ "/app" ]

docker-compose.yml

version: "3"
services:
  nginx-load-balancing:
    image: nginx:alpine
    container_name: nginx-load-balancing
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - app-server1
      - app-server2
      - app-server3
  app-server1:
    build:
      context: ./
      dockerfile: dockerfile
    container_name: app-server1
    environment:
      env_port: 10000
  app-server2:
    build:
      context: ./
      dockerfile: dockerfile
    container_name: app-server2
    environment:
      env_port: 10001
  app-server3:
    build:
      context: ./
      dockerfile: dockerfile
    container_name: app-server3
    environment:
      env_port: 10002
networks:
  default:
    driver: bridge

nginx.conf Configuring Load Balancing

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

   	#include /etc/nginx/conf.d/*.conf;
	# docker容器在一个网络内可以使用服务名通信
	# 负载均衡
    upstream backend {
        server app-server1:10000;
        server app-server2:10001;
        server app-server3:10002;
    }

    server {
        listen 80;
        server_name localhost;
        access_log /var/log/nginx/access.log;
		# 反向代理
        location / {
            proxy_pass http://backend/;
        }
    }
    
}

run

> ls
docker-compose.yml  dockerfile  main.go  nginx.conf

> docker-compose up -d
 Network root_default            Created                                                                         0.1s 
 Container app-server3           Started                                                                         0.8s 
 Container app-server1           Started                                                                         0.8s 
 Container app-server2           Started                                                                         0.7s 
 Container nginx-load-balancing  Started 

录制gif工具