How to pass namespace in Kubernetes create deployment command

12/11/2019

I am trying to define the namespace name when executing the kubectl create deployment command?

This is what I tried:

kubectl create deployment test --image=banu/image1 namespace=test

and this doesn't work.

And I want to expose this deployment using a ClusterIP service within the cluster itself for that given namespace How can I do that using kubectl command line?

-- IT17157124 Jayarathna K.M.J.B.
kubernetes

3 Answers

12/11/2019
  1. You need to create a namespace like this
kubectl create ns test

ns stands for namespace, so with kubectl you say you want to create namespace with name test

  1. Then while you creating the deployment you add the namespace you want
kubectl create deployment test --image=banu/image1 -n test

Where flag -n stands for namespace, that way you say to Kubernetes that all resources related to that deployment will be under the test namespace

  1. In order to see all the resources under a specific namespace
kubectl get all -n test
--namespace and -n is the same things
-- Iakovos Belonias
Source: StackOverflow

12/11/2019

Use -n test instead of namespace=test

Sample with nginx image:

$ kubectl create deployment nginx --image=nginx -n test
deployment.apps/nginx created
$ kubectl get deploy -n test
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           8s

In second case you need to create service and define labels from deployment. You can find correct labels by runnig something like: kubectl -n test describe deploy test |grep Labels: and apply service like:

apiVersion: v1
kind: Service
metadata:
  name: test-svc
  namespace: test
spec:

  ports:
  - name: test
    port: 80 # Change this port
    protocol: TCP
  type: ClusterIP
  selector:
    # Here you need to define output from previous step
-- Oles Rid
Source: StackOverflow

12/11/2019

You can specify either -n or --namespace options.

kubectl create deployment test --image=nginx --namespace default --dry-run -o yaml and see result deployment yaml.

Using kubectl run

kubectl run test --namespace test --image nginx  --port 9090 --dry-run -o yaml
-- Oleg Butuzov
Source: StackOverflow