로드밸런싱 Docker와 Nginx로 설정하자

00_8_3·2021년 4월 18일
0
post-thumbnail

로드밸런싱 Docker와 Nginx로

conf 및 yml

도커Nginx 로 로드밸런싱을 하는 중
conf 설정을 위한 옵션을 잘 모르기도 하고
에러가 발생하여 설정을 해본다

Nginx.conf 설정

user nginx;

worker_processes 1;

pid /var/run/nginx.pid;

events {
  worker_connections 1024;
}
http {
    include \etc\nginx\conf.d\*.conf;
    client_max_body_size 0;
    upstream backend {
        server stackfolio-server_server_1/:3000;
        server stackfolio-server_server_2/:3000;
        server stackfolio-server_server_3/:3000;
        server stackfolio-server_server_4/:3000;
        
    }

    server {
        listen 80;
                  
            proxy_pass http:\\backend;

        location / {

        }
    }
}

docker-compose 설정

설정을 완료하고
docker-compose up 명령어로 빌드를 하는데
자꾸 아래의 오류로 pg 컨테이너가 종료가 되었다.

pg exited with code 1
pg        | The files belonging to this database system will be owned by user "postgres".
pg        | This user must also own the server process.
pg        | 
pg        | The database cluster will be initialized with locale "en_US.utf8".
pg        | The default database encoding has accordingly been set to "UTF8".
pg        | The default text search configuration will be set to "english".
pg        | 
pg        | Data page checksums are disabled.
pg        | 
pg        | fixing permissions on existing directory /var/lib/postgresql/data ... ok     
pg        | creating subdirectories ... ok
pg        | selecting dynamic shared memory implementation ... posix
pg        | selecting default max_connections ... 100
pg        | selecting default shared_buffers ... 128MB
pg        | selecting default time zone ... UTC
pg        | creating configuration files ... ok
pg        | running bootstrap script ... 2021-04-18 15:29:33.088 UTC [29] FATAL:  could not create file "base/1/1249_fsm": I/O error
pg        | 2021-04-18 15:29:33.088 UTC [29] PANIC:  cannot abort transaction 1, it was already committed
pg        | child process was terminated by signal 6: Aborted
pg        | initdb: removing contents of data directory "/var/lib/postgresql/data"

top-level Volume 이란?

위의 문제는 top-level에 컨테이너 내 볼륨을 마운트하지 않아 발생한 것이었다.

version: '3.7'

services:
  pg:
    container_name: pg
    image: postgres:13.2-alpine
    restart: always
    environment:
      - POSTGRES_USER=dddf
      - POSTGRES_PASSWORD=dddddd
      - POSTGRES_DB=DBDBDB
    volumes:
      - db:/var/lib/postgresql/data
    ports:
      - '5432:5432'
    healthcheck:
      test: pg_isready -h 127.0.0.1

volumes:
  db:

구글링을 해보니 top-level 볼륨을 사용하면 조금더 복잡한 옵션을 사용 할 수 있나보다.

network 옵션도 top-level에서 비슷하게 작동

Changed in version 3 file format.
The top-level volumes key defines a named volume and references it from each service’s volumes list. This replaces volumes_from in earlier versions of the Compose file format.
MS 도커 볼륨 설명

the top-level volumes section allows for more detailed configuration of each volume that is defined; for example if you were using some other volume driver you could provide options for it. then in each service, you can reference the volume by name without having to specify the options again (some of which are not even allowed in a definition with the service block, as i recall). this way if you have multiple services using the volume you only have to change the definition of it in one place.
reddit 질문글

depends_on 옵션이란?

web:
 depends_on:
   - pg

위와 같은 옵션은 pgweb이 시작하기 전에 실행 된다는 것을 뜻 한다.

하지만 이것은 webpg시작상태가 되기 전까지 기다린다는 것은 아니다!!

문서에 따르면 순서를 제어하기 위한 최선의 방법으로는

wait-for-it dockerize sh-compatible wait-for RelayAndContainers
와 같은 툴을 사용하는 것이라고 한다.

The best solution is to perform this check in your application code, both at startup and whenever a connection is lost for any reason. However, if you don’t need this level of resilience, you can work around the problem with a wrapper script:
Use a tool such as wait-for-it, dockerize, sh-compatible wait-for, or RelayAndContainers template. These are small wrapper scripts which you can include in your application’s image to poll a given host and port until it’s accepting TCP connections.

MS Depends_on옵션

compose의 순서제어

links옵션은 Docker의 레거시라고 하며
결국에 삭제될 것이니 사용을 권장하지 않는다고 한다!

대신 사용자가 정의한 networks를 사용하자

추가

위와 같은 설정을 통해 ec2 우분투에 배포 후
도커 스케일링을 한 후 실행 했을 때
잘 작동되다가 계속 오류가 발생하였습니다.

그냥..스케일링은 쿠버네티스 또는 EKS를 사용하는게 편한것 같습니다.

0개의 댓글