我有一个模板,它基本上是一个用于在pod中运行kubectl的实用程序容器。
我想要做的是,能够对同一模板进行多次部署,使用不同的名称,例如" utilitypod-randomID"。
有没有办法通过kubectl和一些shell脚本或类似的东西来做到这一点?
当前模板如下所示:
apiVersion: v1
kind: Pod
metadata:
name: utilitypod
namespace: blah-dev
labels:
purpose: utility-pod
spec:
containers:
- name: utilitypod
image: blahblah/utilitypod:latest
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 28800; done;" ]
env:
- name: KUBERNETES_SERVICE_HOST
value: "api.dev.blah.internal"
- name: KUBERNETES_SERVICE_PORT
value: "443"
答案 0 :(得分:10)
您可以将name
替换为generateName
,后者会添加随机后缀。您的模板将如下所示:
apiVersion: v1
kind: Pod
metadata:
generateName: utilitypod-
namespace: blah-dev
labels:
purpose: utility-pod
spec:
containers:
- name: utilitypod
image: blahblah/utilitypod:latest
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 28800; done;" ]
env:
- name: KUBERNETES_SERVICE_HOST
value: "api.dev.blah.internal"
- name: KUBERNETES_SERVICE_PORT
value: "443"
请注意,这只适用于kubectl create -f template.yaml
,而不是apply
,因为apply
会按名称查找资源并尝试比较其定义,但此模板不会包含特定名称。
答案 1 :(得分:0)
由于我想使用它作为某种自动方式快速生成pod和shell,并且能够让多人使用它,我最终创建了一个使用json的powershell脚本:
$randomnumber = Get-Random
$file = ".\temp\utility.$randomnumber.json"
$json = '{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "utilitypod-' + $randomnumber + '",
"namespace": "blahblah-dev",
"labels": {
"purpose": "utility-pod"
}
},
"spec": {
"containers": [
{
"name": "utilitypod",
"image": "blahblah/utilitypod:latest",
"command": [
"/bin/bash",
"-c",
"--"
],
"args": [
"while true; do sleep 28800; done;"
],
"env": [
{
"name": "KUBERNETES_SERVICE_HOST",
"value": "api.dev.blahblah.internal"
},
{
"name": "KUBERNETES_SERVICE_PORT",
"value": "443"
}
]
}
]
}
}' | Out-File $file
Write-Host "Deploying utilty pod, hang on..."
kubectl apply -f $file
Write-Host "Waiting 15 seconds for the pod be deployed"
start-sleep -s 15
kubectl exec -it "utilitypod-$randomnumber" -n blahblah-dev -- /bin/bash
Write-Host "Asking for pod to be deleted"
kubectl delete -f $file
start-sleep -s 1