【问题标题】:Rails is slow in Rancher based Docker environment with PostgresqlRails 在带有 Postgresql 的基于 Rancher 的 Docker 环境中运行缓慢
【发布时间】:2019-04-18 22:15:27
【问题描述】:

我们使用的是基于 Rancher 的 Docker 环境。 服务器装备精良,我不觉得有任何性能问题。 但是启动性能和控制台的性能非常缓慢。

  • 2 分钟 启动 puma-Server(在我的本地 VM 上:10 秒)
  • 1 分 56 秒访问控制台 rails c(在我的本地 VM 上:7 秒)
  • 17 分钟!使用 rails 控制台中的“oneliner”从 100000 条记录中更新 70000 条记录。 每秒仅 70 条记录。选择本身只需要 31 毫秒。
Object.where(view: 0).each { |obj| obj.update_columns(view: 1) }

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

我还比较了我的本地 VM(2 核 3GB RAM)

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 和内存在 30% - 一切都很安静。
上述“oneliner”的进程使用了​​一个内核的 5.7%
CPU:Intel® Core™ i7-6700 四核
RAM: 64 GB DDR4 RAM
HDD:1TB SSD

环境
操作系统:Ubuntu 18.04.1
Ruby:2.6.6
Rails:5.2.4.2
PostgreSQL:10.12

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

Rancher:v2.2.3
用户界面:v2.2.41
Helm: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。

Dockerfiles

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

Rancher 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

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

【问题讨论】:

  • 你在集群上运行 docker build 吗?集群位于何处?集群中有多少个节点? Rails pod 是否与 PostGreSQL 服务在同一节点上运行?
  • @leodotcloud 是的,docker build 正在运行。一个节点上只有一个集群。 Rails pod 与 PostgreSQL 在同一节点上运行。
  • 集群节点是在云中运行还是在您的笔记本电脑上运行的虚拟机?另一个问题,为什么你每次都要做一个 docker build?你不能构建它并将其推送到注册表以在 kubernetes 集群上使用吗?
  • 集群节点是一个专用的网络服务器。我的本地虚拟机是一个没有牧场主和码头工人的环境。只是一个运行 rails 服务器的 ubuntu。根据您的最后一个问题,这与服务器本身的速度有关吗?
  • 您可以通过 SSH 连接到您的专用网络服务器并执行相同的 docker build 以本地检查时间吗?您的虚拟机和网络服务器是两个不同的环境。网络连接不同,设置可能不同,除非您使用相同的 VM 映像或某些进程具有相同的环境。是否有其他工作负载在该网络服务器上运行可能会影响 docker 构建?

标签: ruby-on-rails postgresql docker rancher


【解决方案1】:

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

因此,您应该为您的应用程序指定资源。简单的例子如下:

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

在此处了解有关容器计算资源的更多信息:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/

【讨论】:

  • 不幸的是,这对性能没有影响。仍然与没有资源限制的相同 Pod 的加载时间相同。
  • 这真的很奇怪。那么在 Kubernetes 和本地在同一个节点上运行应用程序的区别有那么大吗?
  • 本地是指在我的本地机器上,在具有 zust 3GB RAM 和 2 个 CPU 的 VM 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
相关资源
最近更新 更多