新手问题 使用 Docker 為 Rails 系統作橫向擴展?

Aiken00 · February 20, 2018 · Last by hechian replied at July 20, 2018 · 1876 hits

作為 Docker 以及 Ngnix 的配置新手我卡關了

我在嘗試以 docker 來 scale up Rails 系統 在沒有 Ngnix 時我是能夠成夠運行 Rails 系統的

但當使用 nginx-proxy 後及 Scale up 後我發現 rails 不能再指定某個 port 了 而且 proxy 也不能指向 Rails 系統群作 load balancing

同時有沒有關於 Ngnix 配置的常數作參考? 我的docker-compose.yml配置如下

version: '3'
services:
  db:
    container_name: 'postgresql'
    image: postgres
    ports:
      - '5432:5432'
  redis:
    container_name: 'redis'
    image: 'redis:4.0.8-alpine'
    command: redis-server --requirepass yourpassword
    ports:
      - '6379:6379'
    volumes:
      - 'redis:/data'
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s"
    volumes:
      - .:/mapAPI
    env_file:
      - '.env'
    depends_on:
      - db
      - redis
  sidekiq:
    depends_on:
      - 'db'
      - 'redis'
    build: .
    command: bundle exec sidekiq -C config/sidekiq.yml
    volumes:
      - '.:/mapAPI'
    env_file:
      - '.env'
  proxy:
    image: jwilder/nginx-proxy:latest
    ports:
      - "80:80"
    volumes:
      - "/var/run/docker.sock:/tmp/docker.sock"
volumes:
  redis:
  postgres:

能解释下 rails不能再指定某個port?是因为在同一台机器上运行多个 containers 所以同一个 port 会冲突?

nginx 应该跑在主机上,成为 container 的 balance

写个脚本,读随机生成的 port,生成一个 conf,reload nginx. 我在测试环境这么做的。生产用的 swarm mode。

Reply to n5ken

對當我嘗試指定 port 為 3000 時,以後的 instance 就不能創建了

Reply to pynix

nginx 跟 load balancer 不是在 docker 裏面運行才對的嗎?

Reply to lithium4010

感謝回覆 Devops 我自己是個新手,請問有沒有類似的代碼作參考? Swarm 我有看過一下,但還是不太理解背後的運作。

Reply to Aiken00

是正體中文欸 XD

command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s"

這邊你在 rails s 改成 rails s -p $RANDOM 試試,可以 google 一下 bash random

You need to Sign in before reply, if you don't have an account, please Sign up first.