首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Rails在使用Postgresql的基于Rancher的Docker环境中运行缓慢

Rails在使用Postgresql的基于Rancher的Docker环境中运行缓慢
EN

Stack Overflow用户
提问于 2019-04-19 06:15:27
回答 1查看 516关注 0票数 2

我们使用的是基于Rancher的Docker环境。服务器配备得很好,我没有感觉到任何性能问题。但是启动性能和控制台的性能都非常慢。

启动puma-的分钟数(在我的本地虚拟机上: 10分钟56秒访问控制台rails c (在我的本地虚拟机上:7秒)

  • 17分钟!)使用rails控制台中的"oneliner“从70000条记录中更新100000条记录。每秒只有70条记录。select本身只需要31ms。

Object.where(view: 0).each { |obj| obj.update_columns(view: 1) }

我认为这应该会快得多。当我是数据库中唯一的用户时,我对它进行了测试。

我还比较了我的本地虚拟机(2核3 3GB )

Benchmark.bm do |x|
  x.report { 1000.times do Object.first.update_columns(view: 0) end }
end

本地

       user     system      total        real
   2.472000   0.216000   2.688000 (  4.719130)

服务器

       user     system      total        real
   1.961856   0.164141   2.125997 ( 14.161671)

那么,谁知道是什么让一切变得如此缓慢?

根据PgHero的说法,数据库是正常的,没有无用的索引。

如果您需要更多信息,请询问。

硬件

CPU和ram都在30% -一切都很安静。

前面提到的"oneliner“进程使用了一个内核的5.7%

处理器:英特尔®酷睿™i7-6700四核

RAM: 64 GB DDR4内存

硬盘: 1TB固态硬盘

环境

操作系统: Ubuntu 18.04.1

Ruby: 2.6.6

Rails: 5.2.4.2

PostgreSQL: 10.12

PostgreSQL不存储在pod中,而是直接存储在机器上,通过ip 172.17.0.1访问

牧场主: v2.2.3

用户界面: v2.2.41

舵机: v2.10.0-rancher10

机器: v0.15.0-rancher6-1

项目

一个中型项目,大约有74个宝石,167个型号和1128条路线。

time bundle exec rake environment
real    0m2,164s
user    0m1,717s
sys     0m0,423s
Benchmark.ms { Rails.application.eager_load! }
=> 16.18773490190506

主Dockerfile使用标准的ruby-slim image。

文档文件

mytag/my_ruby_2_6_6:

FROM ruby:2.6.6-slim
LABEL maintainer="my@email.com"

WORKDIR /app

EXPOSE 3000

# Set the locale
RUN apt-get update && \
    apt-get install -y locales

RUN sed -i -e 's/# de_DE.UTF-8 UTF-8/de_DE.UTF-8 UTF-8/' /etc/locale.gen && \
    locale-gen de_DE.UTF-8

ENV LANG=de_DE.UTF-8 \
    LANGUAGE=de_DE:de \
    LC_ALL=de_DE.UTF-8

RUN echo "set input-meta on" >> /etc/inputrc && \
    echo "set output-meta on" >> /etc/inputrc && \
    echo "set convert-meta off" >> /etc/inputrc && \
    echo "export LANG=de_DE.utf8" >> /etc/profile && \
    cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime

# install bundler
RUN gem install bundler

# install some tools
RUN apt-get install -y cron build-essential git nodejs imagemagick libpq-dev

# Rails ENV
ARG RAILS_ENV=production

# BUNDLER options
ARG BUNDLER_OPTS=" --without development test"

# clean up
RUN apt-get autoremove -y

# dummy start command
CMD ["/bin/bash"]
FROM mytag/my_ruby_2_6_6
LABEL maintainer="my@email.com"

ARG RAILS_ENV=production

COPY Gemfile* ./

# install rubygem
COPY Gemfile Gemfile.lock /app/
RUN bundle config git.allow_insecure true && \
    bundle install --jobs 20 $BUNDLER_OPTS

COPY . /app

RUN rails assets:precompile

WORKDIR /app

# Expose Puma port
EXPOSE 3000

# Start up
CMD ["docker/startup.sh"]

脚本docker/startup.sh是迁移和启动服务器的一项简单任务。

#! /bin/sh
bundle exec rake db:migrate
echo "Migration Done!"

bundle exec rails s -b 0.0.0.0

牧场主YAML (摘录//匿名)

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "58"
  creationTimestamp: "2019-06-03T19:03:28Z"
  generation: 75
  labels:
    workload.user.cattle.io/workloadselector: deployment-railsapp-railsapp
  name: railsapp
  namespace: railsapp
  resourceVersion: "2133509"
  selfLink: /apis/apps/v1beta2/namespaces/railsapp/deployments/railsapp
spec:
  progressDeadlineSeconds: 600
  replicas: 5
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      workload.user.cattle.io/workloadselector: deployment-railsapp-railsapp
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      annotations:
        cattle.io/timestamp: "2019-06-18T12:40:43Z"
      creationTimestamp: null
      labels:
        workload.user.cattle.io/workloadselector: deployment-railsapp-railsapp
    spec:
      affinity: {}
      containers:
      - env:
        - name: DB_HOST
          value: 172.17.0.1
        image: myapp/railsapp:master-4996
        imagePullPolicy: Always
        livenessProbe:
          failureThreshold: 10
          initialDelaySeconds: 70
          periodSeconds: 5
          successThreshold: 1
          tcpSocket:
            port: 3000
          timeoutSeconds: 2
        name: railsapp
        readinessProbe:
          failureThreshold: 10
          initialDelaySeconds: 70
          periodSeconds: 5
          successThreshold: 2
          tcpSocket:
            port: 3000
          timeoutSeconds: 2
        resources: {}
        securityContext:
          allowPrivilegeEscalation: false
          capabilities: {}
          privileged: false
          procMount: Default
          readOnlyRootFilesystem: false
          runAsNonRoot: false
        stdin: true
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        tty: true
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 5
  conditions:
  - lastTransitionTime: "2019-06-18T12:33:27Z"
    lastUpdateTime: "2019-06-18T12:33:27Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2019-06-11T08:22:59Z"
    lastUpdateTime: "2019-06-21T13:11:49Z"
    message: ReplicaSet "railsapp-958579c56" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 75
  readyReplicas: 5
  replicas: 5
  updatedReplicas: 5

我发现,当我删除所有路由时,服务器启动得非常快。但我对此没有任何解释。

EN

回答 1

Stack Overflow用户

发布于 2019-04-19 10:56:08

看到YAML中的"resources:{}“了吗?您忘记指定它,这很可能是原因。我从来没有使用过Rancher,但在Kubernetes中,如果你不指定资源,它会为你指定资源,而且很可能会使用一些较低的值。

因此您应该为您的应用程序指定资源。下面是一个简单的例子:

resources:
  requests:
    memory: "8Gi"
    cpu: "2"
  limits:
    memory: "8Gi"
    cpu: "2"

有关containers计算资源的更多信息,请单击此处:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55754335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档