kubernetes - Openshift中的初始化容器

标签 kubernetes containers openshift kubernetes-pod openshift-3

我是Openshift的新手,我曾经浏览过Openshift网站以获取更多详细信息,但想知道是否有人部署了init容器。
我想用它来从数据库中获取转储,并借助init容器将其还原到新版本
我们正在使用postgres数据库
任何帮助,将不胜感激。
谢谢!

最佳答案

I want to use that to take dump from database and restore it to new version of it with the help of init container


我会说您应该使用运算符而不是initContainer。看看下面的Init Containers Design Considerations
创建initcontainer时,应考虑一些注意事项:
  • 它们总是在Pod中的其他容器之前执行。所以他们
    不应包含需要很长时间才能完成的复杂逻辑。
    启动脚本通常很小且简洁。如果你发现
    您在初始化容器中添加了太多逻辑,应该考虑
    将其一部分移至应用程序容器本身。
  • 初始化容器按顺序启动和执行。初始化
    除非之前的容器完成,否则不会调用该容器
    成功。因此,如果启动任务很长,则可能
    考虑将其分为多个步骤,每个步骤均由init处理
    容器,以便您知道哪些步骤失败。
  • 如果任何初始化容器失败,则重新启动整个Pod
    (除非您将restartPolicy设置为Never(从不)。重新启动Pod意味着
    重新执行所有容器,包括任何初始化容器。
    因此,您可能需要确保启动逻辑允许
    执行多次而不会导致重复。例如,如果
    数据库迁移已经完成,再次执行迁移命令
    应该被忽略。
  • 初始化容器是延迟应用程序的理想选择
    初始化,直到一个或多个依赖项可用。对于
    例如,如果您的应用程序依赖于强加API的API
    请求速率限制,您可能需要等待一段时间才能
    能够从该API接收响应。实施此逻辑
    在应用程序容器中可能很复杂;因为它需要
    与健康和就绪状态探针结合使用。一种更简单的方法是
    正在创建一个初始化容器,等待直到API准备就绪
    在成功退出之前。应用程序容器将启动
    仅在初始化容器成功完成其工作之后。
  • Init容器不能将运行状况和就绪探针用作应用程序
    容器呢。原因是它们旨在启动和退出
    成功,就像乔布斯和CronJobs的举止一样。
  • 同一Pod上的所有容器共享相同的卷和网络。
    您可以利用此功能在
    应用程序及其初始化容器。

  • 我发现有关使用它转储数据的唯一方法是关于mysqlt的example,也许它可以指导您如何使用postgresql。

    In this scenario, we are serving a MySQL database. This database is used for testing an application. It doesn’t have to contain real data, but it must be seeded with enough data so that we can test the application's query speed. We use an init container to handle downloading the SQL dump file and restore it to the database, which is hosted in another container. This scenario can be illustrated as below:


    enter image description here

    The definition file may look like this:

    apiVersion: v1
    kind: Pod
    metadata:
      name: mydb
      labels:
        app: db
    spec:
      initContainers:
        - name: fetch
          image: mwendler/wget
          command: ["wget","--no-check-certificate","https://sample-videos.com/sql/Sample-SQL-File-1000rows.sql","-O","/docker-entrypoint-initdb.d/dump.sql"]
          volumeMounts:
            - mountPath: /docker-entrypoint-initdb.d
              name: dump
      containers:
        - name: mysql
          image: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "example"
          volumeMounts:
            - mountPath: /docker-entrypoint-initdb.d
              name: dump
      volumes:
        - emptyDir: {}
          name: dump
    

    The above definition creates a Pod that hosts two containers: the init container and the application one. Let’s have a look at the interesting aspects of this definition:

    The init container is responsible for downloading the SQL file that contains the database dump. We use the mwendler/wget image because we only need the wget command. The destination directory for the downloaded SQL is the directory used by the MySQL image to execute SQL files (/docker-entrypoint-initdb.d). This behavior is built into the MySQL image that we use in the application container. The init container mounts /docker-entrypoint-initdb.d to an emptyDir volume. Because both containers are hosted on the same Pod, they share the same volume. So, the database container has access to the SQL file placed on the emptyDir volume.



    另外,关于最佳实践,我建议看一下kubernetes运营商,据我所知,这是管理kubernetes中数据库的最佳实践。
    如果您不熟悉运算符,建议您在youtube上以kubernetes documentation和这个简短的video开头。
    运算符是打包Kubernetes的方法,使您可以更轻松地管理和监视有状态的应用程序。已经有许多运营商,例如
  • Crunchy Data PostgreSQL Operator
  • Postgres Operator

  • 通过提供保持PostgreSQL集群正常运行所需的基本功能,它可以自动化并简化在Kubernetes上部署和管理开源PostgreSQL集群的工作。

    关于kubernetes - Openshift中的初始化容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63507098/

    相关文章:

    windows - 查找容器 id 并停止 docker 容器的 bat 脚本语法是什么

    c++ - 为什么 string::data() 不提供可变字符 *?

    java - 如何在 openshift 上将 log4j 连接到 loggly?

    openshift - 在Openshift v3中使用自定义域

    nginx - K8s Ingress服务返回503,在Pod日志中什么也没有

    kubernetes - Kubernetes中的节点上仅允许一个类型的Pod

    Kubernetes 1.9 支持的 Docker 版本

    kubernetes - 无法在 K8S 集群中创建 Prometheus

    azure - 将使用 buildah 构建的镜像从 Azure 容器注册表部署到 Azure 容器实例

    node.js - Openshift 的 MongoDB 大小限制是多少?