因为发生部署错误,所以我想做点什么

时间:2019-01-24 10:03:01

标签: ruby-on-rails docker kubernetes google-kubernetes-engine

我将使用GKE构建带有rubyonrails的容器, 我想做数据库:创建和数据库:自动迁移

Dockerfile就是这样

Dockerfile

FROM ruby: 2.4.1-alpine

RUN apk update && apk upgrade && apk add --update - no - cache bash alpine - sdk tzdata postgresql - dev nodejs
RUN mkdir / app
WORKDIR / app

ADD Gemfile / app / Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install - path vendor / bundle
ADD. / App

RUN bundle exec rake assets: precompile

RUN chmod + x /app/post-start.sh

EXPOSE 3000

设置数据库:创建和数据库:迁移 /app/post-start.sh

#!/bin/bash
RAILS_ENV = $ RAILS_ENV bundle exec rake db: create
RAILS_ENV = $ RAILS_ENV bundle exec rake db: migrate

是部署 rails.yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: rails
  labels:
    app: rails
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rails
  template:
    metadata:
      labels:
        app: rails
    spec:
      containers:
        - image: asia.gcr.io/balmy-geography-216916/railsinitpg:v1
          name: rails
          env:
            - name: RAILS_ENV
              value: "production"
            - name: DATABASE_HOST
              value: postgresql
            - name: DATABASE_USERNAME
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: database_user
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: database_password
            - name: SECRET_KEY_BASE
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: secret_key_base
            - name: DATABASE_PORT
              value: "5432"
          lifecycle:
        postStart:
         exec:
          command:
            - /bin/bash
            - -c
            - /app/post-start.sh
          ports:
            - containerPort: 3000
              name: rails
          command: ["bundle", "exec", "rails", "s", "-p", "3000", "-b", "0.0.0.0"]

kubectl创建-f /kubernetes/rails.yml 我做到了

错误-目录

error: valid validating "STDIN": error validating data: ValidationError (Deployment.spec.template.spec.containers [0]): unknown field "postStart" in io.k8s.api.core.v1.Container; if you choose to ignore these errors, turn validation off with --validate = false

以上错误已不复存在,但又有错误

Warning  FailedPostStartHook    5m1s (x4 over 5m49s)   kubelet, gke-cluster-1-default-pool-04b4ba0b-680k  Exec lifecycle hook ([/bin/bash -c /app/post-start.sh]) for Container "rails" in Pod "rails-5c5964445-6hncg_default(1d6a3479-1fcc-11e9-8139-42010a9201ec)" failed - error: command '/bin/bash -c /app/post-start.sh' exited with 126: , message: "rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused \"exec: \\\"/bin/bash\\\": stat /bin/bash: no such file or directory\"\n\r\n"

修复Dockerfile 固定Deployment.yml 进展顺利。 非常感谢。

3 个答案:

答案 0 :(得分:2)

问题出在yaml文件的缩进中,正确的yaml是:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: rails
  labels:
    app: rails
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rails
  template:
    metadata:
      labels:
        app: rails
    spec:
      containers:
        - image: asia.gcr.io/balmy-geography-216916/railsinitpg:v1
          name: rails
          env:
            - name: RAILS_ENV
              value: "production"
            - name: DATABASE_HOST
              value: postgresql
            - name: DATABASE_USERNAME
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: database_user
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: database_password
            - name: SECRET_KEY_BASE
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: secret_key_base
            - name: DATABASE_PORT
              value: "5432"
          lifecycle:
            postStart:
              exec:
                command:
                - /bin/bash
                - -c
                - /app/kubernetes-post-start.sh
          ports:
            - containerPort: 3000
              name: rails
          command: ["bundle", "exec", "rails", "s", "-p", "3000", "-b", "0.0.0.0"]    

这应该可以解决上述错误。

答案 1 :(得分:0)

postStart的处理程序格式不正确,请参见下面的  正确的缩进

  lifecycle:
    postStart:
      exec:
        command: ["/bin/bash", "-c", "/app/kubernetes-post-start.sh"]

您可以查看文档here

答案 2 :(得分:0)

修复Dockerfile

FROM ruby: 2.4.1-alpine

RUN apk update && apk upgrade && apk add --update - no - cache bash alpine - sdk tzdata postgresql - dev nodejs
RUN mkdir / app
WORKDIR / app

ADD Gemfile / app / Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install - path vendor / bundle
ADD. / App

RUN bundle exec rake assets: precompile

RUN chmod + x /app/post-start.sh

EXPOSE 3000

修复Deployment.yml 进展顺利。 非常感谢。