将Rails + Puma + Postgres应用程序部署到Elastic beantalk的正确方法? [英] Right way to deploy Rails + Puma + Postgres app to Elastic beanstalk?

查看:56
本文介绍了将Rails + Puma + Postgres应用程序部署到Elastic beantalk的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 5 API,我正在尝试(正确)部署在Elastic Beanstalk上.

I have an Rails 5 API which I am trying to deploy(correctly) on Elastic Beanstalk.

这是我使用的最初的config/puma.rb文件:

Here is my initial config/puma.rb file which I use:

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
port        ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
environment ENV.fetch("RAILS_ENV") { "development" }

# Allow puma to be restarted by `rails restart` command.

plugin :tmp_restart

我收到以下套接字错误:

I get the following socket error:

2015/11/24 06:44:12 [crit] 2689#0: *4719 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream

为解决此问题,我尝试添加以下行并使其起作用:

To fix this I tried adding below lines and got it to work:

rails_env = ENV['RAILS_ENV'] || "production"
if rails_env == "production"
  bind "unix:///var/run/puma/my_app.sock"
  pidfile "/var/run/puma/my_app.sock"
end

我真正的问题是,这是正确的方法吗?如果有人以前做过,您能指出我吗?有没有办法通过docker容器来做到这一点?

My real question is, is this the right way to do it? If anyone has done it before can you point me to it? Is there a way to do this via docker containers?

推荐答案

您也可以将Rails应用程序作为Rails-Puma应用程序部署到Elastic Beanstalk或Docker.答案将是更笼统的,而不是提供完整的解决方案.从哪里开始.

You can deploy your Rails app as a Rails - Puma app to Elastic Beanstalk or Docker as well. The answer will be more general and rather points where to start than provides complete solution.

Ruby-Puma

这可能是一个非常棘手的问题:如果您通过控制台(在Web浏览器中)为Ruby创建新的Ruby Elastic Beanstalk环境,它将默认设置乘客平台,而不是Puma平台.可能您无法在控制台中更改它:

This can be a quite tricky: If you create new Elastic Beanstalk Environment for Ruby via Console (in Web Browser), it can set Passenger platform by default, instead of Puma platform. And probably you can't change it in console:

要使用Puma创建新环境,请使用eb cli.很好的演练此处.但是,在运行eb create之前,您还必须做一件事-选择平台:

To create new environment with Puma, use eb cli. Nice walkthrough here. However, before you run eb create, you have to do one more thing - select platform:

$ eb platform select

It appears you are using Python. Is this correct?
(y/n): n

Select a platform.
1) Go
2) Node.js
3) PHP
4) Python
5) Ruby
6) Tomcat
7) IIS
8) Docker
9) Multi-container Docker
10) GlassFish
11) Java
(default is 1): 5

Select a platform version.
1) Ruby 2.3 (Puma)
2) Ruby 2.2 (Puma)
3) Ruby 2.1 (Puma)
4) Ruby 2.0 (Puma)
5) Ruby 2.3 (Passenger Standalone)
6) Ruby 2.2 (Passenger Standalone)
7) Ruby 2.1 (Passenger Standalone)
8) Ruby 2.0 (Passenger Standalone)
9) Ruby 1.9.3
(default is 1):

如果要创建Elastic Beanstalk的Worker而不是Web Server,请运行:

If you want to create Elastic Beanstalk's Worker instead of Web Server, run:

 $ eb create -t worker

您可以使用控制台(Web浏览器)或eb cli(

You can use Console (Web Browser) or eb cli (docs) to set other configuration.

Docker

以下文章可能对设置Rails + Puma + Nginx + Docker有用:

Following post maybe useful how to setup Rails + Puma + Nginx + Docker:

http://codepany.com/blog/rails- 5和docker-puma-nginx/

这是多容器配置,其中Nginx绑定到端口80,并通过套接字将请求流传输到puma.在您的情况下,它将是:"unix:///var/run/puma/my_app.sock"

This is multicontainer configuration, where Nginx is binded to port 80 and streams request to puma via socket. In your case it would be: "unix:///var/run/puma/my_app.sock"

要上传Docker,您可以使用AWS ECR存储Docker映像.您必须创建Dockerrun.aws.json文件(与docker-compose.yml文件非常相似),然后可以通过AWS Console(Web浏览器)将其部署到您的环境中.

To upload Dockers, you can use AWS ECR to store Docker images. You have to create Dockerrun.aws.json file (is quite similar to docker-compose.yml file), which you can than deploy via AWS Console (web browser) to your environment.

编辑

这是puma.rb配置文件:

threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
threads threads_count, threads_count

bind "unix:///var/run/puma.sock?umask=0000"

stdout_redirect "/var/log/puma.stdout.log", "/var/log/puma.stderr.log", true

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch('RAILS_ENV') { 'development' }

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

某些设置可能会有所不同,但关键是我在那里将一个puma服务器绑定到了unix套接字,并且它与NGINX连接. NGINX配置文件:

Some settings may vary, but the point is that I bind there a puma server to unix socket and it connects with NGINX. The NGINX configuration file:

user  root;

error_log  /var/log/app-nginx-error.log;
pid        /var/run/app-nginx.pid;

events {
    worker_connections  8096;
    multi_accept        on;
    use                 epoll;
}

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/app-nginx-access.log  main;

    sendfile           on;
    tcp_nopush         on;
    tcp_nodelay        on;
    keepalive_timeout  10;

    upstream appserver {
      server unix:///var/run/puma.sock;
    }

    server {
      listen 80 default_server;
      root /var/www/public;
      client_max_body_size  16m;

      location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }

      try_files $uri/index.html $uri @appserver;
      location @appserver {
        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_set_header  X-Forwarded-Host $server_name;
        proxy_set_header  Client-IP $remote_addr;
        proxy_pass        http://appserver;
      }

      access_log    /var/log/app-nginx-access.log;
      error_log     /var/log/app-nginx-error.log debug;
      error_page    500 502 503 504 /500.html;
    }
}

NGINX配置文件中最重要的部分是:

The most important part in NGINX configuration file is:

upstream appserver {
  server unix:///var/run/puma.sock;
}

这篇关于将Rails + Puma + Postgres应用程序部署到Elastic beantalk的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆