Why is my kubectl loadbalancer targeting to a random port? - kubernetes

I have a service and deployment kube config files like below.
Now, when i apply these two files, its creating a loadbalancer but its targeting to a random port but not port 80.
I'm a newbie to EKS and tried different kube config files but it still tries to target a random port.
service file:
apiVersion: v1
kind: Service
metadata:
name: runners-test
labels:
app: runners-test
spec:
ports:
- port: 80
targetPort: 80
selector:
app: runners-test
type: LoadBalancer
deployment file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: runners-test
labels:
app: runners-test
spec:
replicas: 1
selector:
matchLabels:
app: runners-test
template:
metadata:
labels:
app: runners-test
spec:
containers:
- name: runners-test
image: mylocaldockerimage
ports:
- containerPort: 80
C02X67GOKL:terraform$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP > PORT(S) AGE
kubernetes ClusterIP 10.100.0.1 443/TCP 8d
runners-test LoadBalancer 10.100.246.180 af3884a05ad7811e99b0e06a70e73221-192467907.us-west-2.elb.amazonaws.com 80:31038/TCP 43m
It's targeting to port a random port 31038, when i connect to my pod and run ps -ef, i can see that a service is running on port 80.

As mentioned in the Kubernetes Service documentation , setting this type will enforce the underlying cloud provider to assign a public IP address to your service and route the traffic on your exposed port ( which is 80 in your case ) to Node Port ( 31038 ) on the kubernetes cluster level.
On cloud providers which support external load balancers, setting the type field to LoadBalancer provisions a load balancer for your Service.

Related

Though external ip is resolved, the website returns connection timedout in kubernetes GKE

I have created a k8s deployment and service yaml for a static website. External IP address is also resolved in kubernetes service. But when I try to access the website through curl or browser, it returns connection timed out.
Dockerfile:
FROM nginx:alpine
COPY . /usr/share/nginx/html
K8s deployment yml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: ohno-website
labels:
app: ohno-website
spec:
replicas: 1
selector:
matchLabels:
app: ohno-website
template:
metadata:
labels:
app: ohno-website
spec:
containers:
- name: ohno-website
image: gkganeshr/ohno-website:v0.1
imagePullPolicy: Always
ports:
- containerPort: 80
k8s service yml:
apiVersion: v1
kind: Service
metadata:
name: ohno-website
spec:
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 9376
selector:
app: ohno-website
ohno_fooserver#cloudshell:~ (fourth-webbing-279817)$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.16.0.1 <none> 443/TCP 8h
ohno-website LoadBalancer 10.16.12.162 34.70.213.174 80:31977/TCP 7h4m
The target port defined in the service defition YAML is incorrect. It should match with container port from pod definition in deployment YAML
targetPort: 9376
should be changed to
targetPort: 80

Access Minikube Loadbalancer Service From Host Machine

I am trying to learn how to use Kibernetes with Minikube and have the following deployment and service:
---
kind: Service
apiVersion: v1
metadata:
name: exampleservice
spec:
selector:
app: myapp
ports:
- protocol: "TCP"
# Port accessible inside cluster
port: 8081
# Port to forward to inside the pod
targetPort: 8080
# Port accessible outside cluster
nodePort: 30002
type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: myappdeployment
spec:
replicas: 5
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: tutum/hello-world
ports:
- containerPort: 8080
I expect to be able to hit this service from my local machine at
http://192.168.64.2:30002
As per the command: minikube service exampleservice --url but when I try to access this from the browser I get a site cannot be reached error.
Some information that may help debugging:
kubectl get services --all-namespaces:
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default exampleservice LoadBalancer 10.104.248.158 <pending> 8081:30002/TCP 26m
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2h
default user-service-service LoadBalancer 10.110.181.202 <pending> 8080:30001/TCP 42m
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 2h
kube-system kubernetes-dashboard ClusterIP 10.110.65.24 <none> 80/TCP 2h
I am running minikube on OSX.
This is expected.
Do note that LoadBalancer is for cloud to create external load balancer like ALP/NLP in AWS and something similar in GCP/Azure etc.
Update the service as shown here. here i assume 192.168.64.2 is your minikube ip. if not, update it with minikube ip to make it work.
kind: Service
apiVersion: v1
metadata:
name: exampleservice
spec:
selector:
app: myapp
ports:
- protocol: "TCP"
# Port accessible inside cluster
port: 8081
# Port to forward to inside the pod
targetPort: 80
# Port accessible outside cluster
nodePort: 30002
type: LoadBalancer
externalIPs:
- 192.168.64.2
Now you can access your application at http://192.168.64.2:8081/
If you need to access the application at 30002, you can use it like this
kind: Service
apiVersion: v1
metadata:
name: exampleservice
spec:
selector:
app: myapp
ports:
- protocol: "TCP"
# Port accessible inside cluster
port: 8081
# Port to forward to inside the pod
targetPort: 80
# Port accessible outside cluster
nodePort: 30002
type: NodePort
Your deployment file does not look correct to me.
delete it
kubectl delete deploy/myappdeployment
use this to create again.
apiVersion: apps/v1beta1
kind: Deployment
metadata:
labels:
app: myapp
name: myappdeployment
spec:
replicas: 5
selector:
matchLabels:
app: myapp
strategy: {}
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: tutum/hello-world
name: myapp
ports:
- containerPort: 80
NOTE: Minikube support LoadBalancer services (via minikube tunnel)
you can get the IP and port through which you
can access the service by running
minikube service kubia-http #=> To open a browser with an IP and port
OR
minikube service kubia --url #=> To get the IP and port in the terminal

digitalocean kubernetes loadbalancer

I have deployed my app on the limited available Kubernetes cluster on DigitalOcean.
I have a spring boot app with a service exposed on port 31744 for external using nodeport service config.
I created a Loadbalancer using the yaml config per DO link doc: https://www.digitalocean.com/docs/kubernetes/how-to/add-load-balancer/
However, I am not able to hook up to my service. Can you advise on how it can be done so I can access my service from the loadbalancer?
The following is my "kubectl get svc" output for my app service:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-springboot NodePort 10.245.6.216 <none> 8080:31744/TCP 2d18h
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 3d20h
sample-load-balancer LoadBalancer 10.245.53.168 58.183.251.550 80:30495/TCP 2m6s
The following is my loadbalancer.yaml:
apiVersion: v1
kind: Service
metadata:
name: sample-load-balancer
spec:
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 31744
name: http
My service.yaml:
apiVersion: v1
kind: Service
metadata:
name: my-springboot
labels:
app: my-springboot
tier: backend
spec:
type: NodePort
ports:
# the port that this service should serve on
- port: 8080
selector:
app: my-springboot
tier: backend
Thanks
To expose your service using LoadBalancer instead of NodePort you need to provide type in service as LoadBalancer. So your new service config yaml will be:
apiVersion: v1
kind: Service
metadata:
name: my-springboot
labels:
app: my-springboot
tier: backend
spec:
type: LoadBalancer
ports:
# the port that this service should serve on
- port: 8080
selector:
app: my-springboot
tier: backend
Once you apply the above service yaml file, you will get the external IP in kubectl get svc which can be used to access the service from outside the kubernetes cluster.

Unable to connect to external load balancer even after exposing service in kubernetes

I have the following deployment file
apiVersion: apps/v1
kind: Deployment
metadata:
name: family-tree-deployment
labels:
app: familytree
spec:
replicas: 1
selector:
matchLabels:
app: familytree
template:
metadata:
labels:
app: familytree
spec:
containers:
- name: familytree
image: index.docker.io/koustubh/familytree:v1.0
ports:
- containerPort: 8080
I could successfully create the deployment using kubectl create -f deploy.yml
Now, I simply exposed this deployment with the following command
kubectl expose deployment family-tree-deployment --type=LoadBalancer --name=familytree-service
The service was successfully created.
The output is
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
familytree-service LoadBalancer 10.51.244.161 35.221.113.235 8080:30505/TCP 1h
$ kubectl describe svc familytree-service
Name: familytree-service
Namespace: default
Labels: app=familytree
Annotations: <none>
Selector: app=familytree
Type: LoadBalancer
IP: 10.51.244.161
LoadBalancer Ingress: 35.221.113.235
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30505/TCP
Endpoints: 10.48.4.7:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
I could login to the pod and I made sure the service is working.
However, when I use the external ip of the load balancer and query my api, the connection times out.
I have made sure firewall allows port 8080.
My application is running on port 8080
The generated Service object looks perfectly valid, so we can exclude a label issue or a missing public IP address. Besides you can access your Service internally, which means the firewall rule was applied incorrectly, most likely.
Please ensure you allow incoming traffic as follows
from the internet to the load balancer on TCP port 8080
from the load balancer to all Kubernetes nodes on TCP port 30505

How to reach kubernete service running in minikube from the same computer

I created a small java application launching manually a jetty server listening to the address 127.0.0.1 port 8081.
The small server app listens for GET requests to the subaddress /dockerClient/ping, and answers "pong".
I test with SoapUI on http://127.0.0.1:8081/dockerClient/ping, and I get my pong.
I create a docker image, deploy the application on minikube and expose a service with the following configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: client
labels:
tier: frontend
spec:
replicas: 1
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: docker-client
image: docker-client
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
name: client-service
labels:
tier: frontend
spec:
type: NodePort
ports:
- port: 8081
protocol: TCP
name: http
selector:
tier: frontend
Once I deploy and expose, I get the following information:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
client-service NodePort 10.107.64.238 <none> 8081:31703/TCP 19m
I try to use SoapUI to reach the service :
For this, I retrieve the IP of the minikube using :
echo $(minikupe ip)
Then, I try a GET request to http://$(minikube ip):31703/dockerClient/ping, but the request is refused.
I tried on http://$(minikube ip):8081/dockerClient/ping, same.
What do I do wrong? How can I reach the jetty server exposing my ping?
In your service yml, you just tell the port (it can only be used within the cluster), it is not enough to expose your service outside your minikube, you also need to specify the targetPort (should match the container port e.g.8081) and nodePort (it is the port you can ping from localhost e.g. 31081).
---
apiVersion: v1
kind: Service
metadata:
name: client-service
labels:
tier: frontend
spec:
type: NodePort
ports:
- port: 8081 # access within the cluster
targetPort: 8081 # should match to the container port
nodePort: 31081 # expose outside the cluster and range from 30000 to 32767
protocol: TCP
name: http
selector:
tier: frontend
After you added the targetPort and nodePort, you can get the public endpoint by:
minikube service client-service --url
P.S. nodePort is optional, minikube would assign an random port from the range if nodePort has not been specified.
I think the jetty server should listen to the address 0.0.0.0.