docker - compose 와 consul - template 를 사용 하여 nginx 자동 발견 자동 등록 - 마이크로 서비스 관리

docker 는 consul - template 와 결합 하여 nginx 의 서비스 발견 을 실현 합 니 다.
docker-compose.yml
version: '3'
services:    
  go-nginx:
    build: 
      context: ./consul
      dockerfile: Dockerfile
    restart: always
    depends_on:
        - consul4
    ports: 
      - "81:80"    
    volumes:
      - ./nginx/logs:/wwwlogs
      - ./nginx/vhost:/etc/nginx/conf.d
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./consul:/consul
      - ../apidoc:/www/apidoc
    ulimits:
      nproc: 60000
      nofile:
        soft: 60000
        hard: 60000   
    networks:
      - gonetwork    
    tty: true
    command:
    - /bin/sh
    - -c
    - |
      nginx && ./consul-template -consul-addr=consul4:8500 -template="./nginx.tpl:/etc/nginx/conf.d/site.conf:nginx -s reload" 
  go-redis:
    image: redis:5.0.5
    volumes:
      - ./redis/data:/data  
    ports:
      - "6379:6379"
    networks:
      - gonetwork    
    tty: true 
    #command: redis-server --requirepass aaaaaaaa
  consul1:
    image: consul
    container_name: node1
    command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    networks:
      - gonetwork

  consul2:
    image: consul
    container_name: node2
    command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    depends_on:
        - consul1
    networks:
      - gonetwork

  consul3:
    image: consul
    container_name: node3
    command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    depends_on:
        - consul1
    networks:
      - gonetwork

  consul4:
    image: consul
    container_name: node4
    command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui 
    ports:
      - 8500:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - gonetwork    
networks:
  gonetwork:
    driver: bridge

consul - template nginx 템 플 릿 생 성
{{range services}} {{$name := .Name}} {{$service := service .Name}}
upstream {{$name}} {
  zone upstream-{{$name}} 64k;
  {{range $service}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1;
  {{else}}server 127.0.0.1:80; # force a 502{{end}}
} {{end}}

server {
    listen 80;	
	charset utf-8;
	default_type application/json;
    server_name  api.fengqiaoju.com;
	
	location /apidoc {
		root /www ; 
	}
	
	{{range services}} {{$name := .Name}}
    location /{{$name}} {
        proxy_pass  http://{{$name}};
 
        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
   {{end}}
}

당 풍 교 대중 호 에 더 관심 을 가 져 주세요.

좋은 웹페이지 즐겨찾기