How do I create a deployment with a port?

This

kubectl run hello --image=gcr.io/google-samples/hello-app:1.0 \
--labels=app=hello  --port=8080

gives an error message “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.”

Using the recommended --generator switch creates a Pod, not a Deployment. Also, there is no equivalent generator run-deployment.

kubectl create deployment --image=gcr.io/google-samples/hello-app:1.0 --labels=app=hello --port=8080 gives an error that port is not supported.

I understand that I can use a yaml; and I can expose a Deployment on a port using a Service. But what is the simple one-line command for launching a Deployment?

As port has no effect on what’s running inside the pods and how to reach them (it’s purely informal), you could just omit this parameter. If you need to reach your pod on port 8080 from another pod, you still need to create a service :

kubectl create deployment hello --image=gcr.io/google-samples/hello-app:1.0 -o yaml --dry-run
kubectl expose deployment hello --port=80 --target-port=8080 -o yaml --dry-run
1 Like

Thank you. So what purpose does --port have? I tried it and I guess it sets the container port: The value from kubectl pod describe hello…` includes

...
Containers:
  hello-web:
 ...
    Port:           8080/TCP

So, how do I arrange to have that same port defined in the container using the kubectl create deployment ... approach?

It looks like you can’t, if you don’t want to write your own YAML file. But that “container port” is useless as-is. The only usecase I can found (other than human awareness) is when you give a name to such a port :

---
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
          ports:
            - name: myapp-http
              containerPort: 8080
[...]

Then, instead of specifying a numbered port when creating a service, you can refer to the named port. But that’s out-of-scope of what the kubectl create deployement ... and kubectl expose ... can do.

Just don’t add the ports definition in your container specs, if you can cope with it.

Reference:

$ kubectl explain pod.spec.containers.ports
1 Like

if you want your service to be a nodeport:
kubectl expose deployment deployment_name --name=service_name --port=80 --type=NodePort --protocol=TCP

k run nginx --image=nginx --port=80
this should deploy pod with container port 80 and once the pod is created you can expose it
k expose po nginx --port=9090 --name=nginx-service
your pod is exposed to external world via nginx-service on port 9090. It will be a clusterip service so you can use the clusterip to map it to your loadbalancer / istio port and forward your calls or else use node port service by doing k edit svc nginx-service
and replace the service