gpt4 book ai didi

mysql - Kubernetes 中的作业出错(如何使用 Kubernetes 部署 Rails 应用程序?)

转载 作者:行者123 更新时间:2023-11-29 15:15:04 25 4
gpt4 key购买 nike

我用rails + mysql + vue.js + webpacker 制作了我的项目。

然后我制作了 Dockerfiledocker-compose.yml,这样我就可以在本地主机中运行它。

现在我想用K8s部署它们,但是我在K8s中的Job有问题。实际上这是我第一次使用 K8s,所以如果有什么遗漏或错误,请让我知道我需要做什么。这是我的 Dockerfile 和 yml。

ps。 khjoo19/line-manager:v1 是我上传到 docker hub 的 docker 镜像。

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

最佳答案

我应用了您的部署,首先看到的是 mysql 数据库崩溃。使用 kubectl logs -n line-manager <db-pod-name> 检查日志后我看到了这个:

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

所以我添加了MYSQL_ROOT_PASSWORD: 'password'rails-config configmap 并开始工作。

然后我部署了web-job作业,发现它也崩溃了,所以我检查了日志:

...
Mysql2::Error: No database selected
...

在容器中khjoo19/line-manager:v1./config/database.yml文件我找到了这一行:database: <%= ENV['DB_DATABASE'] %>这表明代码正在尝试从 DB_DATABASE 加载数据库名称环境变量没有在任何地方设置,所以我将此环境变量添加到rails-config:

apiVersion: v1
kind: ConfigMap
metadata:
namespace: line-manager
name: rails-config
data:
...
DB_DATABASE: 'database'

然后它就成功了,工作成功完成。

请告诉我是否有帮助。

关于mysql - Kubernetes 中的作业出错(如何使用 Kubernetes 部署 Rails 应用程序?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59745173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com