Richard Keene
Richard Keene

Reputation: 402

Kubernetes - This site can’t be reached localhost refused to connect

I have installed Kubernetes and Docker. Windows10. Then ran nginx in docker. Works great. Then changed the index.html to have my name in it and made my own image called rk_nginx_base. Works fine. Then changed the port for nginx from 80 to 9090.

If I run it in docker (The desktop UI), it works fine... I see the page in the browser at http://localhost:9090 and get the little icon to launch the browser in the docker UI. I can refresh the page all I want and it shows the page just fine.

Then I have a kubernetes yaml file ...

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
    app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: rk_nginx_base
        ports:
        - containerPort: 9090

Then I do

kubectl apply -f .\nginx-deployment.yaml

and I see it come up in docker UI. The first time I hit the server it works. (Probably cache in the browser after running in Docker) but any subsequent refreshes on any browser (Brave, Chrome, Edge) fails and get the error message

This site can’t be reached 
localhost refused to connect.

I have tried various solution including adding in a hostPort: 9090 below the containerPort, add a Service for NodePort, and tried kubectl expose -f .\nginx-deployment.yaml

Any ideas?

Upvotes: 0

Views: 990

Answers (1)

Richard Keene
Richard Keene

Reputation: 402

Turns out it was the command

kubectl expose mynginx

was needed to make the internal IP's get forwarded to the localhost IP.

Upvotes: 1

Related Questions