The Ingress Object via API
In the previous chapter, we explored Kubernetes Services (ClusterIP
, NodePort
) and how they provide stable internal IP addresses and basic load balancing for Pods. While NodePort
and type: LoadBalancer
Services can expose applications outside the cluster, they operate primarily at Layer 4 (TCP/UDP) and have limitations:
Each
LoadBalancer
Service typically provisions a separate cloud load balancer, which can be costly.NodePort
exposes services on high-numbered ports across all nodes, which isn't ideal for standard web traffic (ports 80/443).Neither Service type inherently understands HTTP/S concepts like hostnames, URL paths, or TLS termination for routing traffic.
This is where Kubernetes Ingress comes in. Ingress provides a way to manage external access to services within the cluster, specifically focusing on Layer 7 (HTTP/S) routing. It acts as a smart router or reverse proxy, allowing you to define rules that direct external HTTP/S traffic to different internal Services based on requested hostnames or URL paths.
Critically, the Ingress resource itself doesn't do anything on its own. It's a configuration object that defines routing rules. You need an Ingress Controller (a separate application running in your cluster, like Nginx Ingress, Traefik, HAProxy Ingress, etc.) that reads these Ingress resources and configures the underlying proxy (e.g., Nginx) to implement the specified rules.
In this chapter, we'll focus on creating and managing Ingress resources programmatically using client-go
. Let's start by examining the structure of the Ingress object in the API.
The Ingress
Object via API (networking.k8s.io/v1.Ingress
)
Ingress
Object via API (networking.k8s.io/v1.Ingress
)The stable version of the Ingress resource resides in the networking.k8s.io
API group, version v1
. When working with client-go
, you'll primarily interact with the networkingv1.Ingress
struct (k8s.io/api/networking/v1
).
Like other Kubernetes objects, the key definitions are within the metadata
and spec
fields.
metadata
Includes the standard fields like name
, namespace
, labels
, and annotations
. Annotations are often heavily used by specific Ingress Controllers to configure controller-specific behavior (e.g., rewrite rules, authentication, timeouts) beyond the standard Ingress spec.
spec
(v1.IngressSpec
)
This defines the desired state of the Ingress routing rules and configuration.
spec.ingressClassName
(string, optional but standard practice): This field links the Ingress resource to a specific Ingress Controller installed in the cluster. AnIngressClass
resource (a separate cluster-scoped object) defines parameters for an Ingress Controller. The Ingress Controller typically only processes Ingress resources that reference its associatedIngressClassName
. If omitted, behavior might depend on whether a default IngressClass is configured in the cluster, but explicitly setting it is recommended for clarity and predictability.spec.rules
([]v1.IngressRule
): This is the core of the Ingress, defining the routing logic. It's a list, allowing multiple rules within one Ingress resource. EachIngressRule
contains:host
(string, optional): If specified, this rule only applies to requests matching this hostname (e.g.,myapp.example.com
). Wildcards (like*.example.com
) are allowed. If omitted, the rule applies to all hostnames routed to the Ingress Controller.http
(*v1.HTTPIngressRuleValue
): Contains the path-based routing rules for the specified host (or for all hosts ifhost
is omitted). This has apaths
field:paths
([]v1.HTTPIngressPath
): A list of path rules, processed in order by many controllers. EachHTTPIngressPath
has:path
(string): The URL path prefix or exact path to match (e.g.,/api
,/login
).pathType
(string): Specifies how thepath
should be matched. Crucial types:Prefix
: Matches URL paths prefixed by thepath
value (e.g.,/api
matches/api/users
,/api/
). This is common.Exact
: Matches the URL path exactly.ImplementationSpecific
: Matching depends on theingressClassName
. Usually defaults to behavior similar toPrefix
.
backend
(v1.IngressBackend
): Defines where to forward requests matching this host/path combination. The key part is theservice
field:service
(*v1.IngressServiceBackend
): Specifies the target Kubernetes Service.name
(string): The name of the Service to forward traffic to.port
(v1.ServiceBackendPort
): Specifies the port on the target Service. Can be defined by:number
(int32): The specific port number of the Service.name
(string): The name of the port defined in the Service'sspec.ports
. Using the name is generally more robust.
spec.tls
([]v1.IngressTLS
, optional): Configures TLS termination. The Ingress Controller terminates the encrypted connection from the client using the specified certificate, then forwards unencrypted (usually) traffic to the backend Service/Pods. This is a list, allowing different certificates for different hosts. EachIngressTLS
entry contains:hosts
([]string, optional): A list of hostnames covered by the TLS certificate specified insecretName
. The certificate must be valid for these hosts. If omitted, the certificate is used for any host in the rules that doesn't have a specific TLS entry (often used for a default certificate).secretName
(string): The name of a Kubernetes Secret residing in the same namespace as the Ingress resource. This Secret must be of typekubernetes.io/tls
and contain thetls.crt
(certificate chain) andtls.key
(private key) data. The Ingress Controller needs permission to read this Secret.
spec.defaultBackend
(*v1.IngressBackend
, optional): Specifies a default Service to handle any requests that don't match any of the definedrules
. If this is not set and no rules match, the Ingress Controller typically returns a 404 Not Found error.
The Ingress resource provides a powerful way to declare how external HTTP/S traffic should be routed to internal services. By creating and managing these objects using client-go
, you can programmatically control application exposure, automate TLS certificate management integration, and build sophisticated traffic management solutions. Next, we'll look at the concept of Ingress Controllers in slightly more detail.
Last updated
Was this helpful?