Setting up Ingress-NGINX on CMX Clusters

Setting up Ingress-NGINX on CMX Clusters

This guide walks through setting up ingress-nginx on CMX clusters to route HTTP traffic to your applications. Ingress-NGINX acts as a reverse proxy and load balancer, allowing you to expose multiple services through a single entry point.

Prerequisites

  • A running CMX cluster
  • kubectl configured to access your cluster
  • Basic familiarity with Kubernetes concepts

Step 1: Install ingress-nginx

Deploy the ingress-nginx controller using the baremetal provider configuration:

bash

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.13.1/deploy/static/provider/baremetal/deploy.yaml

This creates the ingress controller in the ingress-nginx namespace with all necessary components including the controller deployment, services, and RBAC resources.

Step 2: Check the NodePort Configuration

After installation, check the service configuration to note the NodePort assignments:

bash

kubectl get svc -n ingress-nginx

You should see output similar to:

NAME                                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             NodePort    10.43.94.22     <none>        80:31868/TCP,443:32022/TCP   7m21s
ingress-nginx-controller-admission   ClusterIP   10.43.232.249   <none>        443/TCP                      7m21s

Note the NodePort numbers (31868 for HTTP and 32022 for HTTPS in this example). These ports will be randomly assigned and may differ in your installation.

Step 3: Deploy a Sample Application

Create a sample nginx deployment and service to test the ingress configuration. Save the following as example.yaml:

yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.14.2
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80

Deploy the application:

bash

kubectl apply -f example.yaml

Step 4: Expose the Ingress Port and Create an Ingress Resource

Create an ingress resource to route traffic to your service. Use either host based routing or path based routing and save the configuration as ingress.yaml.

Path Based Routing

Use the Replicated CLI to expose the ingress NodePort (replace with your cluster ID and the actual NodePort from Step 2):

bash

replicated cluster port expose <cluster-id> --port 31868 --protocol http

Replace <cluster-id> with your actual cluster ID and 31868 with the HTTP NodePort from your ingress-nginx service.

This will route to a specific path under the exposed domain: http://your-cluster-url.ingress.replicatedcluster.com/example

yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - http:
        paths:
          - path: /example
            pathType: Prefix
            backend:
              service:
                name: nginx-service
                port:
                  number: 80

Host Based Routing

Use the Replicated CLI to expose the ingress NodePort (replace with your cluster ID and the actual NodePort from Step 2) with a wildcard DNS configuration:

bash

replicated cluster port expose <cluster-id> --port 31868 --protocol http --wildcard

Replace <cluster-id> with your actual cluster ID and 31868 with the HTTP NodePort from your ingress-nginx service. This will route to a specific subdomain of the exposed domain: http://example.your-cluster-url.ingress.replicatedcluster.com/

yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: example.your-cluster-url.ingress.replicatedcluster.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: nginx-service
                port:
                  number: 80

Apply the ingress configuration:

bash

kubectl apply -f ingress.yaml

Step 5: Test the Configuration

Once the port is exposed, you can access your application using the provided URL with the configured path:

bash

curl "http://your-cluster-url.ingress.replicatedcluster.com/example"

Or via the subdomain if you’ve configured host based routing:

curl "http://example.your-cluster-url.ingress.replicatedcluster.com"

You should receive the nginx welcome page:

html

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Adding Additional Services

You can create additional ingress resources to route to other services deployed on your cluster. Simply create new ingress resources with different paths or hosts:

Path Based Routing

yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: another-service-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - http:
        paths:
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 8080

Host Based Routing

yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: another-service-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: api.your-cluster-url.ingress.replicatedcluster.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 8080

Important Notes

  • Resource limits: Consider setting appropriate resource requests and limits for the ingress controller in production environments.

This setup provides a flexible foundation for routing HTTP traffic to multiple services within your CMX cluster through a single exposed port.

1 Like