can not access to service in postman - kubernetes

i create a image of my project in nodejs .
i write Development yaml :
apiVersion: apps/v1
kind: Deployment
metadata:
name: user-depl
labels:
app: user-depl
spec:
replicas: 3
selector:
matchLabels:
app: user-pod
departemant: user
template:
metadata:
labels:
app: user-pod
departemant: user
spec:
containers:
- name : user-pod
image: kia9372/store-user
ports:
- containerPort: 4000
and create a Services :
apiVersion: v1
kind: Service
metadata:
name: user-service
labels:
app: user-service
spec:
selector:
app: user-pod
ports:
- name: user
protocol: TCP
port: 4000
targetPort: 4000
and when i run this command kubectl get services it show me this :
user-service ClusterIP 10.109.72.253 4000/TCP 36m
and when i send request to this service in postman it not show me any thing .
https://10.109.72.253:4000/user
it show me this error :
Error: connect ETIMEDOUT 10.109.72.253:4000
whats the problem ? how can i solve this problem ???

Your service is not of type load balancer so you will not be able to access it.
Since you did not specify any other type ClusterIP is the default for the Service.
ClusterIP
Exposes the Service on a cluster-internal IP.
Choosing this value makes the Service only reachable from within the cluster
Here is a sample code on how to access it using NoodePort/ClusterIP
https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/05-Services
Img source: https://medium.com/avmconsulting-blog/service-types-in-kubernetes-24a1587677d6

Related

Unable to access the app deployed on minikube cluster by using the url

This the IP I am getting for accessing the application but I am unable to access this app on this url.
This is what I am getting while accessing the url
This is the service file I am using for application.
apiVersion: v1
kind: Service
metadata:
name: villa-service
spec:
selector:
app: villa
ports:
- port: 80
targetPort: 80
nodePort: 31000
type: NodePort
This is the deployment file
apiVersion: apps/v1
kind: Deployment
metadata:
name: villa.deployment
labels:
app: villa
spec:
replicas: 2
selector:
matchLabels:
app: villa
template:
metadata:
labels:
app: villa
spec:
containers:
- name: villa
image: farhan23432/angular
ports:
- containerPort: 80
This is the inbound rules of the Security group of the instance on which I am running my minikube cluster.
This is the versions of the minikube, docker and kubectl that I am using.
This is the status of the minikube

How does gateway connect other services in Kubernetes?

I am attaching the image of my application flow. Here the Gateway and other services are created using NestJS. The request for any API comes through the gateway.
The Gateway-pod and API-pod communicate using TCP protocol.
After deployment the Gateway is not able to discover any API pods.
I am attaching the YAML image file also for both Gateway & Pods.
Please do let me know what mistake I am doing in the YAML file.
**APPLICATION DIAGRAM**
Gateway YAML
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: roushan
name: gateway-deployment
spec:
replicas: 1
selector:
matchLabels:
app: roushan-app
template:
metadata:
labels:
app: roushan-app
spec:
containers:
- name: gateway-container
image: nest-api-gateway:v8
ports:
- containerPort: 1000
apiVersion: v1
kind: Service
metadata:
namespace: roushan
name: gateway-svc
spec:
selector:
app: roushan-app
ports:
- name: gateway-svc-container
protocol: TCP
port: 80
targetPort: 1000
type: LoadBalancer
Pod YAML
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: roushan
name: pod1-deployment
spec:
replicas: 1
selector:
matchLabels:
app: roushan-app
template:
metadata:
labels:
app: roushan-app
spec:
containers:
- name: pod1-container
image: nest-api-pod1:v2
ports:
- containerPort: 4000
apiVersion: v1
kind: Service
metadata:
namespace: roushan
name: pod1-srv
spec:
selector:
app: roushan-app
ports:
- name: pod1-svc-container
protocol: TCP
port: 80
targetPort: 4000
the gateway should be able to access the services by their DNS name. for example pod1-srv.svc.cluster.local, if this does not work you may need to look at the Kubernetes DNS setup.
I have not used AKS, they may use a different domain name for the cluster other than svc.cluster.local
YAML Points
Ideally, you should be keeping the different selectors across the deployment.
You are using the same selectors for both deployments. Gateway and application deployment.
Service will forward the traffic to deployment based on selectors and labels, this might redirect the service-2 request to POD-1.
Networking
You gateway service(Pods) connect to internal service by just service-name like : pod1-srv if in same namespaces.
if gateway and application in different namespaces you have to call each other like http://<servicename>.<namespace>.svc.cluster.local

Can't connect with my frontend with kubectl

With kubernetes, I created an ingress with a service like these :
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: syntaxmap2
spec:
backend:
serviceName: testsvc
servicePort: 3000
The service testsvc is already created.
I created a frontend service like these :
apiVersion: v1
kind: Service
metadata:
name: syntaxmapfrontend
spec:
selector:
app: syntaxmap
tier: frontend
ports:
- protocol: "TCP"
port: 7000
targetPort: 7000
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: syntaxmapfrontend
spec:
selector:
matchLabels:
app: syntaxmap
tier: frontend
track: stable
replicas: 1
template:
metadata:
labels:
app: syntaxmap
tier: frontend
track: stable
spec:
containers:
- name: nginx
image: "gcr.io/google-samples/hello-frontend:1.0"
lifecycle:
preStop:
exec:
command: ["/usr/sbin/nginx","-s","quit"]
When I do these command :
kubectl describe ingress syntaxmap2
I have an Ip adress than i can put in my browser and I have an answer
But when I do these command :
kubctl describe service syntaxmapfrontend
I have an Ip adress with a port and when I try to connect to it with curl, I have a time out.
How can I connect to my kubernet frontend with curl ?
The service is accessible only from within the k8s cluster. You either need to change the type of address from ClusterIP to NodeIP, or use something like kubectl port-forward or kubefwd.
If you need more detailed advice, you'll need to post the output of those commands, or even better, show us how you created the objects.
I have found a way.
I write :
minikube service syntaxmapfrontend
And it open a browser with the right URL.

Kubernetes ERR_NAME_NOT_RESOLVED

I have two deployments, one for backend and one for frontend and two services for them. Frontend Service is set as a LoadBalancer and it is exposed as expected(using minikube tunnel). Backend service shouldnt be exposed outside of the cluster, therefore I didnt set any type of service(default is ClusterIP which is available only within a cluster). Now I would like to make calls from frontend to backend. When I type
kubectl exec -it FRONT_END_POD_NAME -- /bin/sh
And then use curl I can get all resources which I expect, however, when I open my website application which fetches the same resource as I type in curl, there is an error in console net::ERR_NAME_NOT_RESOLVED. Do You have any idea why is that happening, even when I am able to curl it from my frontend and everything works? How to fix it?
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
labels:
app: backend
spec:
selector:
matchLabels:
app: backend
replicas: 1
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: ajris/site_backend:pr-kubernetes
ports:
- containerPort: 8080
====
apiVersion: v1
kind: Service
metadata:
name: backend-service
labels:
app: backend
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 8081
targetPort: 8080
===
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
labels:
app: frontend
spec:
selector:
matchLabels:
app: frontend
replicas: 1
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: ajris/site_frontend:travis-66
ports:
- containerPort: 3000
imagePullPolicy: Always
===
apiVersion: v1
kind: Service
metadata:
name: frontend-service
labels:
app: frontend
spec:
ports:
- protocol: TCP
port: 3001
targetPort: 3000
selector:
app: frontend
type: LoadBalancer
Since this doesn't have an section answer yet, might as well make it easier for others to find it in the future. The person who asked the question answered it themselves here in the comments of the question.
Essentially what's happening here is, that React doesn't execute on the frontend pod, but in the browser of the client. Thus any request that tries to contact the backend pod will fail if it's not reachable by the client such as is the case with a backend pod with a ClusterIP service.
To solve this issue, the backend server needs to be accessible from the client. This can be done by changing the service that is associated with the backend pod to a LoadBalancer or by using an ingress.

Kubernetes Communication between services

For learning purposes, i have two services in a cluster on google cloud:
API Service with the following k8 config:
deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: myapp-api
labels:
app: myapp-api
spec:
replicas: 1
template:
metadata:
labels:
app: myapp-api
spec:
containers:
- image: gcr.io/prefab-basis-213412/myapp-api:0.0.1
name: myapp-api
ports:
- containerPort: 3000
service.yaml
kind: Service
apiVersion: v1
metadata:
name: myapp-api
spec:
selector:
app: myapp-api
ports:
- protocol: TCP
port: 80
targetPort: 3000
And a second service, called frontend, that's publicly exposed:
deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: myappfront-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: myappfront
spec:
containers:
- image: gcr.io/prefab-basis-213412/myappfront:0.0.11
name: myappfront-deployment
ports:
- containerPort: 3000
service.yaml
apiVersion: v1
kind: Service
metadata:
name: myappfront-service
spec:
type: LoadBalancer
selector:
app: myappfront
ports:
- port: 80
targetPort: 3000
The front service is basically a nodejs app that only does a rest call to the api service like so axios.get('http://myapp-api').
The issue is that the call is failing and it's unable to find the requested endpoint. Is there any additional config that i'm currently missing for the API service to be discovered?
P.S. Both services are running and I can connect to them by proxying from localhost.
Since you're able to hit the services when proxying it sounds like you've already gone through most of the debugging steps for in-cluster issues (https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/). That suggests the problem might not be within the cluster. Something to watch out for with frontends is where the http call takes place. It could be in the server with node but given you're seeing this issue I'd suggest that it's in the browser. (If you see can see the call in the browser console then it is.) If the frontend's call is being made in the browser then it doesn't have access to the Kubernetes dns and the cluster-internal service name won't resolve. To handle this you could make the backend service LoadBalancer and pass the external name into the frontend.