I am going to use GKE to build containers with rubyonrails, I want to do db: create and db: migrate automatically
Dockerfile made this way
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
Setting db: create and db: migrate /app/post-start.sh
#!/bin/bash
RAILS_ENV = $ RAILS_ENV bundle exec rake db: create
RAILS_ENV = $ RAILS_ENV bundle exec rake db: migrate
It is deployment 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 create -f /kubernetes/rails.yml I did it
Errors - Contents
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
The above error ceased to exist, but there was an error again
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"
Fix the Dockerfile Fixed Deployment.yml It went well. Thank you very much.
The handlers for the postStart is not formatted properly, see below the correct indentation
lifecycle:
postStart:
exec:
command: ["/bin/bash", "-c", "/app/kubernetes-post-start.sh"]
You can check the documentation here
Fix the 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
Fixed Deployment.yml It went well. Thank you very much.
The issue is in indentation in your yaml file, the correct yaml is:
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"]
This should resolve the above error.