OpenShift - Create Ingress Route

If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.

An OpenShift route or an Ingress route will provide a URL such as http://my-route-my-project.apps.openshift.example.com:8080 which is used to route a request onto a service, which is then routed onto a pod, and then to the container in the pod, and finally to the application running in the container.

 

Like this.

 

Egress provides a way for an application deployed on OpenShift to access an external URL, such as http://www.example.com.

 

The oc get services command can be used to list the services that have been created in a project. Let's say you want to create an Ingress route that will be used to route requests to foo-service.

~]$ oc get services
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP    PORT(S)                      AGE
foo-service   ClusterIP   172.30.232.182   <none>         8080/TCP,8443/TCP,8778/TCP   177d
bar-service   ClusterIP   172.30.179.246   <none>         8080/TCP,8443/TCP,8778/TCP   240d

 

The oc create ingress command can be used to create an Ingress route that will be used to route requests onto a service.

  • The syntax of --rule is <url><path>=<service>:<port> (for example - my-ingress-route.apps.openshift.example.com/my-endpoint=foo-service:8080).
    • If the * (wildcard) character is NOT used in --rule, the pathType of the ingress route will be Exact and an OpenShift route will NOT be created.
    • If the * (wildcard) character is used in --rule, the pathType of the ingress route will be Prefix and an an OpenShift route will be created. In this scenario, if the ingress route is not annotated with termination, the route will not be set as an insecured route, not using SSL or TLS termination. --annotation can be included to set the OpenShift route as an edge, passthrough, or reencrypt route
~]$ oc create ingress my-ingress-route --rule my-ingress-route.apps.openshift.example.com/my-endpoint*=foo-service:8080 --default-backend foo-service:8080 --annotation route.openshift.io/termination=edge
ingress.networking.k8s.io/my-ingress-route created

 

Or, a JSON or YAML file that contains key value pairs can be used to create an ingress route. These files are known as templates. For example, let's say you have a YAML file named ingress.yml that contains the following markup, using host from the oc get ingresses.config command.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress-route
  namespace: my-project
  annotations:
    route.openshift.io/termination: passthrough
spec:
  defaultBackend:
    service:
      name: my-service
      port:
        number: 8080
  rules:
  - host: my-ingress-route.apps.openshift.example.com
    http:
      paths:
      - backend:
          service:
            name: my-service
            port:
              number: 8080
        path: /my-endpoint
        pathType: Exact
status:
  loadBalancer: {}

 

The oc apply or oc create command with the -f or --filename option can be used to create the ingress route using the template JSON or YAML file.

The oc replace command can be used to replace an ingress route using a new or updated template JSON or YAML file.

The oc edit command can be used to update an ingress route template YAML file.

~]$ oc apply --filename ingress.yml
ingress.networking.k8s.io/my-ingress-route created

 

The oc get ingress command can then be used the list the Ingress routes in the project/namespace.

~]$ oc get ingress
NAME                CLASS    HOSTS                                            ADDRESS   PORTS   AGE
my-ingress-route    <none>   my-ingress-route.apps.openshift.example.com                80      15m

 

The oc describe ingress command can be used to show more details about the ingress route.

~]$ oc describe ingress my-ingress-route
Name:             my-ingress-route
Namespace:        my-project
Address:          
Default backend:  foo-service:8080 (10.131.4.14:8080)
Rules:
  Host                        Path          Backends
  ----                        ----          --------
  apps.openshift.example.com  /my-endpoint  foo-service:8080 (10.131.4.14:8080)
Annotations:              <none>
Events:                   <none>

 

If you set the ingress route with pathType: Prefix, then there should be an OpenShift route. If you edit the ingress route, a new OpenShift route will automatically be recreated.

~]# oc get routes
NAME                     HOST/PORT                                     PATH           SERVICES      PORT       TERMINATION   WILDCARD
my-ingress-route-vfm8r   my-ingress-route-apps.openshift.example.com   /my-endpoint   foo-service   8080-tcp   passthrough   None

 

I'm not sure why, but I have to label the route to get the route exposed on the default router

oc label ingress my-ingress-route route-type=default

 

And curl can be used to submit a request to the ingress route, which should forward the request onto the service, and then onto the pod.

curl -v http://my-ingress-route.apps.openshift.example.com/my-endpoint

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 21003 in the box below so that we can be sure you are a human.