Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

kubernetes - Unable to forward traffic using NodePort

I have an application running inside minikube K8 cluster. It’s a simple REST endpoint. The issue is after deployment I am unable to access that application from my local computer.

Using http://{node ip}:{node port} endpoint.

However, if I do:

kubectl port-forward (actual pod name) 8000:8000

The application becomes accessible at: 127.0.0.1:8000 from my local desktop.

Is this the right way? I believe this isn't the right way? as I am directly forwarding my traffic to the pod and this port forwarding won't remain once this pod is deleted.

What am I missing here and what is the right way to resolve this?

I have also configured a NodePort service, which should handle this but I am afraid it doesn’t seem to be working:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: rest-api
  name: rest-api-np
  namespace: rest-api-namespace
spec:
  type: NodePort
  ports:
  - port: 8000
  selector:
    app: rest-api


apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: rest-api
  name: rest-api-deployment
  namespace: rest-api-namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rest-api
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: rest-api
    spec:
      containers:
      - image: oneImage:latest
        name: rest-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are having issues because your service is placed in default namespace while your deployment is in rest-api-namespace namespace.

I have deploy you yaml files and when the describe the service there were no endpoints:

?  k describe svc rest-api-np 
Name:                     rest-api-np
Namespace:                default
Labels:                   app=rest-api
Annotations:              <none>
Selector:                 app=rest-api
Type:                     NodePort
IP:                       10.100.111.228
Port:                     <unset>  8000/TCP
TargetPort:               8000/TCP
NodePort:                 <unset>  31668/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Solution for that is to create service in the the same namespace. Once you do that, an ip address and port will appear in the Endpoints field:

? k describe svc -n rest-api-namespace rest-api-np 
Name:                     rest-api-np
Namespace:                rest-api-namespace
Labels:                   app=rest-api
Annotations:              <none>
Selector:                 app=rest-api
Type:                     NodePort
IP:                       10.99.49.24
Port:                     <unset>  8000/TCP
TargetPort:               8000/TCP
NodePort:                 <unset>  32116/TCP
Endpoints:                172.18.0.3:8000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Alternative way is to add endpoints manually:

apiVersion: v1
kind: Endpoints
metadata:
  name: my-service # please note that endpoints and service needs to have the same name
subsets:
  - addresses:
      - ip: 192.0.2.42 #ip of the pod 
    ports:
      - port: 8000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...