How to find out the ip address of the nodePort - kubernetes

I have expose my deployment to the specific nodeport, if I want to connect to this deployment in cluster, how can I find the ip address of nodeport?

You access NodePort service with <node-ip>:<node-port>.
1. Check Node IP
To check node-ip, you can execute the following command:
$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
gke-rafal-test-cluster-default-pool-ef8193e3-1450 Ready <none> 6m47s v1.13.11-gke.23 192.168.1.234 35.188.23.46 Container-Optimized OS from Google 4.14.138+ docker://18.9.7
gke-rafal-test-cluster-default-pool-ef8193e3-1bd4 Ready <none> 6m47s v1.13.11-gke.23 192.168.1.230 34.67.114.201 Container-Optimized OS from Google 4.14.138+ docker://18.9.7
gke-rafal-test-cluster-default-pool-ef8193e3-q3c4 Ready <none> 6m47s v1.13.11-gke.23 192.168.1.228 34.69.230.23 Container-Optimized OS from Google 4.14.138+ docker://18.9.7
Any of node EXTERNAL-IP will work, so you can use 35.188.23.46, 4.67.114.201, or 34.69.230.23. It doesn't matter.
If you don't see any EXTERNAL-IP, it may mean that your Kubernetes nodes do not have external IPs, so you just can't access them from outside.
If you run minikube, you can check node ip with the minikube ip command. If you run Docker Desktop Kubernetes, then node ip is localhost.
2. Check Node Port
To check node-port, you can execute the following command.
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-release-hazelcast NodePort 10.208.7.24 <none> 5701:31096/TCP 4s
The <node-port> is 31096.

Related

mac M1 minikube service tunnel doesn't work

with newest Docker desktop and minikube on Mac m1
I have tried both Nodeport and LoadBalancer with tunnel on minikube's docs: https://minikube.sigs.k8s.io/docs/handbook/accessing/ neither works, I cannot curl or access app from my browser.
Essentially, the port, and url that tunnel gives me from minikube doesn't work and I get "can not reach" in the browser.
❯ k get all
NAME READY STATUS RESTARTS AGE
pod/shuffle-depl-5746c5976f-qg7wf 1/1 Running 0 26m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/shuffle-depl LoadBalancer 10.106.74.89 <pending> 5000:31170/TCP 17m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/shuffle-depl 1/1 1 1 26m
NAME DESIRED CURRENT READY AGE
replicaset.apps/shuffle-depl-5746c5976f 1 1 1 26m
However port forwarding to pod, and port forwarding to LB service works from here.
But I'm not satisfied with the solution. Is there no good solution for this, and what is underneath that's causing this issue?

Unable to reach service/API from outside the cluster - Kubernetes (Metallb+HAProxy Ingress Controller)

I've created a bare-metal multi-master k8s cluster using kubekey.
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
master1 Ready control-plane,master 23h v1.23.10
master2 Ready control-plane,master 23h v1.23.10
master3 Ready control-plane,master 23h v1.23.10
worker1 Ready worker 23h v1.23.10
worker2 Ready worker 23h v1.23.10
worker3 Ready worker 23h v1.23.10
$ curl localhost:10249/healthz
ok
Added MetalLB load balancer and HAProxy Ingress Controller. The haproxy-controller gets the external IP address from the Metallb correctly:
$ kubectl get svc -n haproxy-controller
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
haproxy-kubernetes-ingress LoadBalancer 10.233.59.120 10.30.2.81 80:32244/TCP,443:30908/TCP,1024:32666/TCP 21h
Deployed a microservice, and exposed the service via ingress:
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 23h
ms-login-http ClusterIP 10.233.3.180 <none> 80/TCP 21h
$ kubectl describe ing
Name: ms-login-http
Labels: <none>
Namespace: default
Address: 10.30.2.81
Default backend: ms-login-http:80 (10.233.103.1:8080)
Rules:
Host Path Backends
---- ---- --------
api.mydomain.in
/api/sc ms-login-http:80 (10.233.103.1:8080)
Annotations: haproxy.org/load-balance: roundrobin
haproxy.org/src-ip-header: True-Client-IP
Events: <none>
The issue is reachability of the deployed API:
[✓] Accessing the API from within any of the cluster nodes works fine
$ curl api.mydomain.in/api/sc/healthcheck
success
[✕] Same API from outside the cluster nodes fails
$ curl api.mydomain.in/api/sc/healthcheck
curl: (7) Failed to connect to api.mydomain.in port 80 after 0 ms: Connection refused
Seem to be firewall issue, but unable to narrow down for what maybe blocking the traffic. The IPTables on the master nodes has several calico forward rules. The rules list is shared in this gist.
Any direction/insight would greatly help, as I'm missing something basic here. Not faced this issue when I created a similar cluster some months back. Seems the latest version of calico has something to do with it.

How to connect from pgAdmin to Postgresql in Kubernetes/Minikube

I run a local kubernetes cluster (Minikube) and I try to connect pgAdmin to postgresql, bot run in Kubernetes.
What would be the connection string? Shall I access by service ip address or by service name?
kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dbpostgresql NodePort 10.103.252.31 <none> 5432:30201/TCP 19m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d21h
pgadmin-service NodePort 10.109.58.168 <none> 80:30200/TCP 40h
kubectl get ingress:
NAME CLASS HOSTS ADDRESS PORTS AGE
pgadmin-ingress <none> * 192.168.49.2 80 40h
kubectl get pod
NAME READY STATUS RESTARTS AGE
pgadmin-5569ddf4dd-49r8f 1/1 Running 1 40h
postgres-78f4b5db97-2ngck 1/1 Running 0 23m
I have tried with 10.103.252.31:30201 but without success.
Inside the cluster, services can refer to each other by DNS based on Service object names. So in this case you would use dbpostgresql or dbpostgresql.default.svc.cluster.local as the hostname.
Remember minikube is running inside its' own container, the NodePort clusterIPs you're getting back are open inside of minikube. So to get minikube's resolution of port and ip, run: minikube service <your-service-name> --url
This will return something like http://127.0.0.1:50946 which you can use to create an external DB connection.
Another option would be to use kubectl to forward a local port to the service running on localhost ex. kubectl port-forward service/django-service 8080:80

Connected refused when trying to hit kubernetes nodeport service using minikube in win10

I have a very simple springboot service deployed on minikube in windows 10.
C:\Software\Kubernetes>kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
myspringbootserver 1/1 1 1 68m
C:\Software\Kubernetes>kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 49d
myspringbootserver NodePort 10.110.179.207 <none> 9080:30451/TCP 6m50s
C:\Software\Kubernetes>minikube service myspringbootserver --url
http://192.168.99.101:30451
But when I try to hit the service from my chrome browser with url
http://192.168.99.101:30451/MySpringBootServer/heartbeat
getting connection refused exception.Not sure what is going wrong.Could anyone help to resolve it please?
enter image description here
Can you curl or wget using the IP address of the pod?
For example kubectl exec -it podname -- curl http://podip:9080/MySpringBootServer/heartbeat
if not, ensure the path is correct
if yes, make sure the pod exists as an endpoint of the service
kubectl get endpoints myspringbootserver
there is a good debugging document regarding services here:
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-application/#debugging-services

Accessing app on kubernetes using nodeport

Dear all I have deployed a sample service such as this:
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 1d
mynodejsapp NodePort 10.233.2.225 <none> 3000:31209/TCP 43s
may I ask how do I access the app mynodejsapp on the cluster ip?
When I did a get nodes -o wide this is what I have seen as below,
$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
controlplane-node-001 Ready master 2d v1.9.1+2.1.8.el7 <none> Oracle Linux Server 7.2 4.1.12-112.14.13.el7uek.x86_64 docker://17.3.1
controlplane-node-002 Ready master 2d v1.9.1+2.1.8.el7 <none> Oracle Linux Server 7.2 4.1.12-112.14.13.el7uek.x86_64 docker://17.3.1
controlplane-node-003 Ready master 2d v1.9.1+2.1.8.el7 <none> Oracle Linux Server 7.2 4.1.12-112.14.13.el7uek.x86_64 docker://17.3.1
default-node-001 Ready node 2d v1.9.1+2.1.8.el7 <none> Oracle Linux Server 7.2 4.1.12-112.14.13.el7uek.x86_64 docker://17.3.1
default-node-002 Ready node 2d v1.9.1+2.1.8.el7 <none> Oracle Linux Server 7.2 4.1.12-112.14.13.el7uek.x86_64 docker://17.3.1
Any help. Thanks.
may i ask how do I access the app mynodejsapp on the cluster ip?
Now, for direct answer to your question in regards to your service overview:
To access mynodejsapp service from outside of the cluster you need to target IP of any of the nodes on port 31209 (and kube-proxy will route it to mynodejsapp service for you)
To access mynodejsapp service from within the cluster, meaning from another pod running on that same cluster you need to target clusterIP 10.233.2.225:3000 (or alternatively with running kube-dns you can use service name directly mynodejsapp:3000)
As detailed in the official documentation clusterIP is tied to service, and in turn it is resolved through kube-dns from service name to clusterIP. In a nutshell you can use clusterIP only from within pods running on said cluster (same as service).
As for exposing services externally through NodePort you can find more info also in the official documentation