Open shift use ingress and route - unix1998/technical_notes GitHub Wiki

To manage Ingress resources in OpenShift using the oc command line, we can create and apply an Ingress resource using oc create -f or oc apply -f with a YAML definition. Unlike Routes, which can be easily created using oc expose svc/service-name, Ingress resources need to be explicitly defined in a YAML file and then applied to the cluster.

Here is a step-by-step guide to create an Ingress resource using the oc command line:

1. Create an Ingress YAML file

First, create a YAML file for the Ingress resource. For example, create a file named my-ingress.yaml with the following content:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-service
            port:
              number: 8080
  tls:
  - hosts:
    - myapp.example.com
    secretName: my-tls-secret  # Assuming you have a TLS secret created

2. Apply the Ingress YAML file

Use the oc apply command to apply the Ingress YAML file to your OpenShift cluster:

oc apply -f my-ingress.yaml

3. Verify the Ingress

After applying the Ingress resource, you can verify its status using the following command:

oc get ingress my-ingress

This will show the details of the Ingress resource you just created, including its status and the associated endpoints.

Example Steps

Here is a full example workflow:

  1. Create the Ingress YAML file: Save the following content to a file named my-ingress.yaml.

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: my-ingress
    spec:
      rules:
      - host: myapp.example.com
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: my-service
                port:
                  number: 8080
      tls:
      - hosts:
        - myapp.example.com
        secretName: my-tls-secret
    
  2. Apply the Ingress resource:

    oc apply -f my-ingress.yaml
    
  3. Verify the Ingress resource:

    oc get ingress my-ingress
    

By following these steps, you can create and manage Ingress resources in OpenShift using the oc command line tool.

Red Hat documentation does describe using the oc create ingress command to create an Ingress resource directly from the command line. This is another convenient way to create Ingress resources without manually writing a YAML file. Here are the details on how to use this method:

Using oc create ingress

The oc create ingress command can be used to quickly create an Ingress resource with specific rules and TLS settings.

Example Command

oc create ingress simple --rule="foo.com/bar=svc1:8080,tls=my-cert"

Parameters

  • simple: The name of the Ingress resource.
  • --rule: Specifies the host, path, backend service, port, and optionally TLS settings.
    • foo.com/bar=svc1:8080: This part indicates that requests to foo.com with the path /bar should be directed to the service named svc1 on port 8080.
    • tls=my-cert: This specifies that TLS should be used, with my-cert as the TLS secret.

Detailed Steps

  1. Create an Ingress resource with a rule and TLS:

    oc create ingress simple --rule="foo.com/bar=svc1:8080,tls=my-cert"
    
  2. Verify the Ingress resource:

    oc get ingress simple
    

    This will show the Ingress resource details, confirming that it has been created.

Example Workflow

Here’s a complete example workflow using oc create ingress:

  1. Create a service (if not already created):

    oc expose deployment my-app --port=8080 --name=svc1
    
  2. Create the Ingress resource:

    oc create ingress simple --rule="foo.com/bar=svc1:8080,tls=my-cert"
    
  3. Verify the Ingress resource:

    oc get ingress simple
    

Advantages of Using oc create ingress

  • Simplicity: It’s a quick way to create an Ingress without needing to manually write and manage YAML files.
  • Speed: Reduces the time required to expose services, especially useful for simple or straightforward configurations.
  • Consistency: Ensures that the Ingress resource is created with the exact parameters specified in a single command.

Summary

Both methods—manually creating and applying a YAML file, and using the oc create ingress command—are valid and useful depending on your needs. The oc create ingress command is particularly useful for quick and simple setups, while the YAML method provides more flexibility for complex configurations.