kubenetes deployment expose pods on a port

12/1/2019

My goal is to expose all my ports under one service.

My pod contains a containerised app that runs under port 80.

This is my attempt to create the deployment:

apiVersion: apps/v1
kind: Deployment
metadata: name: my-deployment
spec:
    replicas: 5
    selector:
        matchLabels:
            app: myapp
    template:
        metadata:
            name: my-pod
            labels:
                app: myapp
        spec:
            containers:
                - name: httd
                  image: httpd
                  imagePullPolicy: Always
                  ports:
                      - containerPort: 80

However, I am getting error:

error: error parsing deployment.yaml: error converting YAML to JSON: yaml: line 3: mapping values are not allowed in this context

Notes:

  • If I remove the ports section, the deployment will be created successfully but then the service (that I have it in another file and that I can share if needed), would be able to link a port on the node to a port in the pod because the pod doesn't expose any port (again it just the the container running on a port)
  • I went through this page, and it does say to use the containerPort so I don't know what I missed

Update

The error was in my deployment file: and after fixing it, I could create both the deployment and the service, but the service is still not exposed on the node. Here is my service definition:

apiVersion: v1
kind: Service
metadata:
    name: my-service
spec:
    selector:
            app: front-end
    ports:
        - port: 77
          targetPort: 80
          nodePort: 32766
    type: NodePort

As you can see, I am mapping the port 80 in the pod to port 32766 on the node, and when calling: localhost:32766 it returns 404

What did I miss?

Update

This is what the browser shows:

enter image description here

-- Jack Thomson
kubernetes

3 Answers

12/2/2019

I guess the problem is in your selector section of service.yaml file try this

apiVersion: v1
kind: Service
metadata:
    name: my-service
spec:
    selector:
            app: myapp
    ports:
        - port: 77
          targetPort: 80
          nodePort: 32766
    type: NodePort
-- shubham_asati
Source: StackOverflow

12/5/2019

In addition to what @shubam_asati posted, your service yaml has port: 77 and targetPort: 80. But your deployment container is port is 80. Change port value to the same value as targetPort (ie 80) and you should be able to connect to the app at localhost:<nodePort>.

-- Ruairios
Source: StackOverflow

12/1/2019

when calling: localhost:32766 it returns 404

this means that the app is actually responding to the request. But you sent the request for an URL that the app has not implemented. 404 Not Found is an Http status code that web servers respond when they don't have a path for the requested URL.

-- Jonas
Source: StackOverflow