Sobes.tech
Middle

How to ensure user access to an application running in Kubernetes?

sobes.tech AI

Answer from AI

There are several main methods:

  1. Kubernetes Service Type: LoadBalancer. The cloud provider creates an external load balancer that directs traffic to your application's pods.

    apiVersion: v1
    kind: Service
    metadata:
      name: my-app-service
    spec:
      selector:
        app: my-app
      ports:
        - protocol: TCP
          port: 80
          targetPort: 8080
      type: LoadBalancer # Specifies the LoadBalancer type here
    
  2. Kubernetes Service Type: NodePort. Each cluster node opens a static port that forwards traffic to the service. Access to the application is through the IP address of any cluster node and this static port.

    apiVersion: v1
    kind: Service
    metadata:
      name: my-app-service
    spec:
      selector:
        app: my-app
      ports:
        - protocol: TCP
          port: 80
          targetPort: 8080
      type: NodePort # Specifies the NodePort type here
    

    This approach is less scalable and often not recommended for production.

  3. Ingress. A Kubernetes object that manages external access to services in the cluster. Requires an Ingress controller (e.g., Nginx Ingress, Traefik, HAProxy). Ingress allows routing based on domain names and paths.

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: my-app-ingress
    spec:
      rules:
        - host: myapp.mydomain.com # Domain name
          http:
            paths:
              - path: / # Path
                pathType: Prefix
                backend:
                  service:
                    name: my-app-service # Service name
                    port:
                      number: 80 # Service port
    
  4. Gateway API (more modern approach). An evolution of Ingress, providing a more expressive and extensible API for access management. Includes resources like GatewayClass, Gateway, HTTPRoute (and other route types).

    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: my-app-route
    spec:
      parentRefs:
        - name: my-gateway # Reference to Gateway resource
      hostnames:
        - "myapp.mydomain.com"
      rules:
        - matches:
            - path:
                type: Prefix
                value: /
          backendRefs:
            - name: my-app-service # Service name
              port: 80 # Service port
    

The choice of method depends on requirements for:

  • Scalability
  • Routing complexity
  • Security (SSL/TLS termination)
  • The cloud platform used
  • Need for centralized access management

For most production scenarios, LoadBalancer is used to provide an entry point (often for the Ingress controller) or directly, and Ingress (or Gateway API) for flexible routing and access management to various services in the cluster.