Kubernetes: spring cloud gateway not working - spring

I have a spring cloud gateway that works fine in the docker configuration, like this:
(all routes/services except ratings are removed for readability's sake)
#Value("${hosts.ratings}")
private String ratingsPath;
#Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.host("*").and().path("/api/ratings/**")
.uri(ratingsPath + ":2226/api/ratings/"))
...other routes...
.build();
}
This gets it values from the application.properties locally, and from an environment variable in docker, like so in the docker-compose:
apigw:
build: ./Api-Gateway
container_name: apigw
links:
- ratings
...
depends_on:
- ratings
...
ports:
- "80:8080"
environment:
- hosts_ratings=http://ratings
...
This configuration works just fine. However, when porting this to our kubernetes cluster, all routes get a 404.
The deployment of our api gateway is as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: apigw
name: apigw-deployment
spec:
replicas: 1
selector:
matchLabels:
app: apigw
template:
metadata:
labels:
app: apigw
spec:
containers:
- name: apigw
image: redacted
ports:
- containerPort: 8080
env:
- name: hosts_ratings
value: "ratings-service.default.svc.cluster.local"
...
With ratings-service being our ratings service (that definitely works, because when exposing it directly from its service, it does work), defined like this:
apiVersion: v1
kind: Service
metadata:
name: ratings-service
labels:
app: ratings
spec:
selector:
app: ratings
ports:
- port: 2226
targetPort: 2226
The service of our api gateway is as follows, using bare metal with an external IP that does work:
apiVersion: v1
kind: Service
metadata:
name: apigw-service
labels:
app: apigw
spec:
selector:
app: apigw
ports:
- port: 80
targetPort: 8080
externalIPs:
- A.B.C.D
How I believe it should work is that ratings-service.default.svc.cluster.local would get translated to the correct ip, filled in to the ratingsPath variable, and the query would succeed, but this is not the case.
Our other services are able to communicate in the same way, but the api gateway does not seem to be able to do that.
What could be the problem?

Posting community wiki based on comment for better visibility. Feel free to expand it.
The issue was a faulty version of the image:
It seems like the service i was using just straight up didn't work. Must have been a faulty version of the image i was using.
Check also:
Access Services Running on Clusters | Kubernetes
Service | Kubernetes
Debug Services | Kubernetes
DNS for Services and Pods | Kubernetes

Related

.Net core microservice with HTTPS on AWS EKS

We have lunched .Net microservice on container and published it on EKS Cluster.
It's working fine on http.
We follow the link to deploy .Net microservice as deploy as a container.
https://dotnet.microsoft.com/en-us/learn/aspnet/microservice-tutorial/docker-file
We used below deploy.yaml
**---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mymicroservice
spec:
replicas: 1
template:
metadata:
labels:
app: mymicroservice
spec:
containers:
- name: mymicroservice
image: [YOUR DOCKER ID]/mymicroservice:latest
ports:
- containerPort: 80
env:
- name: ASPNETCORE_URLS
value: http://*:80
selector:
matchLabels:
app: mymicroservice
apiVersion: v1
kind: Service
metadata:
name: mymicroservice
spec:
type: LoadBalancer
ports:
port: 80
selector:
app: mymicroservice**
This exposed our microservices behind classic load balancer. It's working fine on Http.
but we facing challenges on HTTPS. How can this be achieved? If we need to use Nginx Ingress Controller how that yaml we can tune according to our deployment.yaml

Consuming external API request from kubernetes pod

I'm learning Kubernetes and i developed a simple spring-boot project that expose a simple API /getHomeSpace. When invoked, this API making a request to an external API that is the following: https://api.nasa.gov/planetary/apod. When i start-up my app with docker-compose it works perfectly. But when i set up kubernetes pod and service i receive the following error:
Error during callApod: I/O error on GET request for "https://api.nasa.gov/planetary/apod": No subject alternative DNS name matching api.nasa.gov found.; nested exception is javax.net.ssl.SSLHandshakeException: No subject alternative DNS name matching api.nasa.gov found.
My kubernetes config files:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: server
template:
metadata:
labels:
component: server
spec:
containers:
- name: server
image: antonio94c/view_api:11
ports:
- containerPort: 8080
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
type: ClusterIP
ports:
- port: 8080
targetPort: 8080
#nodePort: 30000
selector:
component: server
What i'm missing??
I tried to read Kubernetes doc and stackoverflow related topics, but i didn't found a solution.

Nginx ingress configuration for Kubernetes cluster hosted on windows

I am running Kubernetes cluster on my windows PC via Docker desktop. I am trying to create a very basic pod with a simple ingress configuration, but it doesn't seem to work. I thought the backend pod + service + ingress is a very basic setup, however I don't find a lot of help online. Please advise what I am doing wrong here.
My deployment.yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
service.yaml
apiVersion: v1
kind: Service
metadata:
name: test-cluster-ip
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 1234
targetPort: 80
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /testpath
pathType: Exact
backend:
service:
name: test-cluster-ip
port:
number: 1234
This is what I see when I access localhost from the browser
Also, I would like to ask if it is uncommon to run Kubernetes on windows even for testing (especially with ingress). I don't seem to find a lot of examples in the internet.
I thought the backend pod + service + ingress is a very basic setup, however I don't find a lot of help online. Please advise what I am doing wrong here.
It is indeed a very basic setup. And your k8s deployment/service/ingress yaml files are correct.
First, check if you installed NGINX ingress controller. If not, run:
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml
After that, you will be able to reach the k8s cluster using the following URL:
http://kubernetes.docker.internal/
But deploying ingress like this:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /testpath
pathType: Exact
backend:
service:
name: test-cluster-ip
port:
number: 1234
you are configuring the ingress to rewrite /testpath to the /. And requesting url without /testpath will return 404 status code.
See more rewrite examples here.
So, if you use the following URL, you will get the Nginx webpage from k8s deployment.
http://kubernetes.docker.internal/testpath

Posting a json message to a webserver inside a pod

I am new to kubernetes.
I have implemented a webserver inside a pod and set a Nodeport service for that pod.
I want to send a POST request with a custom message (in json) to a pod after it has been created and ready to use. I want to use the go client library for that matter. Could you please let me know how I can do that?
Which part of the library come to help?
Thanks.
Say the go server runs on locally, you normally use http://localhost:3000 to access it. The pod then has a containerPort of 3000.
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-web-deployment
labels:
app: GoWeb
spec:
replicas: 1
selector:
matchLabels:
app: GoWeb
template:
metadata:
labels:
app: GoWeb
spec:
containers:
- name: go-web
image: me/go-web:1.0.1
ports:
- containerPort: 3000
The Service is then an abstraction of that pod, that describes how to access 1 or many Pods running that service.
The nodePort of the service is 31024.
apiVersion: v1
kind: Service
metadata:
name: go-web-service
spec:
type: NodePort
selector:
app: GoWeb
ports:
- port: 3000
nodePort: 31024
The application is published on http://node-ip:node-port for the public to consume. Kubernetes manages the mappings between the node and the container in the background.
| User | -> | Node:nodePort | -> | Pod:containerPort |
The Kubernetes internal Service and Pod IP's are not often available to the outside world (unless you specifically set a cluster up that way). Whereas the nodes themselves will often carry an IP address that is routable/contactable.

Spring + Kubernetes minikube: Redirects work not as expected

I am learning the kubernetes using minikube and come up with idea to create simple service which will shorten my very long links. The service locally is working fine, but when I create a pod of that service in my cluster it's still working fine, but the redirect mechanism which spring provide in it's MVC module does not work as expected. While I try to redirect to an external URL e.g.
#GetMapping("/google")
public String redirectToGoogle(){
return "redirect:www.google.com"
}
it is redirecting me instead to "MINIKUBE_IP/www.google.pl".
What could cause that behaviour? It that somehow configurable?
Could you guide me how to solve that issue? If there will be a need to add anything I will do that with pleasure. I will attache the Deployment and the Service resources of my service.
For the first time I thought it's issue with Ingress, but tried it without ingress using simple port-forward but the issue it the same.
Using port-forward, NodePorts on services does not work either.
apiVersion: v1
kind: Service
metadata:
name: link-shortener
labels:
app: link-shortener
spec:
ports:
- port: 8080
protocol: TCP
selector:
app: link-shortener
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: link-shortener
labels:
app: link-shortener
spec:
replicas: 1
selector:
matchLabels:
app: link-shortener
template:
metadata:
labels:
app: link-shortener
spec:
containers:
- name: link-shortener
image: link-shortener
imagePullPolicy: Never
ports:
- containerPort: 8080
env:
- name: MONGO_DATABASE
valueFrom:
configMapKeyRef:
name: mongodb
key: database-name
- name: MONGO_USERNAME
valueFrom:
secretKeyRef:
name: mongodb
key: database-user
- name: MONGO_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb
key: database-password
Ok, I found how to deal with it. When I redirect to an adress with http:// or https:// prefixes it is working as intended. So I will just create validator which will check if these prefixes even exists before redirecting.

Resources