NodePort - Exposes the Service on the same port of each selected Node in the cluster using NAT. It Makes a Service accessible from outside the cluster using :. Superset of ClusterIP.
How to create NodePort?
NodePort can be created by specifying a type in specs
while creating Service object.For example, you have a node with IP address 172.18.0.6
and a spring-deployment
pod running under it. NodePort will expose 172.18.0.6:30005
, assuming the port exposed is 30005
, which you can then access outside the Kubernetes cluster.
Example
apiVersion: v1
kind: Service
metadata:
name: spring-service
spec:
type: NodePort
selector:
app: spring-app
ports:
- protocol: TCP
port: 8081
targetPort: 8081
nodePort: 30005
note: The range of node port is 30000-32767
Let's expose our deployment created previously in NodePort type service.Here we will only change spec
type as NodePort
and assign nodePort as 30005
. If we do not specify nodePort
value service object will automatically assign from the range 30000-32767
.
Let's modify our deploy-service.yaml
created in the last blog.
vim deploy-service.yaml
Output
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: spring-service
name: spring-service
spec:
type: NodePort
ports:
- nodePort: 30005
port: 8081
protocol: TCP
targetPort: 8081
selector:
app: spring-service
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-deployment
spec:
replicas: 3
selector:
matchLabels:
app: spring-service
template:
metadata:
labels:
app: spring-service
spec:
containers:
- name: demo-springapp
image: jeewangautam02261995/springbootapp:v2
In this file below changes are made.
type
as NodePort
and added nodePort
as 30005
Let's apply this changes and run below command to verify service type.
kubectl get svc -l app=spring-service
you can see, spring-service
is. NodePort
type assigned IP 10.96.89.35
and which will listen port '30005' and forward the traffic to pod's port '8081'.
Let's try to access our application from one of the master node as we deployed application in multi-node cluster architecture.
let's list out all the nodes first.
kubectl get nodes
Result
There are 3 master nodes and 5 worker nodes but we deploy application in 3 replicas so letβs filter it out using pod's labels.
kubectl get pods -l app=spring-service -o wide
Result
From above command you can see application is deployed in spring-app-worker
spring-app-worker3
spring-app-worker5
. Now we will access application from spring-app-control-plane
master node.
Run below command to access master node first.
docker exec -it spring-app-control-plane sh
Docker command is used here to access master node because kind cluster runs in the docker container.
Result
Let's use ip address of spring-app-worker3
which is 10.244.7.4
and user curl
command. syntax is given below.
Syntax
curl http://<nodeIP>:<nodeport>
Example
curl http://172.18.0.5:30005
Result
We can see, Application is accessible.
In the next blog, we will discuss how to create LoadBalancer
type service locally in Kind and expose it to the external user.
Top comments (2)
Typo?
Here we will only change spec type as NodePort and assign nodePort as 30038.
Here 30038 should be 30005?You are correct, and Thanks for correcting me.