简体   繁体   English

如何在 curl 中使用原始 YAML 文件来创建 Pod

[英]How to use raw YAML file inside a curl to create Pod

I am trying to create a Pod in Kube.netes using curl .我正在尝试使用curl在 Kube.netes 中创建一个 Pod。

This is the YAML:这是 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

I have token with permissions to do it and I wrote the following curl command:我有权限执行此操作的令牌,我编写了以下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}'

Which should be equivalent to the nginx-pod.yaml file.这应该等同于nginx-pod.yaml文件。
The YAML is ok because when I run kubectl create -f nginx.pod.yaml it creates it. YAML 没问题,因为当我运行kubectl create -f nginx.pod.yaml时,它会创建它。
But when I tried to run it with curl I received:但是当我尝试使用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

I tried to change the Content-Type to Content-type: text/x-yaml but it didn't help.我试图将Content-Type更改为Content-type: text/x-yaml但它没有帮助。

Any idea what can be the reason?知道可能是什么原因吗?

One of the errors is regarding the "metadata.name" field.其中一个错误与“metadata.name”字段有关。

The payloads don't seem equivalent. 有效载荷似乎并不等效。

This is the JSON payload you are sending: 这是您要发送的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
}

Your payload should be something like this. 您的有效载荷应该是这样的。 I created this with kubectl convert -f <yaml.file> -o=json , which is what I believe kubectl does before sending the payload. 我使用kubectl convert -f <yaml.file> -o=json创建了这个,我相信kubectl在发送有效负载之前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": {}
}

Tried this and it works fine for me: 尝试了一下,对我来说很好用:

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

确保将内容类型设置为application / yaml,并在yaml中使用--binary-data…--data删除换行符

Each line of a yaml file contains line breaks ( \n ). yaml 文件的每一行都包含换行符 ( \n )。 Curl removes line breaks when use with the -d option which change the actual yaml format. Curl 在与更改实际 yaml 格式的-d选项一起使用时删除换行符。 To avoid this, use --data-binary option instead of -d option when using yaml files as data.为避免这种情况,在将 yaml 个文件用作数据时,请使用--data-binary选项而不是-d选项。 Further its better to use Content-Type as application/x-yaml.此外,最好将 Content-Type 用作 application/x-yaml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM