Home > database >  I am trying to Connect simple nginx pod to Nodeport service. but i am getting a error i.e, Connectio
I am trying to Connect simple nginx pod to Nodeport service. but i am getting a error i.e, Connectio

Time:05-03

root@Master:~# cat new-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: zero-pod
  labels:
    app: my-ns
spec:
  containers:
  - image: nginx
    name: zero-pod
root@Master:~# cat nps.yaml 
apiVersion: v1
kind: Service
metadata:
  name: my-ns
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: my-ns
  type: NodePort
root@Master:~# kubectl get svc my-ns
NAME    TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
my-ns   NodePort   10.105.183.144   <none>        80:30007/TCP   48m

root@Master:~# curl 10.2.0.4:30007
curl: (7) Failed to connect to 10.2.0.4 port 30007: Connection refused

I have created another simple helloworld pod connected to same nodeport service, its working.

root@Master:~# curl 10.2.0.4:30007
Hello, world!
Version: 2.0.0
Hostname: pod-theta

What is the basic thing, that i am missing in this or we cannot connect nginx server to nodeport at all.

If not, i see nginx is being connected to nodeport through deployment, how is that working, can someone please throw some light on this??

CodePudding user response:

The nginx service in the official nginx container listens on port 80, not on port 8080. You need to update the targetPort setting in your Service:

apiVersion: v1
kind: Service
metadata:
  name: my-ns
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: my-ns
  type: NodePort

While not absolutely necessary, I would define the port in your Pod manifest, possibly with a name, so that you end up with this Pod manifest:

apiVersion: v1
kind: Pod
metadata:
  name: zero-pod
  labels:
    app: my-ns
spec:
  containers:
  - image: nginx
    name: zero-pod
    ports:
      - containerPort: 80
        name: http

And this Service:

apiVersion: v1
kind: Service
metadata:
  name: my-ns
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: http
  selector:
    app: my-ns
  type: NodePort
  • Related