Accessing app on kubernetes using nodeport - kubernetes

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

Related

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

How to find out the ip address of the nodePort

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.

Conecting to a specific pod inside a statefulset from outside the cluster

I have a StatefulSet that matchs the one described in the
StatefulSet Kubernetes tutorial which creates a mysql master/slaves structure. I have the following kubernetes objects:
NAME READY STATUS RESTARTS AGE
pod/mysql-0 2/2 Running 7 23h
pod/mysql-1 2/2 Running 6 23h
pod/mysql-2 2/2 Running 6 23h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 21d
service/mysql ClusterIP None <none> 3306/TCP 23h
service/mysql-read ClusterIP 10.152.183.89 <none> 3306/TCP 23h
NAME READY AGE
statefulset.apps/mysql 3/3 23h
EDIT: The mysql and mysql-read services connect with all the mysql pods.
The node is on my computer, I'm using microk8s.
Now I have a little program runnning on my computer (not in the cluster) that I would like to connect to the master (mysql-0) inside the StatefulSet. Id need something like a service that only connect with mysql-0. Any suggestions on how to do it?
EDIT: The idea is to find a solution that allow me to deploy the cluster just with the .yaml files. It isn't interesting to find one that involves more commands than kubectl apply
The program is the following one:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost??",
user="root",
passwd="",
database="testtable"
)
mycursor = mydb.cursor()
sql = "INSERT INTO testtable (name) VALUES (%s)"
val = ("holaquetal")
mycursor.execute(sql, val)
mydb.commit()
I thought I could add a different label to the mysql-0 pod and add a NodePort service that looks for that label, but I dont want to do it by command line. Is it possible to add one label to mysql-0 inside the StatefulSet yaml?
Another idea could be to do a query DNS to the DNS server that microk8s provides, looking for "mysql.mysql-0", but I don't know how to connect the program to the DNS server, so that I can use host=mysql.mysql-0 or the whole CNAME.
If the node is on your computer so can use port-forward to forward the port of mysql from the pod to your local. Try this:
kubectl port-forward mysql-0 3306:3306
or forward directly to your master service
kubectl port-forward svc/mysql-read 3306:3306

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

Kubernetes service stays in pending state

Service showing pending status after exposing the deployment.
packet#ubuntu:/home/gss$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
wms1 LoadBalancer 10.106.19.103 <pending> 8000:32461/TCP 17h
Installed kubeadm with one master and 4 worker nodes.
created deployment with the command:
sudo docker run -p 8000:8000 w1
here w1 is my image name.
created service with the command:
kubectl expose deployment wms1 --type=LoadBalancer --port=8000
To retrieve external ip for your application in Kubernetes Cluster you have to use cloud provider like Google Kubernetes Engine or Amazon Web Services.
Please check:
https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#external-load-balancer-providers
Otherwise you can use Type NodePort and in that case Kubernetes master will allocate a port from a range specified by --service-node-port-range flag (default: 30000-32767), and each Node will proxy that port (the same port number on every Node) into your Service.
For detailed information: https://kubernetes.io/docs/concepts/services-networking/service/#nodeport
In a second terminal, run the command:
minikube tunnel
restart your service and you should see the EXTERNAL-IP populated