Why is the connection refused each time I try to connect to a nodeport exposed service on MicroK8s?
Here is the following deployment, service and ingress:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: 'nginx'
ports:
- containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
name: nginx
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: nginx
type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx.info
labels:
app: 'nginx'
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: 'nginx.plz'
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: 'nginx'
port:
number: 80
I've included the following line my /etc/hosts
file as well:
127.0.0.1 nginx.plz
However, when I try to reach the site I continue to get the following error:
curl -v -H "host: nginx.plz" 10.152.183.60
* Trying 10.152.183.60...
* TCP_NODELAY set
* Connection failed
* connect to 10.152.183.60 port 80 failed: Operation timed out
* Failed to connect to 10.152.183.60 port 80: Operation timed out
* Closing connection 0
curl: (7) Failed to connect to 10.152.183.60 port 80: Operation timed out
Is anyone familiar with setting this up on microk8s? I did see a few other questions such as this one: https://serverfault.com/questions/1032572/microk8s-deploy-application-not-working but it didn't have a solution
EDIT: I have enabled the microk8s ingress as well.
i just deployed your configuration on my local microk8s and was able to hit the endpoint. The only difference i see is that I have enabled Microk8s ingress (https://microk8s.io/docs/addon-ingress),with the ingress add on enabled, it creates a namespace called ingress with ingress controller and it which watches for new Ingress rules (you have already defined them) and configures its underlying proxy to enact the corresponding routes.
To enable ingress on microk8s, do the below, everthing else is taken care by Microk8s. Our ingress resource has the rules, ingress controller will map it.
microk8s enable ingress
k get all -n ingress
NAME READY STATUS RESTARTS AGE
pod/nginx-ingress-microk8s-controller-rmgs4 1/1 Running 1 1d
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/nginx-ingress-microk8s-controller 1 1 1 1 1 <none> 1d
Also, note your ingress addon can be configured to expose TCP and UDP services by editing the nginx-ingress-tcp-microk8s-conf and nginx-ingress-udp-microk8s-conf ConfigMaps respectively, and then exposing the port in the Ingress controller.
k get cm -n ingress
NAME DATA AGE
nginx-load-balancer-microk8s-conf 0 1d
nginx-ingress-udp-microk8s-conf 0 1d
nginx-ingress-tcp-microk8s-conf 8 1d
ingress-controller-leader-public 0 1d
The other solution to enable the traffic is to enable the metallb loadbalancer. (https://microk8s.io/docs/addon-metallb). Once done, check your ingress/service (kubectl get svc/ingress) and it should show your endpoint either of the solution should work without much manual intervention.
curl http://10.152.183.102:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>