1

I set up a local kubernetes cluster with minikube. On my cluster I have only one deployment runnning and one service attached to it. I used a NodePort on port 30100 to expose the service, so I can access it from my browser or via curl.

here is the python-server.yml file I use to setup the cluster:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-server-deployment
  namespace: kubernetes-hello-world
  labels:
    app: python-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: python-server
  template:
    metadata:
      labels:
        app: python-server
    spec:
      containers:
      - name: python-hello-world
        image: hello-world-python:latest
        imagePullPolicy: Never
        ports:
        - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: python-server-internal-service
  namespace: kubernetes-hello-world
spec:
  type: NodePort
  selector:
    app: python-server
  ports:
    - protocol: TCP
      port: 80
      targetPort: 5000
      nodePort: 30100

my python-hello-world image is based on this python file:

from http.server import BaseHTTPRequestHandler, HTTPServer


class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        html = """
<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
    <meta charset="utf-8">
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>
        """
        self.send_response(200)
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(bytes(html, "utf-8"))
    

def run():
    addr = ('', 5000)
    httpd = HTTPServer(addr, MyServer)
    httpd.serve_forever()


if __name__ == '__main__':
    run()

When I run the cluster I can as expected receive the hello world html with curl {node_ip}:30100. But when I try to access my service via my browser with the same ip:port I get a time out. I read that that can be caused by missing headers but I think I have all necessary ones covered in my python file, so what else could cause this?

Cake
  • 90
  • 1
  • 8

1 Answers1

1

It is not said that you reach the IP of your node (you should provide some more information about the environment if necessary).

But you could port forward the service and reach it easily.

Take a look here: https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

Some other doc: How kubectl port-forward works?

glv
  • 994
  • 1
  • 1
  • 15
  • Thanks, portforwarding the service worked. But I explicitly want to use an ingress to access my cluster, so can I do something like port forwarding to do that? – Cake Mar 27 '23 at 08:16
  • 1
    Yes of course, just create an ingress like this example: https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/service/networking/minimal-ingress.yaml --> adapting it to your scenario. – glv Mar 27 '23 at 10:02