20

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?

favok20149
  • 489
  • 2
  • 5
  • 6

3 Answers3

35

There are three possible solutions.

  1. Specify namespace in the kubectl apply or create command:
kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml -n my-namespace
  1. Specify namespace in your yaml files:
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: my-deployment
    namespace: my-namespace
  1. Change default namespace in ~/.kube/config:
apiVersion: v1
kind: Config
clusters:
- name: "k8s-dev-cluster-01"
  cluster:
    server: "https://example.com/k8s/clusters/abc"
    namespace: "my-namespace"
mirekphd
  • 4,799
  • 3
  • 38
  • 59
Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55
14

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
Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33
2

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

Iakovos Belonias
  • 1,217
  • 9
  • 25