如何在Amazon OpsWorks上为Rails应用程序运行Redis? [英] How to run Redis on Amazon OpsWorks for a Rails application?

查看:107
本文介绍了如何在Amazon OpsWorks上为Rails应用程序运行Redis?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Amazon OpsWorks环境中设置Redis实例/图层以用于缓存和Sidekiq,但无法使Rails应用程序与Redis通信.

无论如何我都需要厨师食谱吗?我试图在一个单独的层上创建Redis,在该层上添加了一个实例,但是找不到找到使Rails和Redis之间相互通信的方法.

任何建议如何制作?

谢谢

解决方案

一种可行的方法是创建一个"

在OpsWorks中->层-> Rails App Server安全性:

最后,在Rails项目中,编辑您的 config/production.rb 文件(假设您正在为生产环境工作),并添加如下一行来设置缓存存储:/p>

  config.cache_store =:redis_store,"redis://#{Rails.application.secrets.redis_host}:#{Rails.application.secrets.redis_port}/0/cache",{expires_in:90.分钟 } 

然后要使Sidekiq使用Redis,您需要一个 config/sidekiq.rb 文件,如下所示:

  Sidekiq.configure_server做| config |config.redis = {url:"redis://##Rails.application.secrets.redis_host}:#{Rails.application.secrets.redis_port}/12",network_timeout:Rails.application.secrets.redis_timeout}结尾Sidekiq.configure_client做| config |config.redis = {网址:"redis://#{Rails.application.secrets.redis_host}:#{Rails.application.secrets.redis_port}/12",network_timeout:Rails.application.secrets.redis_timeout}结尾 

您可以在与您的集群相关的节点"列下单击,以检索AWS控制台ElastiCache仪表板内部的redis URL和端口.

除非您要通过ssh在机器内部手动启动它(当然不适合生产),否则仅需要启动和停止sidekiq的配方即可.

在这种情况下,您必须为Deploy事件将自定义配方添加到OpsWorks堆栈中.这个食谱将是这样的:

 #用于部署事件的配方厨师:: Log.info(重新启动Sidekiq ...")node [:deploy] .each |执行应用程序,部署|deploy_to =节点[:deploy] [应用程序] [:deploy_to]rails_env =节点[:deploy] [应用程序] [:rails_env]执行"sidekiq停止"操作用户部署"cwd#{deploy_to}/current/"命令"bundle exec sidekiqctl stop tmp/pids/sidekiq.pid"环境"RAILS_ENV" =>rails_envonly_if {"ps aux | grep [s] idekiq"}结尾bash捆绑"做用户部署"cwd#{deploy_to}/current/"代码<< -EOHRAILS_ENV =#{rails_env}"捆绑包执行sidekiq --index 0 --pidfile tmp/pids/sidekiq.pid-环境#{rails_env}" --logfile log/sidekiq.log --daemonEOH结尾结尾 

希望有帮助!

I am trying to set up Redis instance/layer in the Amazon OpsWorks environment for caching purposes and Sidekiq, but cannot make the Rails application communicate with Redis.

Do I need a Chef recipe for it no matter what? I've tried to create Redis on a separated layer, added an instance to this layer, but cannot find a way to make communicate Rails and Redis between each other.

Any advice how to make it?

Thank you

解决方案

A possible way to do it is create a "ElastiCache cluster" on AWS and tell to Rails to use it.

I have my rails app running with a OpsWorks stack and I use redis for two different reasons: use Sidekiq for delayed job and use the cache store.

Is very important to set the correct security group for your redis ElastiCache cluster, this security group must be available for your OpsWorks stack.

First create your Redis ElastiCache cluster. Then go in your AWS console, click on "EC2", then click on "Security Group" (under NETWORK & SECURITY). Search the security group using the security group id associated to your elasticache cluster.

Now set an Inbound rule where the source is the opsworks security group that you have inside opsworks.

In your EC2 -> Security Group:

In OpsWorks -> Layers -> Rails App Server Security:

At the end, in you Rails project, edit your config/production.rb file (assuming that you are working for production env) and add a line like this to set your cache store:

config.cache_store = :redis_store, "redis://#{Rails.application.secrets.redis_host}:#{Rails.application.secrets.redis_port}/0/cache", { expires_in: 90.minutes }

Then to make Sidekiq use Redis, you need a config/sidekiq.rb file like this:

Sidekiq.configure_server do |config|
  config.redis = { url: "redis://#{Rails.application.secrets.redis_host}:#{Rails.application.secrets.redis_port}/12", network_timeout: Rails.application.secrets.redis_timeout }
end

Sidekiq.configure_client do |config|
  config.redis = { url: "redis://#{Rails.application.secrets.redis_host}:#{Rails.application.secrets.redis_port}/12", network_timeout: Rails.application.secrets.redis_timeout }
end

You can retrieve the redis URL and port inside your AWS Console ElastiCache Dashboard, clicking under Nodes column related to your cluster.

You need a recipe only to Start and Stop sidekiq, unless you want to start it manually going inside your machine via ssh (of course not good for production).

In this case you have to add a Custom recipe to your OpsWorks stack, for the Deploy event. This recipe will be something like this:

# Recipe used for a deploy event
Chef::Log.info("Restart Sidekiq...")

node[:deploy].each do |application, deploy|
  deploy_to = node[:deploy][application][:deploy_to]
  rails_env = node[:deploy][application][:rails_env]

  execute "sidekiq stop" do
    user "deploy"
    cwd "#{deploy_to}/current/"
    command "bundle exec sidekiqctl stop tmp/pids/sidekiq.pid"
    environment "RAILS_ENV" => rails_env
    only_if { "ps aux | grep [s]idekiq" }
  end

  bash "bundle" do
    user "deploy"
    cwd "#{deploy_to}/current/"
    code <<-EOH
      RAILS_ENV="#{rails_env}" bundle exec sidekiq --index 0 --pidfile tmp/pids/sidekiq.pid --environment "#{rails_env}" --logfile log/sidekiq.log --daemon
    EOH
  end
end

Hope it helps!

这篇关于如何在Amazon OpsWorks上为Rails应用程序运行Redis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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