๐Ÿ”ง DevOps

Ingress

Ingress

Routes external traffic into Kubernetes services. L7 load balancing.

Detailed Explanation

Ingress is an API object that manages HTTP/HTTPS traffic from outside a Kubernetes cluster to internal services. Unlike a Service that simply exposes a port, Ingress provides L7 (application layer) features such as domain-based routing, TLS termination, and load balancing.

Ingress vs Service

  • ClusterIP: Accessible only from within the cluster
  • NodePort: Exposed externally on a port of each node (30000-32767)
  • LoadBalancer: Creates a cloud LB, one per service
  • Ingress: Domain/path-based routing to multiple services through a single LB

Key Features of Ingress

  • Host-based routing: Separate api.example.com and web.example.com
  • Path-based routing: /api and /static each go to a different service
  • TLS termination: HTTPS certificate management
  • Redirect: Automatically redirect HTTP to HTTPS
  • Rate Limiting: Request rate limiting (depends on the controller type)

Major Ingress Controllers

  • NGINX Ingress: Most widely used, stable
  • Traefik: Automatic service discovery, Let's Encrypt integration
  • HAProxy: High-performance load balancing
  • AWS ALB Controller: Uses AWS Application Load Balancer
  • Istio Gateway: Integrates with a service mesh

Code Example

Basic Ingress Resource

# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  namespace: production
  annotations:
    # NGINX Ingress ์„ค์ •
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: "10m"
spec:
  ingressClassName: nginx  # Ingress Controller ์ง€์ •
  tls:
    - hosts:
        - api.example.com
        - web.example.com
      secretName: example-tls  # TLS ์ธ์ฆ์„œ Secret
  rules:
    # ํ˜ธ์ŠคํŠธ ๊ธฐ๋ฐ˜ ๋ผ์šฐํŒ…
    - host: api.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 80
    - host: web.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web-service
                port:
                  number: 80

Path-Based Routing

# path-based-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: path-routing-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
    - host: example.com
      http:
        paths:
          # /api/* -> api-service
          - path: /api(/|$)(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: api-service
                port:
                  number: 8080
          # /static/* -> static-service
          - path: /static(/|$)(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: static-service
                port:
                  number: 80
          # ๋‚˜๋จธ์ง€ -> frontend
          - path: /
            pathType: Prefix
            backend:
              service:
                name: frontend-service
                port:
                  number: 80

TLS Certificate Secret

# tls-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: example-tls
  namespace: production
type: kubernetes.io/tls
data:
  tls.crt: LS0tLS1CRUdJTi...  # base64 encoded
  tls.key: LS0tLS1CRUdJTi...  # base64 encoded
---
# cert-manager๋กœ ์ž๋™ ๋ฐœ๊ธ‰ (๊ถŒ์žฅ)
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: example-cert
  namespace: production
spec:
  secretName: example-tls
  issuerRef:
    name: letsencrypt-prod
    kind: ClusterIssuer
  dnsNames:
    - api.example.com
    - web.example.com

Advanced NGINX Ingress Configuration

# advanced-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: advanced-ingress
  annotations:
    # Rate Limiting
    nginx.ingress.kubernetes.io/limit-rps: "10"
    nginx.ingress.kubernetes.io/limit-connections: "5"

    # CORS
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-origin: "https://example.com"
    nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, PUT, DELETE"

    # ํƒ€์ž„์•„์›ƒ
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "30"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "300"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "300"

    # WebSocket ์ง€์›
    nginx.ingress.kubernetes.io/proxy-http-version: "1.1"
    nginx.ingress.kubernetes.io/upstream-hash-by: "$request_uri"

    # ์ปค์Šคํ…€ ํ—ค๋”
    nginx.ingress.kubernetes.io/configuration-snippet: |
      add_header X-Frame-Options "SAMEORIGIN";
      add_header X-Content-Type-Options "nosniff";

    # Basic Auth
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/auth-secret: basic-auth
    nginx.ingress.kubernetes.io/auth-realm: "Authentication Required"
spec:
  ingressClassName: nginx
  rules:
    - host: admin.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: admin-service
                port:
                  number: 80

AWS ALB Ingress

# aws-alb-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: aws-alb-ingress
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
    alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:...
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}, {"HTTPS": 443}]'
    alb.ingress.kubernetes.io/ssl-redirect: "443"
    alb.ingress.kubernetes.io/healthcheck-path: /health
    alb.ingress.kubernetes.io/success-codes: "200,301"
spec:
  rules:
    - host: api.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 80

Practical Conversation Example

Backend developer
"I created a LoadBalancer for every service and my AWS bill is too high. If I have 10 services, that's 10 LBs."
DevOps engineer
"Use Ingress. With a single load balancer, you can route to multiple services based on domain or path. For example, send api.example.com to the API service and web.example.com to the frontend."
Backend developer
"How do you manage HTTPS certificates?"
DevOps engineer
"If you use it with cert-manager, it automatically issues and renews Let's Encrypt certificates. Just add one annotation to the Ingress and you're done. No need to renew certificates manually."

Cautions

Ingress Controller Required

Creating an Ingress resource alone will not work. You must install an Ingress Controller such as NGINX or Traefik first.

Annotation Compatibility

Annotations differ for each Ingress Controller. Annotations for NGINX Ingress do not work with Traefik.

PathType Caution

Choose the correct type among Prefix, Exact, and ImplementationSpecific. With Prefix, /api matches both /api and /api/users.

TLS Secret Namespace

A TLS Secret must be in the same namespace as the Ingress. You cannot reference a Secret in a different namespace.

Learn More