Expose a web app on port 80 using Kubernetes and minikube

7/18/2020

I'm studying Kubernetes using my laptop(no cloud). I started with minikube by following the docs at kubernetes(.)io. I'm not sure what I'm missing since I can only access my web app using high TCP port 32744 and not the standard port 80. I want to be able to access my web app using a web browser by visiting http://ipaddress.

Here is my deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
    name: webapp-deployment
spec:
    replicas: 2
    selector:
        matchLabels:
            app: nodejs
    template:
        metadata:
            labels:
                app: nodejs
        spec:
            containers:
            - name: nodeapp
              image: localregistry/nodeapp:1.0
              ports:
              - containerPort: 3000
$ minikube service webapp-deployment
|-----------|-------------------|-------------|---------------------------|
| NAMESPACE |       NAME        | TARGET PORT |            URL            |
|-----------|-------------------|-------------|---------------------------|
| default   | webapp-deployment |        3000 | http://192.168.64.2:32744 |
|-----------|-------------------|-------------|---------------------------|
-- devwannabe
kubernetes
minikube

2 Answers

7/18/2020

This is just how Kubernetes works.

3000 is the port in your container and 32744 is the NodePort where your application got exposed. There are multiple reasons for this, one of them is that port 80 and 443 are standard reserved ports 🔒🔒 for web services and Kubernetes needs to be able to run many containers and services. Another reason is that ports 0-1024 are restricted to root only on *nix systems 🚫🚫.

If you really would like to serve it on port 80 on your local machine I would just set up something like Nginx and proxy the traffic to http://192.168.64.2:32744

# nginx.conf
...
location / {
    proxy_set_header Accept-Encoding "";
    proxy_pass http://192.168.64.2:32744;
}
...

Or you can do port-forward from a non-restricted local post as the other answer here suggested.

✌️

-- Rico
Source: StackOverflow

7/18/2020

You can use kubectl port-forward for this.

$ kubectl port-forward -n <namespace> pod/mypod 8888:5000

What it does?

Listen on port 8888 locally, forwarding to port 5000 in the pod.

NB: You can also use port-forward with k8s services too using kubectl port-forward svc/<service-name> PORT:PORT.

-- Kamol Hasan
Source: StackOverflow