我正在尝试使用curl
在Kubernetes中创建一个Pod。
这是YAML:
cat > nginx-pod.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
name: nginx1
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
EOF
我具有有权操作的令牌,并且我写了以下curl
命令:
curl -k -v -X POST -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json;charset=utf-8' https://127.0.0.1:6443/api/v1/namespaces/default/pods --data '{"name":"","namespace":"default","content":"apiVersion: v1\nkind: Pod\nmetadata:\n name: nginx1\nspec:\n containers:\n - name: nginx\n image: nginx:1.7.9\n ports:\n - containerPort: 80\n","validate":true}'
应与nginx-pod.yaml
文件等效。
YAML是可以的,因为当我运行kubectl create -f nginx.pod.yaml
时,它会创建它。
但是,当我尝试使用curl
运行它时,我收到了:
< Content-Length: 617
<
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "Pod \"\" is invalid: [metadata.name: Required value: name or generateName is required, spec.containers: Required value]",
"reason": "Invalid",
"details": {
"kind": "Pod",
"causes": [
{
"reason": "FieldValueRequired",
"message": "Required value: name or generateName is required",
"field": "metadata.name"
},
{
"reason": "FieldValueRequired",
"message": "Required value",
"field": "spec.containers"
}
]
},
"code": 422
* Connection #0 to host 127.0.0.1 left intact
我尝试将Content-Type
更改为Content-type: text/x-yaml
,但没有帮助。
任何想法可能是什么原因?
错误之一与“ metadata.name”字段有关。
答案 0 :(得分:0)
有效载荷似乎不相等。
这是您要发送的JSON负载:
{
"name": "",
"namespace": "default",
"content": "apiVersion: v1\nkind: Pod\nmetadata:\n name: nginx1\nspec:\n containers:\n - name: nginx\n image: nginx:1.7.9\n ports:\n - containerPort: 80\n",
"validate": true
}
您的有效载荷应该是这样的。我是用kubectl convert -f <yaml.file> -o=json
创建的,这是我相信kubectl
在发送有效负载之前所做的。
{
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "nginx1",
"creationTimestamp": null
},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx:1.7.9",
"ports": [
{
"containerPort": 80,
"protocol": "TCP"
}
],
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "IfNotPresent"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {},
"schedulerName": "default-scheduler"
},
"status": {}
}
试过这个,对我来说很好:
curl -k -v -X POST -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json;charset=utf-8' https://127.0.0.1:6443/api/v1/namespaces/default/pods -d@payload.json
答案 1 :(得分:0)
确保将内容类型设置为application / yaml,并在yaml中使用--binary-data…--data删除换行符