使用 kubectl run 创建带有卷的 kubernetes pod [英] Create kubernetes pod with volume using kubectl run
问题描述
我知道您可以使用 kubectl run 创建一个带有 Deployment/Job 的 pod.但是是否可以创建一个附加了卷的文件?我尝试运行此命令:
I understand that you can create a pod with Deployment/Job using kubectl run. But is it possible to create one with a volume attached to it? I tried running this command:
kubectl run -i --rm --tty ubuntu --overrides='{ "apiVersion":"batch/v1", "spec": {"containers": {"image": "ubuntu:14.04", "volumeMounts": {"mountPath": "/home/store", "name":"store"}}, "volumes":{"name":"store", "emptyDir":{}}}}' --image=ubuntu:14.04 --restart=Never -- bash
但是该卷没有出现在交互式 bash 中.
But the volume does not appear in the interactive bash.
有没有更好的方法来创建一个带有可以附加到的卷的 Pod?
Is there a better way to create a pod with volume that you can attach to?
推荐答案
您的 JSON 覆盖指定不正确.不幸的是,kubectl run 只是忽略了它不理解的字段.
Your JSON override is specified incorrectly. Unfortunately kubectl run just ignores fields it doesn't understand.
kubectl run -i --rm --tty ubuntu --overrides='
{
"apiVersion": "batch/v1",
"spec": {
"template": {
"spec": {
"containers": [
{
"name": "ubuntu",
"image": "ubuntu:14.04",
"args": [
"bash"
],
"stdin": true,
"stdinOnce": true,
"tty": true,
"volumeMounts": [{
"mountPath": "/home/store",
"name": "store"
}]
}
],
"volumes": [{
"name":"store",
"emptyDir":{}
}]
}
}
}
}
' --image=ubuntu:14.04 --restart=Never -- bash
为了调试这个问题,我运行了你指定的命令,然后在另一个终端中运行:
To debug this issue I ran the command you specified, and then in another terminal ran:
kubectl get job ubuntu -o json
从那里您可以看到实际的作业结构与您的 json 覆盖不同(您缺少嵌套的模板/规范,并且卷、volumeMounts 和容器需要是数组).
From there you can see that the actual job structure differs from your json override (you were missing the nested template/spec, and volumes, volumeMounts, and containers need to be arrays).
这篇关于使用 kubectl run 创建带有卷的 kubernetes pod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!