Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

json - Kubernetes / kubectl - "A container name must be specified" but seems like it is?

I'm debugging log output from kubectl that states:

Error from server (BadRequest): a container name must be specified for pod postgres-operator-49202276-bjtf4, choose one of: [apiserver postgres-operator]

OK, so that's an explanatory error message, but looking at my JSON template it ought to just create both containers specified, correct? What am I missing? (please forgive my ignorance.)

I'm using just a standard kubectl create -f command to create the JSON file within a shell script. The JSON deployment file is as follows:

{
    "apiVersion": "extensions/v1beta1",
    "kind": "Deployment",
    "metadata": {
        "name": "postgres-operator"
    },
    "spec": {
        "replicas": 1,
        "template": {
            "metadata": {
                "labels": {
                    "name": "postgres-operator"
                }
            },
            "spec": {
                "containers": [{
                    "name": "apiserver",
                    "image": "$CCP_IMAGE_PREFIX/apiserver:$CO_IMAGE_TAG",
                    "imagePullPolicy": "IfNotPresent",
                    "env": [{
                        "name": "DEBUG",
                        "value": "true"
                    }],
                    "volumeMounts": [{
                        "mountPath": "/config",
                        "name": "apiserver-conf",
                        "readOnly": true
                    }, {
                        "mountPath": "/operator-conf",
                        "name": "operator-conf",
                        "readOnly": true
                    }]
                }, {
                    "name": "postgres-operator",
                    "image": "$CCP_IMAGE_PREFIX/postgres-operator:$CO_IMAGE_TAG",
                    "imagePullPolicy": "IfNotPresent",
                    "env": [{
                        "name": "DEBUG",
                        "value": "true"
                    }, {
                        "name": "NAMESPACE",
                        "valueFrom": {
                            "fieldRef": {
                                "fieldPath": "metadata.namespace"
                            }
                        }
                    }, {
                        "name": "MY_POD_NAME",
                        "valueFrom": {
                            "fieldRef": {
                                "fieldPath": "metadata.name"
                            }
                        }
                    }],
                    "volumeMounts": [{
                        "mountPath": "/operator-conf",
                        "name": "operator-conf",
                        "readOnly": true
                    }]
                }],
                "volumes": [{
                    "name": "operator-conf",
                    "configMap": {
                        "name": "operator-conf"
                    }
                }, {
                    "name": "apiserver-conf",
                    "configMap": {
                        "name": "apiserver-conf"
                    }
                }]
            }
        }
    }
}
question from:https://stackoverflow.com/questions/47129376/kubernetes-kubectl-a-container-name-must-be-specified-but-seems-like-it-is

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If a pod has more than 1 containers then you need to provide the name of the specific container.

in your case, There is a pod (postgres-operator-49202276-bjtf4) which has 2 containers (apiserver and postgres-operator ). following commands will provide logs for the specific containers

kubectl logs deployment/postgres-operator -c apiserver


kubectl logs deployment/postgres-operator -c postgres-operator

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...