问题
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
回答1:
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!
来源:https://stackoverflow.com/questions/35741359/how-to-run-redis-on-amazon-opsworks-for-a-rails-application