I made my project with rails + mysql + vue.js + webpacker.
Then I made Dockerfile
and docker-compose.yml
, so i can run it in localhost.
Now I want to deploy them with K8s, but I have a problem with Job in K8s. Actually this is my first time to work with K8s, so if there is something missing or something wrong, please let me know what i need to do. Here's my Dockerfile and ymls.
ps. khjoo19/line-manager:v1
is my docker image uploaded to docker hub.
Dockerfile
FROM ruby:2.5.5
ENV LANG C.UTF-8
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs && \
curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
apt-get install nodejs
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
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 && \
gem install bundler
WORKDIR /tmp
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT [ "entrypoint.sh" ]
ENV APP_HOME /line_manager
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
COPY . /line_manager
RUN yarn upgrade
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
environment:
DB_USER: root
DB_ROOT_PASSWORD: password
DB_PASSWORD: password
DB_HOST: db
webpacker:
build: .
command: bundle exec bin/webpack-dev-server
volumes:
- .:/line_manager
ports:
- "8080:8080"
web:
build: .
command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/line_manager
environment:
DB_USER: root
DB_ROOT_PASSWORD: password
DB_PASSWORD: password
DB_HOST: db
RAILS_ENV: development
ports:
- "3000:3000"
depends_on:
- db
- webpacker
tty: true
stdin_open: true
mysql.yml (K8s)
apiVersion: v1
kind: Service
metadata:
namespace: line-manager
name: db
labels:
app: web
spec:
selector:
app: web
clusterIP: None
ports:
- port: 3306
---
kind: PersistentVolume
apiVersion: v1
metadata:
namespace: line-manager
name: db-pv
labels:
app: web
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
storageClassName: standard
hostPath:
path: "/tmp/mysql"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
namespace: line-manager
name: db-pvc
labels:
app: web
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: standard
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: line-manager
name: db
labels:
app: web
spec:
selector:
matchLabels:
app: web
strategy:
type: Recreate
template:
metadata:
labels:
app: web
spec:
containers:
- name: mysql
image: mysql:5.7.17
envFrom:
- configMapRef:
name: rails-config
ports:
- containerPort: 3306
name: db
volumeMounts:
- name: db-pv
mountPath: /var/lib/mysql
volumes:
- name: db-pv
persistentVolumeClaim:
claimName: db-pvc
rails-config.yaml(K8s)
apiVersion: v1
kind: ConfigMap
metadata:
namespace: line-manager
name: rails-config
data:
RAILS_ENV: 'development'
DB_HOST: 'db'
DB_USER: 'root'
DB_ROOT_PASSWORD: 'password'
DB_PASSWORD: 'password'
rails-job.yaml(K8s)
apiVersion: batch/v1
kind: Job
metadata:
namespace: line-manager
name: web-job
labels:
app: web
spec:
template:
metadata:
namespace: line-manager
name: web-job
labels:
app: web
spec:
containers:
- name: web-job
image: khjoo19/line-manager:v1
imagePullPolicy: Always
command: ["/bin/sh"]
args: ["-c", "bundle exec rake db:create && bundle exec rake db:migrate"]
envFrom:
- configMapRef:
name: rails-config
restartPolicy: Never
I applied your deployments and first I saw was mysql database crashing. After checking logs with kubectl logs -n line-manager <db-pod-name>
I saw this:
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
So I added MYSQL_ROOT_PASSWORD: 'password'
to rails-config configmap and it started working.
Then I deployed web-job job and saw it was also crashing so I checked the logs:
...
Mysql2::Error: No database selected
...
In the container khjoo19/line-manager:v1
in ./config/database.yml
file I have found this line: database: <%= ENV['DB_DATABASE'] %>
which suggests the code is trying to load DB name from DB_DATABASE
environment variable which wasn't being set anywhere so I added this env variable to rails-config:
apiVersion: v1
kind: ConfigMap
metadata:
namespace: line-manager
name: rails-config
data:
...
DB_DATABASE: 'database'
and then it worked and job completed successfully.
Let me know if it helped.