前提・実現したいこと
自作railsアプリケーション(DBはMysql)をDocker環境で動くように開発し、GKE(google kubernetes engine)にデプロイしたい
こちらのサイトを参考にしています
前提
・アプリケーションはローカル(開発環境)だとdocker-compose upで正常に起動する状態
・localのワーキングディレクトリにはkey.jsonがある
・GCPのIAMと管理「サービス アカウントの詳細」にキーは設定されていてkey.jsonのものと同じ(メールアドレスも一致)
・cloudsql-db-credentialsとcloudsql-instance-credentialsはGCP側に設定されている
GKE構成
・nodeは2つ、それぞれに同じ2つpodを入れている
・マニュフェストファイルはひとつ(deployment.yml)
json
1#key.json 2{ 3 "type": "service_account", 4 "project_id": "プロジェクト名", 5 "private_key_id": "・・・8a8c5", 6 "private_key": "-----BEGIN PRIVATE KEY-----\n・・・BfYN\nMkTAtnTgtiC77mllNDhONA==\n-----END PRIVATE KEY-----\n", 7 "client_email": "test-user@プロジェクト名.iam.gserviceaccount.com", 8 "client_id": "・・・06700", 9 "auth_uri": "https://accounts.google.com/o/oauth2/auth", 10 "token_uri": "https://oauth2.googleapis.com/token", 11 "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 12 "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-user%40プロジェクト名.iam.gserviceaccount.com" 13}
発生している問題・エラーメッセージ
GKEにデプロイする際にエラー(podのstatusがCrashLoopBackOff)になってしまう。
ruby
1textPayload: "/usr/local/bundle/gems/railties-5.2.3/lib/rails/application.rb:585:in `validate_secret_key_base': Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit` (ArgumentError)
ruby
1textPayload: "2019/07/02 14:18:29 invalid json file "./key.json": open ./key.json: no such file or directory
bash
1$ kubectl get pods 2NAME READY STATUS RESTARTS AGE 3rails-api-798fd446db-c6jwc 1/2 CrashLoopBackOff 1 5s 4rails-api-798fd446db-d8g8k 1/2 CrashLoopBackOff 1 5s
該当のソースコード
ruby
1#deployment.yml 2apiVersion: extensions/v1beta1 3kind: Deployment 4metadata: 5 name: rails-api 6spec: 7 replicas: 2 8 template: 9 metadata: 10 labels: 11 app: rails-api 12 spec: 13 containers: 14 - image: gcr.io/プロジェクト名/rails_api 15 name: web 16 env: 17 - name: DB_USER 18 valueFrom: 19 secretKeyRef: 20 name: cloudsql-db-credentials 21 key: username 22 - name: DB_PASS 23 valueFrom: 24 secretKeyRef: 25 name: cloudsql-db-credentials 26 key: password 27 - name: DB_NAME 28 value: SQLのインスタンス 29 - name: DB_HOST 30 value: 127.0.0.1 31 - name: RAILS_ENV 32 value: production 33 - name: RACK_ENV 34 value: production 35 - name: RAILS_SERVE_STATIC_FILES 36 value: 'true' 37 ports: 38 - containerPort: 3000 39 name: rails-api 40 command: ["rm", "-f", "/rails_api/tmp/pids/server.pid"] 41 command: ["bundle", "exec", "rails", "server", "-p", "3000", "-b", "0.0.0.0"] 42 - image: b.gcr.io/cloudsql-docker/gce-proxy:1.11 43 name: cloudsql-proxy 44 command: ["/cloud_sql_proxy", 45 "-instances=プロジェクト名:asia-northeast1:SQLのインスタンス=tcp:3306", 46 "-credential_file=./key.json"] 47 volumeMounts: 48 - name: cloudsql-instance-credentials 49 mountPath: /secrets/cloudsql 50 readOnly: true 51 volumes: 52 - name: cloudsql-instance-credentials 53 secret: 54 secretName: cloudsql-instance-credentials
ruby
1#Dockerfile 2FROM ruby:2.6.2 3 4 5# 必要なパッケージのインストール(基本的に必要になってくるものだと思うので削らないこと) 6RUN apt-get update -qq && \ 7 apt-get install -y build-essential \ 8 libpq-dev \ 9 nodejs 10 11# 作業ディレクトリの作成、設定 12RUN mkdir /rails_api 13##作業ディレクトリ名をAPP_ROOTに割り当てて、以下$APP_ROOTで参照 14ENV APP_ROOT /rails_api 15WORKDIR $APP_ROOT 16 17# ホスト側(ローカル)のGemfileを追加する(ローカルのGemfileは【3】で作成) 18ADD ./Gemfile $APP_ROOT/Gemfile 19ADD ./Gemfile.lock $APP_ROOT/Gemfile.lock 20 21# Gemfileのbundle install 22RUN bundle install 23ADD . $APP_ROOT

あなたの回答
tips
プレビュー