Hi, During practice tests, in 1 question it asked to create a pod and expose it . . .

Indranil Dey:
Hi,
During practice tests, in 1 question it asked to create a pod and expose it to a service with a port.I created the pod first and then the service but then was not able to expose the pod to the service.
This is the equivalent command : kubectl run httpd --image=httpd:alpine --port=80 --expose
How to do it if we want to create the pod,then service and then expose in steps?
Can someone help?
Thanks,
Indranil

Nuno:
:wave: Hi

What you did with that command was to create a pod and a service of type ClusterIP.

You can:
• Create the pod with --port as you did;
• then do a kubectl expose pod/<PodName> --type <ClusterIP, NodePort, LoadBalancer> (with additional flag, accordingly).
What type of service does that question require?

Nuno:
Can you paste the question here with a print screen?

Indranil Dey:
Thanks for your reply

Indranil Dey:

image.png

Nuno:
Ok. So you did it correctly.

Do you want to do it step by step with different commands, is that it? I think my previous answer helps on that :slightly_smiling_face:

Indranil Dey:
http://Thanks.It|Thanks.It worked:slightly_smiling_face:

Shivesh:
Hi, I am yet to appear for the exam … I am trying to expand my question… We are creating two type of services NodePort and ClusterIP …
Can we create both services with single command as my understanding is that NodePort service should be created via YAML file.

Nuno:
What I usually do is to leverage the imperative command and then add nodePort in YAML.
For example: kubectl expose deployment nginx --type NodePort --port 80 --target-port 80 --dry-run=client -o yaml > svc.yaml.

1 Like

Nuno:
You can also: kubectl create service nodeport my-service --tcp=port:targetPort --node-port=nodePort --dry-run=client -o yaml > svc.yaml
This is new to me :sweat_smile: I always use kubectl expose. With kubectl create service nodeport we need do add the selector in YAML, it seems.