Kubectl:run
kubectl의 run 명령
Example
kubectl:get 명령으로 확인 결과:
kubectl:describe 명령으로 확인 방법:
그리고 결과:
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
Pod 제거:
컨테이너 접속이 필요하다면
# 컨테이너가 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
port-forward로 임시 포트 오픈
Ctrl+C 하기 전까지 임시 포트가 오픈된다.
NodePort 로 서비스 연결
다음 명령으로 연결한다:
만약 다음과 같은 에러가 출력된다면:
error: couldn't retrieve selectors via --selector flag or introspection: the pod has no labels and cannot be exposed
Selector 가 라벨을 못찾아서 그런거다. 라벨을 pod 에 쓰자:
정상동작 확인 방법
그리고 몇 가지 명령으로 확인해보자. 우선 서비스 확인:
출력:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-svc NodePort 10.43.173.102 <none> 8080:31869/TCP 68s
Endpoints 확인:
출력:
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
라벨 확인:
출력:
POD 상태 확인:
출력:
접속 방법
직관적으로 보면 8080 포트로 직접 연결된것 처럼 보인다:
하지만 실제 동작은:
이다.
| 플래그 | 의미 | 역할 |
| | Pod의 컨테이너 포트 | 최종 목적지 |
| | Service의 클러스터 내부 포트 | 클러스터 안에서 |
| NodePort (자동할당) | 노드에 열리는 포트 (30000-32767) | 호스트에서 접근할 때 사용 |
즉, --port=8080은 호스트 포트가 아니라 클러스터 내부용 포트입니다.
kubectl expose로는 NodePort 번호를 직접 지정할 수 없습니다. NodePort를 지정하고 싶으면 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
저장한 다음 Apply 하자.
이러면 curlhttp://localhost:30080으로 접속 가능합니다. 단, 범위는 30000-32767로 제한됩니다.