我是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时,应考虑一些注意事项:
不应包含需要很长时间才能完成的复杂逻辑。
启动脚本通常很小且简洁。如果你发现
您在初始化容器中添加了太多逻辑,应该考虑
将其一部分移至应用程序容器本身。
除非之前的容器完成,否则不会调用该容器
成功。因此,如果启动任务很长,则可能
考虑将其分为多个步骤,每个步骤均由init处理
容器,以便您知道哪些步骤失败。
(除非您将restartPolicy设置为Never(从不)。重新启动Pod意味着
重新执行所有容器,包括任何初始化容器。
因此,您可能需要确保启动逻辑允许
执行多次而不会导致重复。例如,如果
数据库迁移已经完成,再次执行迁移命令
应该被忽略。
初始化,直到一个或多个依赖项可用。对于
例如,如果您的应用程序依赖于强加API的API
请求速率限制,您可能需要等待一段时间才能
能够从该API接收响应。实施此逻辑
在应用程序容器中可能很复杂;因为它需要
与健康和就绪状态探针结合使用。一种更简单的方法是
正在创建一个初始化容器,等待直到API准备就绪
在成功退出之前。应用程序容器将启动
仅在初始化容器成功完成其工作之后。
容器呢。原因是它们旨在启动和退出
成功,就像乔布斯和CronJobs的举止一样。
您可以利用此功能在
应用程序及其初始化容器。
我发现有关使用它转储数据的唯一方法是关于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:
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的方法,使您可以更轻松地管理和监视有状态的应用程序。已经有许多运营商,例如
通过提供保持PostgreSQL集群正常运行所需的基本功能,它可以自动化并简化在Kubernetes上部署和管理开源PostgreSQL集群的工作。
关于kubernetes - Openshift中的初始化容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63507098/