ClusterIP
kubectl expose deployment nginx --name nginx-svc --port 8080 --target-port 80
NodePort
kubectl expose deployment nginx --name nginx-svc --port 8081 --target-port 80 --type NodePort
Different Ports
-
target-port
-- container port- Where your pod/container listens
k get endpoints
-
port
-- service's internal port- access the svc from other port
k get svc
-
NodePort
-- expose to external/all nodes-
k get endpoints
ork get svc
-
controlplane $ k get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE apache-svc ClusterIP 10.104.249.75 <none> 8080/TCP 2m11s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d8h nginx-svc NodePort 10.97.210.100 <none> 8081:32433/TCP 3s controlplane $ k get node -owide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME controlplane Ready control-plane 2d8h v1.31.0 172.30.1.2 <none> Ubuntu 20.04.5 LTS 5.4.0-131-generic containerd://1.7.13
ClusterIP vs. NodeIP
- For accessing from nodes:
-
curl localhost:NodePort
orcurl NODE_IP:NodePort
(NodePort) -
curl SVC_IP:SVC_Port
(ClusterIP)
-
# SVC_IP:SVC_Port controlplane $ curl 10.97.210.100:8081 # Node_IP:Node_Port controlplane $ curl 172.30.1.2:32433 # localhost:NodePort controlplane $ curl localhost:32433
- For accessing from inside another pod:
-
curl SVC_NAME:SVC_Port
orcurl SVC_IP:SVC_Port
-
# SVC_NAME:SVC_Port ~ # curl nginx-svc:8081 ~ # curl apache-svc:8080 # SVC_IP:SVC_Port ~ # curl 10.104.249.75:8080 ~ # curl 10.97.210.100:8081
Endpoints
The Endpoints object (192.168.0.7:80) shows the actual pod IP and port where the traffic will eventually go. It's the backend configuration that tells Kubernetes where to forward the traffic from the service.
Therefore, endpoints IP -> Pod IP
controlplane $ k get endpoints NAME ENDPOINTS AGE apache-svc 192.168.0.6:80 2m20s kubernetes 172.30.1.2:6443 2d8h nginx-svc 192.168.0.7:80 12s
Top comments (0)