Kubectl:run
lastmod 2026-02-26created 2023-03-295 edits
The run command of kubectl
Example
Result checked with the kubectl:get command:
How to check with the kubectl:describe command:
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:
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
The temporary port stays open until you press Ctrl+C.
Connecting a service with NodePort
Connect with the following command:
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:
How to verify it works
Then let's check with a few commands. First, check the service:
Output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-svc NodePort 10.43.173.102 <none> 8080:31869/TCP 68s
Check Endpoints:
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:
Output:
Check POD status:
Output:
How to connect
Intuitively, it looks as if it is connected directly to port 8080:
But what actually happens is:
That is the actual flow.
| Flag | Meaning | Role |
| | Container port of the Pod | Final destination |
| | Cluster-internal port of the Service | Used when accessing as |
| 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.
Then you can connect with curlhttp://localhost:30080. However, the range is limited to 30000-32767.