Skip to content

using targetPort with a name in service definition doesn't seem to work via route #6265

Closed

Description

@dustymabe

Using targetPort in the service definition to map to a named port in a pod definition doesn't seem to work. Everything looks like it works and the web interface even has 80/TCP → apacheport in the display, but I can never establish a connection. If I instead use targetPort : 8080 in openshift it works. Mapping to a name works in kubernetes-1.1.0-0.17.git388061f.fc23.x86_64 so I think I'm not doing something wrong, but I can't be sure. My target ose environment is 3.1.

Example Pod

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "labels": {
            "app": "helloapache"
        },
        "name": "helloapache"
    },
    "spec": {
        "containers": [
            {
                "image": "dustymabe/httpd:latest",
                "name": "helloapache",
                "ports": [
                    {
                        "name": "apacheport",
                        "containerPort": 8080,
                        "protocol": "TCP"
                    }
                ]
            }
        ]
    }
}

Example service

{
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "labels": {
            "name": "apacheservice"
        },
        "name": "apacheservice"
    },
    "spec": {
        "ports": [
            {
                "port": 80,
                "targetPort": "apacheport"
            }
        ],
        "selector": {
            "app": "helloapache"
        },
        "type": "LoadBalancer"
    }
}

Activity

  1. liggitt commented on Dec 10, 2015

    @liggitt
    Contributor

    How are you attempting to establish the connection?

  2. dustymabe commented on Dec 10, 2015

    @dustymabe
    MemberAuthor

    curl or firefox, neither work. I always get a 503 error

  3. liggitt commented on Dec 10, 2015

    @liggitt
    Contributor

    What do the endpoints for the service look like?

    oc get services -o json
    oc get endpoints -o json
    
  4. liggitt commented on Dec 10, 2015

    @liggitt
    Contributor

    and you are curling the service ip directly, not going through the API proxy, right?

  5. dustymabe commented on Dec 10, 2015

    @dustymabe
    MemberAuthor

    I am curling the hostname associated with the route that is associated with the service.

    route info

    # ./oc get route apacheservice -o json
    {
        "kind": "Route",
        "apiVersion": "v1",
        "metadata": {
            "name": "apacheservice",
            "namespace": "proj1",
            "selfLink": "/oapi/v1/namespaces/proj1/routes/apacheservice",
            "uid": "fbc1e3a5-9f89-11e5-bbd9-0e2e65752393",
            "resourceVersion": "18394421",
            "creationTimestamp": "2015-12-10T22:04:28Z",
            "labels": {
                "name": "apacheservice"
            },
            "annotations": {
                "openshift.io/host.generated": "true"
            }
        },
        "spec": {
            "host": "apacheservice-proj1.e8ca.engint.openshiftapps.com",
            "to": {
                "kind": "Service",
                "name": "apacheservice"
            },
            "port": {
                "targetPort": "apacheport"
            }
        },
        "status": {}
    }
    

    service info

    # ./oc get service apacheservice -o json                                                                                                                                            
    {
        "kind": "Service",
        "apiVersion": "v1",
        "metadata": {
            "name": "apacheservice",
            "namespace": "proj1",
            "selfLink": "/api/v1/namespaces/proj1/services/apacheservice",
            "uid": "e7264b46-9f89-11e5-bbd9-0e2e65752393",
            "resourceVersion": "18394205",
            "creationTimestamp": "2015-12-10T22:03:54Z",
            "labels": {
                "name": "apacheservice"
            }
        },
        "spec": {
            "ports": [
                {
                    "protocol": "TCP",
                    "port": 80,
                    "targetPort": "apacheport",
                    "nodePort": 31114
                }
            ],
            "selector": {
                "app": "helloapache"
            },
            "portalIP": "172.30.205.253",
            "clusterIP": "172.30.205.253",
            "type": "LoadBalancer",
            "sessionAffinity": "None"
        },
        "status": {
            "loadBalancer": {}
        }
    }
    

    endpoints info

    # ./oc get endpoints apacheservice -o json                                                                                                                                          
    {
        "kind": "Endpoints",
        "apiVersion": "v1",
        "metadata": {
            "name": "apacheservice",
            "namespace": "proj1",
            "selfLink": "/api/v1/namespaces/proj1/endpoints/apacheservice",
            "uid": "e72933e2-9f89-11e5-bbd9-0e2e65752393",
            "resourceVersion": "18394251",
            "creationTimestamp": "2015-12-10T22:03:54Z",
            "labels": {
                "name": "apacheservice"
            }
        },
        "subsets": [
            {
                "addresses": [
                    {
                        "ip": "10.1.16.21",
                        "targetRef": {
                            "kind": "Pod",
                            "namespace": "proj1",
                            "name": "helloapache",
                            "uid": "e71e2b92-9f89-11e5-bbd9-0e2e65752393",
                            "resourceVersion": "18394250"
                        }
                    }
                ],
                "ports": [
                    {
                        "port": 8080,
                        "protocol": "TCP"
                    }
                ]
            }
        ]
    }
    
  6. dustymabe commented on Dec 10, 2015

    @dustymabe
    MemberAuthor

    Note that the route was created using ./oc expose service apacheservice

  7. 0xmichalis commented on Dec 10, 2015

    @0xmichalis
    Contributor

    The problem is that the route uses a named target port and the associated endpoint is not. #5578 fixes this.

  8. 0xmichalis commented on Dec 10, 2015

    @0xmichalis
    Contributor

    In the meantime, @dustymabe can you try

    oc patch route/apacheservice -p '{"spec":{"port":{"targetPort":8080}}}'
    

    and try to connect?

  9. dustymabe commented on Dec 10, 2015

    @dustymabe
    MemberAuthor

    @Kargakis that patch command works

  10. liggitt commented on Dec 11, 2015

    @liggitt
    Contributor

    Does #5578 actually fix the issue with named ports, or just let you set a numeric target port on the route. Named ports are needed when you want services or routes to route to different port numbers on pods that are serving the same content.

    For that to work, the names of the ports need to make it into the endpoints somehow. Isn't that an upstream bug that needs fixing?

  11. 0xmichalis commented on Dec 11, 2015

    @0xmichalis
    Contributor

    I think that the problem is this. Which is an upstream bug, yes. In the meantime, making oc expose set a numeric target port (I dont think users generate routes manually any more) and beefing up oc status to report (even if we fix endpoints upstream, we probably still want the oc status changes) seems fine?

  12. liggitt commented on Dec 11, 2015

    @liggitt
    Contributor

    It just seems like the route is picking up the name of the pod port, when that is not what will be surfaced in the endpoint. In this case, the route should have been using the endpoint port named "", right?

  13. 0xmichalis commented on Dec 11, 2015

    @0xmichalis
    Contributor

    It just seems like the route is picking up the name of the pod port, when that is not what will be surfaced in the endpoint. In this case, the route should have been using the endpoint port named "", right?

    I am not sure about that. Maybe @pweil- can weigh in

  14. liggitt commented on Dec 11, 2015

    @liggitt
    Contributor

    Summary:

    • if a route has a numeric targetPort, that matches the containerport number.
    • if a route has a named targetPort, that matches the serviceport name (NOT the containerport name)

    My preference would be to switch routes to use the serviceport names, so that changes to the service port mapping get picked up by the route automatically.

    @Kargakis @spadgett I think we should change oc expose and the UI route creation to set a route's targetPort to the name of the service port by default.

    We could also look into requesting that the service port number be added to the endpoint info, which would give us a chance of letting a routeport numeric targetPort map to a service port number without needing to watch services as well

  15. 7 remaining items

  16. changed the title [-]using targetPort with a name in service definition doesn't seem to work[/-] [+]using targetPort with a name in service definition doesn't seem to work via route[/+] on Dec 11, 2015
  17. thincal commented on Jan 20, 2016

    @thincal
    • if a route has a numeric targetPort, that matches the containerport number.
    • if a route has a named targetPort, that matches the serviceport name (NOT the containerport name)

    but, if a route without the targetPort, which port will be matched if the service/pod with more than one port defined ? thanks.

  18. liggitt commented on Jan 20, 2016

    @liggitt
    Contributor

    The route round-robins among all the service ports.

  19. thincal commented on Jan 20, 2016

    @thincal

    if there are more than one port could serve the request, which will be selected eventually ? just randomly ?

  20. liggitt commented on Jan 20, 2016

    @liggitt
    Contributor

    It is effectively random, yes. That behavior is probably not what you want... in most cases, the route should target a specific port

  21. gWOLF3 commented on Dec 31, 2019

    @gWOLF3

    I can confirm that this is still an issue. The actual behavior does not match the expected, and doesn't make sense. If we define a targetPort: , that should match to a containerPort name on the pod.

  22. gWOLF3 commented on Dec 31, 2019

    @gWOLF3

    /bump

  23. k79e commented on Mar 11, 2020

    @k79e

    Yes. Same too. IDK why but I viewed the service port from container and find that it's can connect.
    So it's not caused by connection problem of services.
    I find that this only happen when port of service is different from container's.

    I don't use port name but the number.

  24. k79e commented on Mar 11, 2020

    @k79e

    if a route has a numeric targetPort, that matches the containerport number.
    if a route has a named targetPort, that matches the serviceport name (NOT the containerport name)

    But when create route from web console the number port selected is the service port.
    e.g the page shows 80->1234 then route use 80 port of service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions