问题描述
我最近了解到 Kubernetes具有称为初始化容器的功能.太好了,因为我可以使用此功能来等待我的postgres服务并在运行Web应用程序服务之前创建/迁移数据库.
I learned recently that Kubernetes has a feature called Init Containers. Awesome, because I can use this feature to wait for my postgres service and create/migrate the database before my web application service runs.
但是,似乎只能在Pod yaml文件中配置初始化容器.有什么办法可以通过Deployment yaml文件执行此操作?还是我必须选择?
However, it appears that Init Containers can only be configured in a Pod yaml file. Is there a way I can do this via a Deployment yaml file? Or do I have to choose?
推荐答案
为避免混淆,请回答您的特定问题.我同意oswin,您可能想考虑另一种方法.
To avoid confusion, ill answer your specific question. i agree with oswin that you may want to consider another method.
是的,您可以在部署中使用init容器.这是使用旧样式(1.6之前的版本)的示例,但应该可以使用
Yes, you can use init containers with a deployment. this is an example using the old style (pre 1.6) but it should work
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: 'nginx'
spec:
replicas: 1
selector:
matchLabels:
app: 'nginx'
template:
metadata:
labels:
app: 'nginx'
annotations:
pod.beta.kubernetes.io/init-containers: '[
{
"name": "install",
"image": "busybox",
"imagePullPolicy": "IfNotPresent",
"command": ["wget", "-O", "/application/index.html", "http://kubernetes.io/index.html"],
"volumeMounts": [
{
"name": "application",
"mountPath": "/application"
}
]
}
]'
spec:
volumes:
- name: 'application'
emptyDir: {}
containers:
- name: webserver
image: 'nginx'
ports:
- name: http
containerPort: 80
volumeMounts:
- name: 'application'
mountPath: '/application'
这篇关于Kubernetes部署和初始化容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!