Kubernetes service : randomly getting Connection refused
The issue was that 2 services in selector was using same label value - 'environment: dev' , and I assume this random connection was provoked, because it was balancing between one pod to another. Fixed labels values, now works perfectly.
When I run K8s with a NodePort, I don't have any problem. You can try first by using a proxy (port-forward) to your service and then your pod to ensure that all is working with the same behavior. If doing the port-forward to your pod directly works without any issue, then you might have an issue between your service and pod (e.g.: network policies such as too many calls in a short amount of time).
Regarding my nginx config, it's quite simple:
# /usr/share/nginx/html # cat /etc/nginx/conf.d/default.confserver { listen 80; # Optional listen [::]:80; server_name localhost; # Default configuration location / { root /usr/share/nginx/html; index index.html index.htm; # Forward everything to the react router try_files $uri $uri/ /index.html?$args; } # If we wish some custom error page later, we could also add them. error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}
Else, if you want to try with a verbatim nginx, simply use the nginx image (see kubernetes.io Cheat Sheet)
kubectl create deployment nginx --image=nginxkubectl create services nodeport nginx-svc --tcp 30666:80 # Nodeport should then be 30666 on the cluster