How to get response from docker desktop k8s pod with browser?

8/8/2021

I'm using MacOS docker desktop. I'm trying to get response in browser from pod within simple Flask app. There is my configuration file.

apiVersion: v1
kind: Service
metadata:
  name: hello-python-service
spec:
  selector:
    app: hello-python
  ports:
  - port: 8080
    targetPort: 5000
    protocol: TCP
    nodePort: 30007
  type: NodePort


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-python-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-python
  template:
    metadata:
      labels:
        app: hello-python
    spec:
      containers:
      - name: hello
        image: hello-python:latest
        imagePullPolicy: Never
        ports:
        - containerPort: 5000
          hostPort: 8080

Response after kubectl get svc hello-python

NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
hello-python   ClusterIP   10.103.188.2   <none>        5000/TCP   65s

If I run this config and type in terminal kubectl port-forward <name of pod with app> 8080:5000 all works great - I got response from localhost:8080, but how to make it work out of the box without forwarding ports?

-- Ian
docker-desktop
kubernetes

2 Answers

8/8/2021

You've got some confusing mismatches there. In the YAML you have name: hello-python-service but then in your command line you have hello-python as the object name. If you did apply the manifest you showed then localhost:30007 should work.

-- coderanger
Source: StackOverflow

8/8/2021

You must create a Service with type: LoadBalancer and then execute the command minikube tunnel for assign a Local IP to your service (in this case is 127.0.0.1) so you can visit your app from outside, i deploy this example in an Macbook M1 and you can view the default page in http://127.0.0.1:8080

enter image description here

YAML:

apiVersion: v1
kind: Service
metadata:
  name: nginx-example-service
spec:
  selector:
    app: nginx-example
  ports:
  - port: 8080
    targetPort: 80
    protocol: TCP
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-example-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-example
  template:
    metadata:
      labels:
        app: nginx-example
    spec:
      containers:
      - name: hello
        image: nginx:latest
        ports:
        - containerPort: 80
-- Enrique Tejeda
Source: StackOverflow