I am using kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml
to create deployment.
I want to create deployment in my namespace examplenamespace
.
How can I do this?
There are three possible solutions.
kubectl
command:kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml -n my-namespace
yaml
files: apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
namespace: my-namespace
~/.kube/config
:apiVersion: v1
kind: Config
clusters:
- name: "k8s-dev-cluster-01"
cluster:
server: "https://example.com/k8s/clusters/abc"
namespace: "my-namespace"
By adding -n namespace
to command you already have. It also works with other types of resources.
kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml -n namespacename
First you need to create the namespace likes this
kubectl create ns nameOfYourNamespace
Then you create your deployment under your namespace
kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml -n examplenamespace
The ns at
kubectl create ns nameOfYourNamespace
stands for namespace
The -n
kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml -n examplenamespace
stands for --namespace
So you first create your namespace in order Kubernetes know what namespaces dealing with.
Then when you are about to apply your changes you add the -n flag that stands for --namespace so Kubernetes know under what namespace will deploy/ create the proper resources