Skip to content
Search

Kubectl:run

lastmod 2026-02-26created 2023-03-295 edits

The run command of kubectl

Example

$ kubectl run nginx --image=nginx
pod/nginx created

Result checked with the kubectl:get command:

$ kubectl get pods
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          11s

How to check with the kubectl:describe command:

$ kubectl describe pods/nginx

And the result:

Name:         nginx
Namespace:    default
Priority:     0
Node:         yourid-sf314-42/192.168.1.209
Start Time:   Wed, 29 Mar 2023 22:33:56 +0900
Labels:       run=nginx
Annotations:  <none>
Status:       Running
IP:           10.42.0.44
IPs:
  IP:  10.42.0.44
Containers:
  nginx:
    Container ID:   containerd://657c9fc2c68166949dccfd3dd3541f438abbce0a9558c2271c44edbb13cb336e
    Image:          nginx
    Image ID:       docker.io/library/nginx@sha256:2ab30d6ac53580a6db8b657abf0f68d75360ff5cc1670a85acb5bd85ba1b19c0
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Wed, 29 Mar 2023 22:34:09 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-5jgd5 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-5jgd5:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  46s   default-scheduler  Successfully assigned default/nginx to yourid-sf314-42
  Normal  Pulling    46s   kubelet            Pulling image "nginx"
  Normal  Pulled     34s   kubelet            Successfully pulled image "nginx" in 12.378215819s
  Normal  Created    34s   kubelet            Created container nginx
  Normal  Started    33s   kubelet            Started container nginx

Delete the Pod:

$ kubectl delete pods/nginx
pod "nginx" deleted

If you need to access the container

# 컨테이너가 1개일 경우
kubectl exec -it <pod-name> -- /bin/bash

# 멀티 컨테이너 Pod인 경우 -c 옵션으로 컨테이너 지정:
kubectl exec -it <pod-name> -c <container-name> -- /bin/bash

# 네임스페이스 지정이 필요하면 -n 추가:
kubectl exec -it <pod-name> -n <namespace> -- /bin/bash

Opening a temporary port with port-forward

kubectl port-forward pod/nginx 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80

The temporary port stays open until you press Ctrl+C.

Connecting a service with NodePort

Connect with the following command:

kubectl expose pod nginx --name=nginx-svc --port=8080 --target-port=80 --type=NodePort

If the following error is printed:

error: couldn't retrieve selectors via --selector flag or introspection: the pod has no labels and cannot be exposed

It is because the Selector cannot find a label. Add a label to the pod:

kubectl label pod nginx app=nginx

How to verify it works

Then let's check with a few commands. First, check the service:

kubectl get svc nginx-svc

Output:

NAME        TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
nginx-svc   NodePort   10.43.173.102   <none>        8080:31869/TCP   68s

Check Endpoints:

kubectl get endpoints nginx-svc

Output:

Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME        ENDPOINTS      AGE
nginx-svc   10.42.0.7:80   74s

Check labels:

kubectl get pod nginx --show-labels

Output:

NAME    READY   STATUS    RESTARTS      AGE    LABELS
nginx   1/1     Running   2 (43h ago)   3d5h   app=nginx

Check POD status:

kubectl get pod nginx

Output:

NAME    READY   STATUS    RESTARTS      AGE
nginx   1/1     Running   2 (43h ago)   3d5h

How to connect

Intuitively, it looks as if it is connected directly to port 8080:

Host:8080 → Pod:80

But what actually happens is:

Host:31869 → Service:8080 → Pod:80
(NodePort)        (port)       (targetPort)

That is the actual flow.

Flag

Meaning

Role

--target-port=80

Container port of the Pod

Final destination

--port=8080

Cluster-internal port of the Service

Used when accessing as nginx-svc:8080 from inside the cluster

NodePort (auto-assigned)

Port opened on the node (30000-32767)

Used when accessing from the host

In other words, --port=8080 is not a host port but a port for use inside the cluster.

kubectl expose cannot set the NodePort number directly. If you want to specify the NodePort, you have to write it in YAML:

# node-port.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - port: 8080
      targetPort: 80
      nodePort: 30080

Save it and then Apply it.

kubectl apply -f node-port.yml

Then you can connect with curlhttp://localhost:30080. However, the range is limited to 30000-32767.

See also

author Zer0 sitemap