I deployed an application running on nginx in Kubernetes, it's a simple static index.html
. I defined a button with a url to http://backservice:8080/action
. backservice
is a k8s service backing a Spring application.
The problem is, when I click on that button, nothing happens. backservice
is not hit. I expect a CORS
error but it seems like nginx blocks all outbound requests. I don't want to proxy the backend service into nginx.
Nginx config:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
Server conf:
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /svg/ {
}
location /assets/ {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
The backendservice
is in the same namespace as the nginx app.
I think the main problem is that backservice
is hostname which is resolved only inside the k8s cluster. If you want to make it accessible from the outside of the cluster it should be exposed using ingress or external services (LoadBalancer, NodePort).
Have you opened port 8080 on the ingress deployemnt ?
If your kubernetes is on public cloud . It should have a Load Balancer that used by Ingress nginx . You can configure that load balancer in the service of ingress deployment in kubernetes namespace kube-system
this is an example if you want to allow port 8080 on your ingress nginx to hit port 80 of your service :
kgs nginx-ingress-lb -n kube-system -o yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress-lb
namespace: kube-system
spec:
clusterIP: 172.21.12.220
externalTrafficPolicy: Cluster
ports:
- name: nginx-ingress-lb-443-443
nodePort: 32672
port: 443
protocol: TCP
targetPort: 443
- name: nginx-ingress-lb-8080-80
nodePort: 32026
port: 8080
protocol: TCP
targetPort: 80
Your static app runs in your browser. The browser isn't part of the k8s cluster so it is not aware of the URL http://backservice:8080/action
Expose your backend service using Ingress. For example https://backend.example.com/action
https://kubernetes.io/docs/concepts/services-networking/ingress/ (You can expose using Loadbalancer type too but I suggest Ingress)
Then change your frontend code to hit https://backend.example.com/action