DEV Community

Cheedge Lee
Cheedge Lee

Posted on • Originally published at notes-renovation.hashnode.dev

ClusterIP vs. NodePort

ClusterIP

kubectl expose deployment nginx --name nginx-svc --port 8080 --target-port 80
Enter fullscreen mode Exit fullscreen mode

NodePort

kubectl expose deployment nginx --name nginx-svc --port 8081 --target-port 80 --type NodePort
Enter fullscreen mode Exit fullscreen mode

Different Ports

  1. target-port -- container port
    • Where your pod/container listens
    • k get endpoints
  2. port -- service's internal port
    • access the svc from other port
    • k get svc
  3. NodePort -- expose to external/all nodes
    • k get endpoints or k 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
Enter fullscreen mode Exit fullscreen mode

ClusterIP vs. NodeIP

  • For accessing from nodes:
    • curl localhost:NodePort or curl 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
Enter fullscreen mode Exit fullscreen mode
  • For accessing from inside another pod:
    • curl SVC_NAME:SVC_Port or curl 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)