0
0
Kubernetesdevops~5 mins

Ingress resource definition in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to let users access your apps running inside Kubernetes from outside, you need a way to route their requests. An Ingress resource defines rules to control this routing, so you can direct traffic to different apps based on the URL or host name.
When you have multiple apps running in the same Kubernetes cluster and want to share one external IP address for all.
When you want to route traffic to different services based on the URL path, like example.com/app1 and example.com/app2.
When you want to use HTTPS with your apps by configuring TLS certificates in one place.
When you want to control access to your apps with rules like blocking or allowing certain hosts.
When you want to simplify exposing your services without creating many LoadBalancer or NodePort services.
Config File - ingress.yaml
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: default
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /app1
        pathType: Prefix
        backend:
          service:
            name: app1-service
            port:
              number: 80
      - path: /app2
        pathType: Prefix
        backend:
          service:
            name: app2-service
            port:
              number: 80
  tls:
  - hosts:
    - example.com
    secretName: example-tls-secret

This file defines an Ingress resource named example-ingress in the default namespace.

The rules section routes requests for example.com/app1 to the app1-service and example.com/app2 to the app2-service.

The tls section enables HTTPS for example.com using a TLS secret named example-tls-secret.

Commands
This command creates the Ingress resource in the cluster using the configuration file. It sets up the routing rules and TLS settings.
Terminal
kubectl apply -f ingress.yaml
Expected OutputExpected
ingress.networking.k8s.io/example-ingress created
This command checks that the Ingress resource was created and shows its current status and assigned IP or hostname.
Terminal
kubectl get ingress example-ingress
Expected OutputExpected
NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress <none> example.com 192.168.1.100 80,443 10s
This command shows detailed information about the Ingress resource, including rules, backend services, and events.
Terminal
kubectl describe ingress example-ingress
Expected OutputExpected
Name: example-ingress Namespace: default Address: 192.168.1.100 Default backend: <none> Rules: Host Path Backends ---- ---- -------- example.com /app1 app1-service:80 (10.1.1.10:80) /app2 app2-service:80 (10.1.1.11:80) Annotations: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Sync 10s ingress-controller Scheduled for sync
Key Concept

If you remember nothing else from this pattern, remember: an Ingress resource lets you control how external traffic reaches your Kubernetes services by defining rules and TLS settings in one place.

Common Mistakes
Not specifying the correct pathType in the paths section
Without the correct pathType (like Prefix), the routing may not work as expected and requests might not reach the intended service.
Always set pathType to Prefix or Exact depending on your routing needs; Prefix is common for routing all paths under a URL.
Forgetting to create or reference a valid TLS secret for HTTPS
If the TLS secret does not exist or is misnamed, HTTPS will fail and users may get security warnings or no encryption.
Create the TLS secret with your certificates and reference it correctly in the tls section of the Ingress.
Applying the Ingress resource without an Ingress controller installed
The Ingress resource alone does nothing without a controller to implement the rules, so traffic won't be routed.
Make sure an Ingress controller like NGINX or Traefik is installed and running in your cluster before applying Ingress resources.
Summary
Create an Ingress resource YAML file to define routing rules and TLS settings.
Apply the Ingress resource with kubectl apply to create it in the cluster.
Use kubectl get and describe commands to verify the Ingress status and details.