Does ClusterIP that was automatically created by type NodePort service exists itself as a service?

I learned that type : NodePort service automatically makes a ClusterIP from Link.

And I wondered that the automatically-created ClusterIP exists itself as a type : ClusterIP service?

Hi @jssim710

Why not test the theory? You can do this sort of thing in any lab if you ignore the questions. Use the lab as a playground.

Run an nginx pod to act as the server, and create a NodePort service

controlplane $ k run server --image nginx 
pod/server created
controlplane $ k expose pod server --name my-service --type NodePort --port 80 --target-port 80 -o yaml --dry-run=client > my-service.yaml 
controlplane $ vi my-service.yaml 

Edit the YAML to add in the nodePort property

  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30080  #<- Add this

Create the service

controlplane $ k create -f my-service.yaml 
service/my-service created

Test the nodeport

controlplane $ curl http://localhost:30080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Test the ClusterIP of the same service by doing curl from inside a pod

controlplane $ k run test -it --image wbitt/network-multitool --restart Never --command -- curl http://my-service:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
controlplane $ 

So in the above, we hit the node port from the controlplane prompt, and get the nginx response.

Then we ran a curl command to the service from inside another pod to hit the ClusterIP addres and also got a response

So the answer to your question is YES - sort of. A NodePort service also functions as a ClusterIP. It doesn’t create an additional service

1 Like

Yes, when you create a Service of type NodePort in Kubernetes, it automatically creates a ClusterIP Service as well. This is because the NodePort type Service is built on top of the ClusterIP type Service.

To explain further, in Kubernetes, a Service is an abstraction that defines a logical set of Pods and a policy by which to access them. When you create a Service of type NodePort, Kubernetes creates the following components:

  1. ClusterIP Service: This is a virtual IP address assigned to the Service, which acts as an internal load balancer. It allows other Pods within the Kubernetes cluster to access the Pods targeted by the Service using this internal IP address.

  2. NodePort Service: In addition to the ClusterIP Service, Kubernetes also creates a NodePort Service. This service exposes the Service on each Node’s IP at a static port (NodePort), which allows external clients to access the Service. Internally, this NodePort Service forwards traffic to the ClusterIP Service.

So, in summary, when you create a NodePort type Service in Kubernetes, it automatically creates both a NodePort Service and a ClusterIP Service. The ClusterIP Service exists to provide internal communication within the cluster, while the NodePort Service exposes the Service externally.

Here are the best HTML online learning platforms:

  1. W3School 2. Iqra Technology