I declared a service in nodePort mode. but there's something I don't understand. maybe I'm getting confused. The nodePort, forwarded to the service’s port, and received on the targetPort by the pod.
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: {{ .Release.Name }}
product: {{ .Values.product }}
environment: {{ .Values.environment }}
version: {{ .Values.version }}
component: frontend
spec:
type: NodePort
ports:
- port: 6001
targetPort: 6001
protocol: TCP
selector:
app: countly-frontend
when I list the services on kubernetes I see this :
service/countly-frontend NodePort 10.xx.xx.12 <none> 6001:31145/TCP 110s
normally I should see 6001:6001 and not 6001:31145 ? The app is listening on port 6001 as you may have guessed.
It's how kubernetes service nodeport works. NodePort service bind on random port in the range 30000-32767. If you want to specify the bind port add nodePort attribute to set it (docs ) but it must be in the required range. For the example I choose 30007 :
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: {{ .Release.Name }}
product: {{ .Values.product }}
environment: {{ .Values.environment }}
version: {{ .Values.version }}
component: frontend
spec:
type: NodePort
ports:
- port: 6001
targetPort: 6001
protocol: TCP
nodePort: 30007
selector:
app: countly-frontend
Not sure if this might work:
ports:
- protocol: TCP
port: 6001
targetPort: 6001
Or if you could try changing targetPort
to nodePort
in your original
Let me clarify some misconceptions:
You cannot change the nodePort
port range for a managed Kubernetes cluster like GKE
.
A word about nodePort
from official Kubernetes documentation:
NodePort
: Exposes the Service on each Node’s IP at a static port (theNodePort
). AClusterIP
Service, to which theNodePort
Service routes, is automatically created. You’ll be able to contact theNodePort
Service, from outside the cluster, by requesting<NodeIP>:<NodePort>
.
nodePort
port ranges from 30000
to 32767
.
What you see here:
when I list the services on kubernetes I see this :
service/countly-frontend NodePort 10.xx.xx.12 ><none> 6001:31145/TCP 110s
Specifically: 6001:31145/TCP
is correct.
Assume that there is a pod with an application running on port 50001
.
This is a service.yaml
of above application:
apiVersion: v1
kind: Service
metadata:
name: hello-service
spec:
selector:
app: hello
ports:
- name: hello-port
port: 5678 # CLUSTER-IP PORT
targetPort: 50001 # PORT WHICH YOUR APPLICATION IS RUNNING ON
nodePort: 30051 # NODEPORT PORT
type: NodePort
Output of $ kubectl get services
:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-service NodePort 10.86.1.195 <none> 5678:30051/TCP 25h
You will have an access to your application by:
NodeIP:NodePort(30051)
(external access)ClusterIP:port(5678)
(internal access)PodIP:targetPort(50001)
(internal access)If you would like to expose your application on port 6001
for external use you can try a service type of LoadBalancer
.
There are answers on StackOverflow that are going deeper in this topic:
NodePort
by looking on this answer.Please take a look on official documentation about exposing applications on a GKE
cluster: Cloud.google.com: Kubernetes engine: Exposing apps
Please let me know if you have any questions to that.