Hi Team, In the CKA course practice test imperative commands. There is a questio . . .

Ganesh B.G:
Hi Team, In the CKA course practice test imperative commands. There is a question to create rcustom-nginx pod and expose container port 8080.

Answer says: kubectl run custom-nginx --image=nginx --port=8080.

I see that this is different from

kubectl run custom-nginx --image=nginx
kubectl expose pod custom-nginx --port=8080 --name any-name

Could someone help me understand the difference on what is going on above?

Tej Singh Rana:
Hello, @Ganesh B.G
First command will create a pod…
Second command will create a clusterIP type service called any-name for custom-nginx pod.

Tej Singh Rana:
Reference -
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#expose

Ganesh B.G:
Hi Tej, Thanks for responding, what is the meaning of kubectl run custom-nginx --image=nginx --port=8080 ? arent they same?

Tej Singh Rana:
No, they are not same.

Ganesh B.G:
could you link me to a doc with some explanation regarding kubectl run custom-nginx --image=nginx --port=8080

Tej Singh Rana:
It’s available in the same page which I shared earlier.

Tej Singh Rana:
First command is creating a Pod and second command is creating a Service.

Ganesh B.G:
Sorry I am a bit confused. My ask is.

  1. kubectl run custom-nginx --image=nginx --port=8080
2. kubectl run custom-nginx --image=nginx
kubectl expose pod custom-nginx --port=8080 --name any-name

aren’t 1 and 2 the same?

Tej Singh Rana:
If you wanna see more difference then please use these commands with --dry-run=client -oyaml

kubectl run custom-nginx --image=nginx --port=8080 --dry-run=client -oyaml


But we need to create a pod first for this expose command.

kubectl expose pod custom-nginx --port=8080 --name any-name --dry-run=client -oyaml

Ganesh B.G:
Got it, will try them :thumbsup:

Paul:
first one creates the pod then opens the containerport of the pod of 8080. so this actually allows you to access the pod ip with the the containerport. The terminology in kubernetes when you say to “expose” is to actually create a service that will make the pods selected accessible through that service.

Ganesh B.G:
Got it, so the first one directly allows to interact with the app on pod on port 8080 where as the second one does the same but through service give there is only one pod.

Ganesh B.G:
Thanks @Paul @Tej Singh Rana

Hi,
My query is regarding the first part where we create the pod. What I did was,
created pod on port 80, this failed the answer for obvious reason of wrong port.
Then I tried to edit it using “kubectl edit …”, after editing it didn’t allow me to save the change of port to 8080.

Could anyone explain why? Are there only certain fields that we can edit with edit command?

Hi @pramod-suryawanshi ,
You will see these lines in form of comment (#) when you run the command: kubectl edit pod <pod-name>.

Pod updates may not change fields other than 
spec.containers[*].image, 
spec.initContainers[*].image, 
spec.activeDeadlineSeconds, 
spec.tolerations (only additions to existing tolerations) 
spec.terminationGracePeriodSeconds (allow it to be set to 1 if it was previously negative)

Apart from these fields, if you try to add any new fields or try to update the existing fields values will not accept.

You can try in the labs and share your feedback.

Regards,
KodeKloud Support