favok20149
favok20149

Reputation: 539

How to create K8S deployment in specific namespace?

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?

Upvotes: 25

Views: 53150

Answers (3)

Dávid Molnár
Dávid Molnár

Reputation: 11533

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"

Upvotes: 47

Iakovos Belonias
Iakovos Belonias

Reputation: 1373

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

Upvotes: 3

Oleg Butuzov
Oleg Butuzov

Reputation: 5395

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

Upvotes: 18

Related Questions