Exposing an application through the preconfigured ingress controller

Expose an NGINX deployment through a ClusterIP service and an ingress resource, and open the application in your browser by using an sslip.io host name.

  • You have a project with a deployed Kubernetes cluster in the Ready status.
  • You have a kubeconfig file for the cluster and a local terminal with kubectl. For more information, see Kubeconfig files in Overview.
  1. Save the following deployment and service in a file named ingress-test.yaml.

    The service type is ClusterIP, because the ingress controller, not a load balancer, routes the traffic to the service. The labels on the Pods must match the selector in the service. In this example, both use the label app: ingress-test.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: ingress-test
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: ingress-test
      template:
        metadata:
          labels:
            app: ingress-test # All pods created by this deployment have the label app: ingress-test
        spec:
          containers:
            - name: my-container
              image: nginx:latest
              ports:
                - containerPort: 80
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: ingress-test-svc
    spec:
      selector:
        app: ingress-test # Send packets to pods that have the label app: ingress-test
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: ClusterIP
  2. Save the following ingress resource in a file named ingress.yaml.

    The ingressClassName: ccf setting selects the preconfigured ingress controller. The rule has no host, so it matches any host name that resolves to the Access IP.

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-test
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /
    spec:
      ingressClassName: ccf
      rules:
        - http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: ingress-test-svc
                    port:
                      number: 80
  3. Apply both files:
    kubectl apply -f ingress-test.yaml -f ingress.yaml
  4. Optional: Set up DNS.

    Point your DNS records to the bastion IP address of your cluster. To find this address, open your project, and see Access IP in General Cluster Information.

  5. Open the application in your browser.

    If you do not have a custom DNS domain, use an sslip.io wildcard domain, for example http://x.x.x.x.sslip.io, where x.x.x.x is the bastion IP address.

The browser displays the application that your deployment serves, which is the NGINX welcome page.