DEV Community

Cover image for kind - Setting up an Ingress Controller - Part 5
Unni P
Unni P

Posted on • Originally published at iamunnip.hashnode.dev

kind - Setting up an Ingress Controller - Part 5

In this article we will talk about how we can configure Nginx Ingress Controller in our cluster

Introduction

  • An API object that manages external access to the cluster

  • Provides load balancing, SSL termination and name-based virtual hosting

  • Ingress controller is responsible for fulfilling the ingress

  • An ingress does not expose arbitrary ports or protocols

  • ingress-nginx is a popular ingress controller

Configuration

Create a cluster using below configuration file, where we have added extra port mappings and node labels.

extraPortMappings allow the local host to make requests to the Ingress controller over ports 80 and 443.

node-labels only allow the ingress controller to run on a specific node matching the label selector.

$ cat kind.yml 
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
name: dev
nodes:
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "ingress-ready=true"
  extraPortMappings:
  - containerPort: 80
    hostPort: 80
    protocol: TCP
  - containerPort: 443
    hostPort: 443
    protocol: TCP
- role: worker
- role: worker
Enter fullscreen mode Exit fullscreen mode
$ kind create cluster --config kind.yml 
Creating cluster "dev" ...
 ✓ Ensuring node image (kindest/node:v1.26.3) đŸ–ŧ
 ✓ Preparing nodes đŸ“Ļ đŸ“Ļ đŸ“Ļ  
 ✓ Writing configuration 📜 
 ✓ Starting control-plane 🕹ī¸ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
 ✓ Joining worker nodes 🚜 
Set kubectl context to "kind-dev"
You can now use your cluster with:

kubectl cluster-info --context kind-dev
Enter fullscreen mode Exit fullscreen mode

Deploy Nginx ingress to our cluster using the manifest file.

This manifest contains kind specific patches to forward the hostPorts to the ingress controller, set taint tolerations and schedule it to the custom-labeled node.

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
namespace/ingress-nginx created
serviceaccount/ingress-nginx created
serviceaccount/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
configmap/ingress-nginx-controller created
service/ingress-nginx-controller created
service/ingress-nginx-controller-admission created
deployment.apps/ingress-nginx-controller created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
Enter fullscreen mode Exit fullscreen mode

Now verify our ingress controller pod is up and running

$ kubectl -n ingress-nginx get pods --selector=app.kubernetes.io/component=controller
NAME                                        READY   STATUS    RESTARTS   AGE
ingress-nginx-controller-6bdf7bdbdd-gbdpc   1/1     Running   0          79s
Enter fullscreen mode Exit fullscreen mode

Create an Nginx pod using the below manifest file and verify its status

$ cat nginx.yml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    ports:
    - containerPort: 80
Enter fullscreen mode Exit fullscreen mode
$ kubectl apply -f nginx.yml 
pod/nginx created
Enter fullscreen mode Exit fullscreen mode
$ kubectl get pods nginx
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          20s
Enter fullscreen mode Exit fullscreen mode

Expose our Nginx pod to as ClusterIP service using the below manifest file

$ cat nginx-clusterip.yml 
apiVersion: v1
kind: Service
metadata:
  labels:
    run: nginx
  name: nginx
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: nginx
  type: ClusterIP
Enter fullscreen mode Exit fullscreen mode
$ kubectl apply -f nginx-clusterip.yml 
service/nginx created
Enter fullscreen mode Exit fullscreen mode
$ kubectl get svc nginx 
NAME    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
nginx   ClusterIP   10.96.16.194   <none>        80/TCP    20s
Enter fullscreen mode Exit fullscreen mode

Now create an Ingress resource using the below manifest file

$ cat nginx-ingress.yml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - pathType: Prefix
        path: /my-app
        backend:
          service:
            name: nginx
            port:
              number: 80
Enter fullscreen mode Exit fullscreen mode
$ kubectl apply -f nginx-ingress.yml 
ingress.networking.k8s.io/nginx created
Enter fullscreen mode Exit fullscreen mode
$ kubectl get ingress nginx
NAME    CLASS   HOSTS   ADDRESS   PORTS   AGE
nginx   nginx   *                 80      21s
Enter fullscreen mode Exit fullscreen mode

Access our application from our local machine and it worked

$ curl http://localhost/my-app
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
Enter fullscreen mode Exit fullscreen mode

Cleanup

Delete the cluster after use

$ kind delete cluster --name dev
Deleting cluster "dev" ...
Deleted nodes: ["dev-worker" "dev-worker2" "dev-control-plane"]
Enter fullscreen mode Exit fullscreen mode

Reference

https://kind.sigs.k8s.io/

https://kubernetes.io/docs/concepts/services-networking/ingress/

Top comments (0)