我在google cloud中运行了一个rails应用程序,使用:
云构建
云注册表
云端运行
云sql
云存储
云密钥管理服务
云调度程序
我已经将sidekiq配置为在后台执行一些作业,但是当我尝试执行sidekiq作业时,在云运行日志中出现了这个错误。
Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)):
我让您在这里查看我的部署设置:
云构建.yaml
steps:
# Decrypt Rails Master key file
- name: gcr.io/cloud-builders/gcloud
args: ["kms", "decrypt", "--ciphertext-file=./config/master.key.enc",
"--plaintext-file=./config/master.key",
"--location=us-central1","--keyring=project_name",
"--key=rails_master_key"]
# Decrypt Whale on Rails service account credentials
- name: gcr.io/cloud-builders/gcloud
args: ["kms", "decrypt", "--ciphertext-file=./config/project_name_runner.key.enc",
"--plaintext-file=./config/project_name_runner.key",
"--location=us-central1","--keyring=project_name",
"--key=project_name_runner_key"]
# Build image with tag 'latest' and pass decrypted Rails DB password as argument
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build',
'--tag', 'gcr.io/$PROJECT_ID/project_name:latest',
'--build-arg', 'DB_PWD',
'--build-arg', 'RUBY_VERSION=${_RUBY_VERSION}',
'--build-arg', 'PG_MAJOR=${_PG_MAJOR}',
'--build-arg', 'NODE_MAJOR=${_NODE_MAJOR}',
'--build-arg', 'BUNDLER_VERSION=${_BUNDLER_VERSION}',
'--build-arg', 'RAILS_ENV=${_RAILS_ENV}',
'--build-arg', 'REDIS_URL=${_REDIS_URL}',
'--build-arg', 'DATABASE_HOST=${_DATABASE_HOST}',
'--build-arg', 'DATABASE_USER=${_DATABASE_USER}',
'--build-arg', 'DATABASE_NAME=${_DATABASE_NAME}',
'.'
]
secretEnv: ['DB_PWD']
# Push new image to Google Cloud Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/project_name:latest']
secrets:
- kmsKeyName: projects/project_name/locations/us-central1/keyRings/project_name/cryptoKeys/db_pwd_key
secretEnv:
DB_PWD: "db_password"
substitutions:
_RUBY_VERSION: '2.7.0'
_PG_MAJOR: '11'
_NODE_MAJOR: '12'
_BUNDLER_VERSION: '2.1.2'
_RAILS_ENV: production
_REDIS_URL: redis://redis:6379/
_DATABASE_HOST: /cloudsql/project_name:us-central1:project_name-production
_DATABASE_USER: production_user
_DATABASE_NAME: project_name-production
入口点.sh
# !/usr/bin/env bash
cd /usr/src/app
# Create the Rails production DB on first run
bundle exec rake db:prepare
# Do some protective cleanup
> log/production.log
rm -f tmp/pids/server.pid
# Run the web service on container startup
# $PORT is provided as an environment variable by Cloud Run
bundle exec rails server -b 0.0.0.0 -p $PORT
# Run sidekiq in production
bundle exec sidekiq -C config/sidekiq.yml
dockerfile文件
# Leverage the official Ruby image from Docker Hub
# https://hub.docker.com/_/ruby
ARG RUBY_VERSION
FROM ruby:$RUBY_VERSION
LABEL maintainer="myemail@gmail.com"
ARG PG_MAJOR
ARG NODE_MAJOR
ARG BUNDLER_VERSION
ARG RAILS_ENV
# Add PostgreSQL to sources list
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list
# Install recent versions of nodejs (10.x) and yarn pkg manager
# Needed to properly pre-compile Rails assets
RUN (curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -) && apt-get update && apt-get install -y nodejs
# Add Yarn to the sources list
RUN (curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -) && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn
# Install production dependencies (Gems installation in
# local vendor directory)
WORKDIR /usr/src/app
COPY Gemfile Gemfile.lock ./
ENV BUNDLE_FROZEN=true
RUN gem update --system && \
gem install bundler:$BUNDLER_VERSION
RUN bundle install
# Copy application code to the container image.
# Note: files listed in .gitignore are not copied
# (e.g.secret files)
COPY . .
# Pre-compile Rails assets (master key needed)
RUN yarn install
RUN RAILS_ENV=production bundle exec rake assets:precompile
# Set Google App Credentials environment variable with Service Account
ENV GOOGLE_APPLICATION_CREDENTIALS=/usr/src/app/config/sunne_cms_api_runner.key
# Setup Rails DB password passed on docker command line (see Cloud Build file)
ARG DATABASE_NAME
ARG DATABASE_HOST
ARG DATABASE_USER
ARG DB_PWD
ENV DATABASE_PASSWORD=${DB_PWD}
ENV DATABASE_NAME=${DATABASE_NAME}
ENV DATABASE_HOST=${DATABASE_HOST}
ENV DATABASE_USER=${DATABASE_USER}
# Setting up Rails environment
ENV RAILS_ENV=${RAILS_ENV}
# For now we don't have a Nginx/Apache frontend so tell
# the Puma HTTP server to serve static content
# (e.g. CSS and Javascript files)
ENV RAILS_SERVE_STATIC_FILES=true
# Redirect Rails log to STDOUT for Cloud Run to capture
ENV RAILS_LOG_TO_STDOUT=true
# Designate the initial sript to run on container startup
RUN chmod +x /usr/src/app/entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
2条答案
按热度按时间relj7zay1#
云运行不支持后台作业。您的容器以http请求开始,并在请求返回时结束。在请求返回后,不要再期待任何东西。
云运行不是操作系统、任务调度器、后台线程处理器等。
阅读本文了解cloud run可以/不能做什么:
云运行容器合同
5tmbdcev2#
正如johnhanley提到的,cloudrun不支持后台处理。根据你的要求,你有一些选择。
方案1
使用单独的服务来运行后台作业,可能使用云发布/订阅服务来服务消息订阅,以便在作业准备就绪时进行广播。您的web服务可以返回订阅的句柄/id,调用者可以使用它来侦听更新。
如果合适的话,后台服务本身可以在自己的云运行容器中运行,或者因为您似乎在使用nodejs,所以可以将其打包为云函数。
方案2
将容器移动到支持后台处理的解决方案,如appengine或kubernetes engine(gke)。不过,这两种云计算的定价模式截然不同,而且根据您的使用模式,最终的成本可能会大大提高(这篇google博客文章分析了gke和cloud run之间的区别。)
gke可以处理单个容器的设置,而不需要您直接学习或处理kubernetes。但是如果您的项目架构发生了变化,或者您需要进行任何配置或故障排除,那么可能会涉及到一个重要的学习曲线。
方案3
重写代码以使用同步处理,在作业完成之前保持请求。只有当您的作业总是在超时(60秒?)之前完成,并且您的最终用例允许时,这才是可行的。这是一个最简单的解决方案,但限制性最大,并且容易出现面向用户的错误。