cancel
Showing results for 
Search instead for 
Did you mean: 
yasufumic
Cadet
Cadet
  • 6,154 Views

DO280 router / service targetPort and port

Jump to solution

Hi experts,

I'm running DO280 class this week, and got the question about p81 YAML file from a student.

---------------

kind: Route

...

...

spec:

 host: quotapp.apps.lab.example.com

 port:

    targetPort: 8080-tcp     <----

...

 

He wants to know the reason why the targetPort attribute is written in the file: Is it nessessary?

And I'd like to understand the difference between "port" and "targetPort" attribute in difinition of Router/Service also.

Does anyone know answer?

 

Thanks in advance.

Yasufumi

Labels (2)
1 Solution

Accepted Solutions
beelandc
Flight Engineer Flight Engineer
Flight Engineer
  • 6,142 Views

In the context of an OpenShift Service, targetPort is the mapping of the service traffic to the port inside the container. So for example, you might have a service that maps from port 80 on the service/route to port 8080 inside the container. In that case, a snip of your YAML file might look something like this:

apiVersion: v1
kind: Service
...
spec:
  ...
  ports:
  - name: web
    port: 80
    protocol: TCP
    targetPort: 8080
...

A route simply exposes a service at a host name, like www.example.com, so that external clients can reach it by name, and you can reference a specific service port mapping to expose like this, where web is the name of the port mapping defined in the service:

apiVersion: route.openshift.io/v1
kind: Route
...
spec:
  host: mywebapp.apps.openshift.com
  port:
    targetPort: web
  to:
    kind: Service
    name: frontend
    weight: 100

Specifically for the targetPort attribute on a route object's YAML, the OpenShift Route Object Schema defines the attribute as:

(intstr.IntOrString) The target port on pods selected by the service this route points to. If this is a string, it will be looked up as a named port in the target endpoints port list. Required

So it is just how you define what pod port to target via your route.

References:

  1. https://docs.openshift.com/container-platform/3.11/architecture/core_concepts/pods_and_services.html...
  2. https://docs.openshift.com/container-platform/3.11/dev_guide/routes.html
  3. https://docs.openshift.com/container-platform/3.11/rest_api/oapi/v1.Route.html#object-schema

View solution in original post

2 Replies
beelandc
Flight Engineer Flight Engineer
Flight Engineer
  • 6,143 Views

In the context of an OpenShift Service, targetPort is the mapping of the service traffic to the port inside the container. So for example, you might have a service that maps from port 80 on the service/route to port 8080 inside the container. In that case, a snip of your YAML file might look something like this:

apiVersion: v1
kind: Service
...
spec:
  ...
  ports:
  - name: web
    port: 80
    protocol: TCP
    targetPort: 8080
...

A route simply exposes a service at a host name, like www.example.com, so that external clients can reach it by name, and you can reference a specific service port mapping to expose like this, where web is the name of the port mapping defined in the service:

apiVersion: route.openshift.io/v1
kind: Route
...
spec:
  host: mywebapp.apps.openshift.com
  port:
    targetPort: web
  to:
    kind: Service
    name: frontend
    weight: 100

Specifically for the targetPort attribute on a route object's YAML, the OpenShift Route Object Schema defines the attribute as:

(intstr.IntOrString) The target port on pods selected by the service this route points to. If this is a string, it will be looked up as a named port in the target endpoints port list. Required

So it is just how you define what pod port to target via your route.

References:

  1. https://docs.openshift.com/container-platform/3.11/architecture/core_concepts/pods_and_services.html...
  2. https://docs.openshift.com/container-platform/3.11/dev_guide/routes.html
  3. https://docs.openshift.com/container-platform/3.11/rest_api/oapi/v1.Route.html#object-schema
yasufumic
Cadet
Cadet
  • 6,102 Views

Thanks beelandc-san,

I've now totaly clarified and I will answer for the student's question!

yasufumic

 

Join the discussion
You must log in to join this conversation.