2

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?

0

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, how can I define the containerPort to 8080 in this deployment? is it like --containerPort=8080 at the end of the above command?
You can't define ports using create deployment. Use kubectl run instead.
what is --dry-run here?
you can get meaning of option keys by adding --help to your kubectl
1
  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

4 Comments

Thank you, how can I define the containerPort to 8080 in this deployment? is it like --containerPort=8080 at the end of the above command?
I think it's better to leave the -n(or --namespace) flag for the end
If I where you, I would do it inside the yaml file
So that means, we should edit the yaml file with containerPort manually and we can't set it using the one line of command?
0

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

5 Comments

Thank you, how can I define the containerPort to 8080 in this deployment? is it like --containerPort=8080 at the end of the above command?
kubectl -n test edit deploy test and also change port in service that I've posted before to 8080. Also please use correct labels for selector
How can I do this with kubectl commands without really writing a yaml?
You can use kubectl edit command as I metioned in previous comment
even with edit it is a manual work. I need to do this just with a single command of line and an enter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.