Cloud Computing Software Engineering

How to expose NodePort Service in GKE?

To expose node port service in google cloud Kubernetes engine a few things to note,

Service can only be exposed through NodePort with port range of 30000–32767

In case if you have your service defined as below to expose in Google Kubernetes Engine(GKE).

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
    - port: 443
      nodePort: 30100
      name: https
  selector:
    name: nginx

This will expose your service through port of 30100. NodePort type allows you to access the service in the format of the https://{NODE_IP}:{NODE_PORT}.

Kubernetes cluster can contains multiple nodes and you can get IP’s of nodes using below command. You can use any IP return from the below command as NODE_IP.

(kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="ExternalIP")].address }')

But you can’t access the service without configuring network rules. Using the google cloud CLI, it’s required to setup the network work rules for your project. Before setting the network rules, it’s required to select the project with the id.

Project View in GCloud

You need to set the project so do that first using the below command

gcloud config set project elite-advice-262306

Then need to allow the port using network rules as follow.

gcloud compute firewall-rules create myservice --allow tcp:32001

Without above firewall rule configurations, you won’t be able access the service through NodePort IP and Port.

#kubernetes #k8s #GKE #gcp

Author

KR Kaleraj