How to create K8S deployment in specific namespace?

12/10/2019

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
kubernetes
kubernetes-pod

3 Answers

12/10/2019

There are three possible solutions.

  1. Specify namespace in the kubectl 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"
-- Dávid Molnár
Source: StackOverflow

12/10/2019

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
Source: StackOverflow

12/10/2019

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
Source: StackOverflow