3

Google course on Kubernetes proposes:

$ kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080  
  kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. 
  Use kubectl run --generator=run-pod/v1 or kubectl create instead.

Now I'm trying to use the new syntax:

$ kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1  
$ kubectl expose deployment/kubernetes-bootcamp --port=8080 type=NodePort

I'm sure that they are not the same. Could anyone help me?

Rr

Not duplicate: I'm asking about equivalence of two commands

Riccardo79
  • 954
  • 4
  • 17
  • 35
  • Possible duplicate of [kubernetes - kubectl run vs create and apply](https://stackoverflow.com/questions/48015637/kubernetes-kubectl-run-vs-create-and-apply) – Charlie Sep 25 '19 at 07:01
  • 1
    Sorry, I would know how to set "--port" with the "create deployment" way. The service on 8080 is for the node, I need it at container level – Riccardo79 Sep 25 '19 at 07:11
  • What is your problem? What is the error? – Charlie Sep 25 '19 at 07:24
  • With the first command, the deployment creates a POD with a container port on TCP.8080. I don't know how to do this with the "create deployment" command. – Riccardo79 Sep 25 '19 at 07:45
  • What did you try? What was the result? – Charlie Sep 25 '19 at 07:50
  • "create deployment" has not "--port" param. The alternative is to manually edit the deployment and then to add the container port. Is there a fastest way? – Riccardo79 Sep 25 '19 at 08:00
  • Of course. Deployment cannot have a port. It is only about what port the container exposes. It is the service you are exposing the deployment through which should decide the port. I will add an answer. – Charlie Sep 25 '19 at 09:18
  • 2
    This question is not duplicate and an [answer](https://stackoverflow.com/a/48020843/11602913) mentioned here is not an exact answer neither to this or [that](https://stackoverflow.com/q/48015637/11602913) question. Putting aside the demagogy "imperative vs declarative" it ought to be said that the fact that the `kubectl run` creates Deployment like the `kubectl create deployment` does is confusing for newbies. `kubectl` developers admit it in the "apologizing" error message "kubectl run ... is DEPRECATED... Use ... kubectl create instead". @Riccardo79, thanks for raising this topic. – mebius99 Mar 31 '20 at 18:01

2 Answers2

4

You can see what these commands do by executing a dry-run and inspecting the generated manifests:

kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080 \
  --dry-run -o yaml > kubectl-run.yaml

And

kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 \
  --dry-run -o yaml >kubectl-create.yaml

The only substantial difference between the two manifests is that there is no ports field in kubectl-create.yaml.

The kubectl create deployment command has no option for specifying any exposed ports of the container, so there's probably no easy way to achieve exactly the same output as kubectl run with a kubectl create command.

The kubectl expose command that you're using just creates a Service resource and doesn't modify the Deployment resource.

In general, kubectl create <resource> commands are rather limited. They're mostly useful to either create a resource with only default settings, or to create the base for a manifest with --dry-run -o yaml that you can then customise.

weibeld
  • 13,643
  • 2
  • 36
  • 50
  • 1
    Ok. This means that I need to manually edit the deployment and add the container port. The service is not useful at all in this case – Riccardo79 Sep 25 '19 at 11:58
  • Exactly, if you want to achieve the same Deployment manifest as with `kubectl run`. You will probably need the Service later too, if you want to make the Pods in your Deployment accessible. – weibeld Sep 25 '19 at 12:15
3

Deployments has no --port option as you are guessing correctly. It is the port the container listen to, which makes the difference.

The service you use to expose the deployment should say which container port should be exposed - and to which port of the service it should map the container port to.

$ kubectl expose deployment/kubernetes-bootcamp --port=80  --container-port=8080 type=NodePort

The above command exposes the bootcamp via port 80.

Charlie
  • 22,886
  • 11
  • 59
  • 90