I am working on the imperative commands practice test in the CKA course. There . . .

Tom H:
I am working on the imperative commands practice test in the CKA course.

There was a task:

“Create a new pod called custom-nginx using the nginx image and expose it on container port 8080.”

I tried to do this with:

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

But this was not an accepted answer.

I see the correct solution given is:

kubectl run custom-nginx --image=nginx --port=8080

Obviously the given solution is a more efficient as it’s just one line.

But my question would be, does the end result differ between these two methods?
I notice that the given solution doesn’t create a service. How come?

K8s_Member:
Firstly, I think you should clarify between kubectl run ... --port=8000 and kubectl expose pod ... --port=8000

  1. kubectl run ... --port=8000
    a. Start a pod and let the container expose port 8000
    b. At this time, you can’t access it from outside. You have to manually create a new service with --ports configuration values like .*spec.ports.targetPort = 8000 (*It’s actually containerPort)
  2. kubectl expose pod ... --port=8000
    a. Create a service for a pod valid-pod, which serves on port 8000.
    b. You can access service now.
    Maybe you’re confuse with the requirement of question is “expose” keyword.

Tom H:
That’s very helpful, thank you. Think I get it now!

Alistair Mackay:
You can also create the pod and the service with a single command which is fine if you’re asked to expose as a service and no requirements to set labels to any specific values

kubectl run custom-nginx --image=nginx --port=8080 --expose