kuberneteskubectl

kubectl create namespace with label


Is there a way to create a namespace with one label using kubectl?

e.g.

kubectl create ns <newnamespace> label=appid:foo12

as opposed to using kubectl apply -f <somefile>


Solution

  • Right away this is not possible, see kubectl help create ns manual. But there are solutions: You can either create the namespace with kubectl and label it right away with kubectl label namespaces <newnamespace> label=appid:foo12 or you could use kubectl to generate the yaml, where you just add the label: kubectl create namespace demo --dry-run=client -o yaml

    If you are really really eager to have it all in one command, you could modify the JSON inline, for example

    kubectl create namespace demo --dry-run=client -o yaml | \
     sed  '/^metadata:/a\ \ labels: {"demo":"true"}' | kubectl apply -f -