To access NGINX Ingress on our local Kubernetes setup, we can use NodePort or port forwarding. Using NodePort lets us reach our NGINX Ingress Controller through a specific port on our local machine. On the other hand, we can use kubectl port-forward to access the Ingress directly. This method does not change service settings, so it is a good choice for local testing.
In this article, we will show you the steps to access NGINX Ingress on our local Kubernetes setup. We will talk about these topics:
- How to Install NGINX Ingress Controller on Local Kubernetes
- How to Configure Ingress Resources for NGINX in Local Kubernetes
- How to Expose NGINX Ingress Controller Using NodePort
- How to Use Port Forwarding to Access NGINX Ingress Locally
- How to Verify NGINX Ingress Connectivity on Local Kubernetes
- Frequently Asked Questions
At the end of this guide, we will understand how to manage and access NGINX Ingress in our local environment. This will make our Kubernetes experience better.
How to Install NGINX Ingress Controller on Local Kubernetes
To install the NGINX Ingress Controller on our local Kubernetes setup, we can use Helm. Helm helps us manage Kubernetes packages easily. Let’s go through the steps:
Prerequisites
- First, we need to have Helm installed. If we don’t have it yet, we can install it with this command:
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash- Next, we need a running local Kubernetes cluster. We can set this up using Minikube or Kind.
Installing NGINX Ingress Controller
- Add the Ingress NGINX repository:
helm repo add ingress-nginx https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/helm-chart/ingress-nginx- Update our Helm repositories:
helm repo update- Install the NGINX Ingress Controller:
helm install nginx-ingress ingress-nginx/ingress-nginx --create-namespace --namespace ingress-nginx- Check the installation:
We can check if the NGINX Ingress Controller pods are running with this command:
kubectl get pods -n ingress-nginxAccessing the NGINX Ingress Controller
After we install it, we may want to expose the NGINX Ingress Controller. This helps us access services. We can do this by setting a service type as NodePort or using port forwarding. It depends on what we like.
For more details on accessing NGINX Ingress on our local setup, we can check out how to expose NGINX Ingress Controller using NodePort or how to use port forwarding.
This installation will help us set up the NGINX Ingress Controller. We can then manage ingress rules for our applications that run in the local Kubernetes cluster.
How to Configure Ingress Resources for NGINX in Local Kubernetes
To configure Ingress resources for NGINX in our local Kubernetes setup, we can follow these steps:
Create a Sample Application: We need a simple application to show via Ingress. Below is a basic NGINX deployment example.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-app spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80Save this as
nginx-deployment.yaml. Then apply it using:kubectl apply -f nginx-deployment.yamlCreate a Service: We expose the NGINX application using a Service of type ClusterIP.
apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80Save this as
nginx-service.yaml. Then apply it:kubectl apply -f nginx-service.yamlCreate Ingress Resource: We define an Ingress resource to direct traffic to our NGINX service.
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: nginx-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - host: nginx.local http: paths: - path: / pathType: Prefix backend: service: name: nginx-service port: number: 80Save this as
nginx-ingress.yaml. Then apply it:kubectl apply -f nginx-ingress.yamlUpdate /etc/hosts: We add an entry to our
/etc/hostsfile to link thenginx.localdomain to our local Kubernetes cluster. If we use Minikube, we can run this command to get the IP:minikube ipThen add this line to our
/etc/hosts:<MINIKUBE_IP> nginx.localAccess the Application: Now we can reach our application by going to
http://nginx.localin our browser.
This setup helps the NGINX Ingress Controller to send traffic to the NGINX application running in our local Kubernetes. For more details, check how to configure ingress for external access to my applications.
How to Expose NGINX Ingress Controller Using NodePort
We can expose the NGINX Ingress Controller in our local Kubernetes setup with NodePort. We need to change the service type of the Ingress Controller deployment. Let’s follow these steps:
Install NGINX Ingress Controller: If we have not installed the NGINX Ingress Controller yet, we can do it using this command:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yamlUpdate the Service to NodePort: We should change the service type of the NGINX Ingress Controller to
NodePort. We can use this command to edit the service:kubectl edit svc ingress-nginx-controller -n ingress-nginxIn the editor, find the
spec.typefield. Change it fromLoadBalancerorClusterIPtoNodePortlike this:spec: type: NodePortSave and Exit: After we make the changes, we should save the file and exit the editor. Kubernetes will apply the changes by itself.
Get the NodePort: To see the assigned NodePort, we run this command:
kubectl get svc ingress-nginx-controller -n ingress-nginxWe need to look for
NodePortin the output under thePORT(S)column. It will look like80:<NodePort>/TCPor443:<NodePort>/TCP.Access the NGINX Ingress Controller: We can access our NGINX Ingress Controller using the Node IP and NodePort. For example:
http://<Node_IP>:<NodePort>Testing: We need to make sure our ingress rules are set up right. We can also check the logs of the NGINX Ingress Controller for any errors by using:
kubectl logs -l app.kubernetes.io/name=ingress-nginx -n ingress-nginx
By following these steps, we can expose the NGINX Ingress Controller with NodePort in our local Kubernetes setup. This will let us access our applications that the Ingress Controller manages. For more details on exposing applications using Ingress, we can look at this article on configuring Ingress.
How to Use Port Forwarding to Access NGINX Ingress Locally
We can access the NGINX Ingress Controller in our local Kubernetes
setup by using kubectl port-forward. This helps us forward
local ports to a pod. This way, we can access services without making
them public.
Identify the NGINX Ingress Controller Pod: First, we need to find the name of the NGINX Ingress Controller pod. Run this command:
kubectl get pods -n ingress-nginxLook for the pod name. It usually starts with
ingress-nginx-controller.Port Forwarding Command: Next, we can set up port forwarding. We need to replace
<pod-name>with the name of our Ingress Controller pod.kubectl port-forward <pod-name> 8080:80 --namespace ingress-nginxThis command forwards local port 8080 to port 80 of the NGINX Ingress Controller pod.
Access Your Services: After we set up port forwarding, we can access our services through the NGINX Ingress. We just go to:
http://localhost:8080Make sure we have set up the Ingress resources correctly for the services we want to access.
Stopping the Port Forwarding: To stop the port forwarding, we just need to interrupt the command. Press
Ctrl + Cin the terminal where the command is running.
By using kubectl port-forward, we can test and access
our NGINX Ingress Controller and the services. This is very helpful for
development and debugging in our local Kubernetes environment.
How to Verify NGINX Ingress Connectivity on Local Kubernetes
To check the connection of the NGINX Ingress Controller in our local Kubernetes setup, we can follow some easy steps.
Check NGINX Ingress Controller Pods: First, we need to make sure the NGINX Ingress Controller pods are running well.
kubectl get pods -n ingress-nginxWe should see the status is
Running.Get Ingress Controller Service: Next, we will get the service details to see how it is exposed.
kubectl get services -n ingress-nginxWe should see a service type like
NodePortorLoadBalancer. We need to note the port that is set.Test Connectivity using Curl: If we exposed the NGINX Ingress Controller using
NodePort, we can usecurlto check the connection to our application.curl http://<Node-IP>:<NodePort>We need to replace
<Node-IP>with our local machine’s IP and<NodePort>with the port number we got from the service details.Check Ingress Resource: We should also verify our Ingress resource to make sure it is set up right.
kubectl get ingress -n <your-namespace>We need to look at the output to ensure that the rules match our expected host and paths.
Inspect Logs: We can check the logs of the NGINX Ingress Controller to fix any problems.
kubectl logs -l app.kubernetes.io/name=ingress-nginx -n ingress-nginxUse Port Forwarding (if needed): If we want to reach the Ingress Controller directly, we can port-forward it to localhost.
kubectl port-forward svc/ingress-nginx-controller -n ingress-nginx 8080:80After we run this command, we can access our services through
http://localhost:8080.
By following these steps, we can check the connectivity of the NGINX Ingress Controller in our local Kubernetes setup. For more details on how to set up an Ingress, we can look at how to configure ingress for external access to my applications.
Frequently Asked Questions
1. What is NGINX Ingress in Kubernetes?
NGINX Ingress is a helpful tool in Kubernetes. It helps us manage outside access to services in our cluster. It works as a reverse proxy. This means it sends HTTP(S) traffic to different services based on rules we set. This makes it easier to manage traffic and balance the load. If we are new to Kubernetes, learning how to set up and configure NGINX Ingress is very important. We can learn more about configuring ingress for Kubernetes here.
2. How do I install the NGINX Ingress Controller locally?
To install the NGINX Ingress Controller on our local Kubernetes
setup, we can use Helm or apply the deployment manifests directly. If we
use Helm, we just run
helm install nginx-ingress ingress-nginx/ingress-nginx. If
we like using manifests, we can apply the official YAML files from the
NGINX Ingress documentation. This will set up the controller. Then we
can create ingress resources for our applications.
3. What is the difference between NodePort and LoadBalancer services in Kubernetes?
NodePort and LoadBalancer are both service types in Kubernetes. They help expose applications. A NodePort service opens a specific port on each node’s IP. This lets outside traffic reach the service. A LoadBalancer service, on the other hand, creates an external load balancer. It routes traffic to the service and manages the traffic distribution. For local setups, NodePort is often easier. It does not need cloud provider integrations.
4. How can I verify connectivity to my NGINX Ingress Controller?
To check connectivity, we can use tools like curl. We
can make requests to the ingress resource’s URL. First, we make sure
that our ingress resource is set up correctly. Also, we ensure the NGINX
Ingress Controller is running. Then we run a command like
curl http://<NODE_IP>:<NODE_PORT>. This checks
if the ingress sends traffic as it should. This simple test confirms
that our NGINX Ingress setup is working well.
5. What common issues might I face when using NGINX Ingress on Kubernetes?
Common issues we might face are misconfigured ingress resources, DNS
problems, or the NGINX Ingress Controller not handling traffic well. To
fix these, we can check the logs of the controller. We do this using
kubectl logs <nginx-ingress-pod>. We also make sure
that our ingress rules match our service settings. Furthermore, we check
that our DNS settings point to the right IP address of the ingress
controller. This helps us access our applications smoothly.
By looking at these frequently asked questions, we can improve our understanding of how to use NGINX Ingress on our local Kubernetes setup. This will help us manage our Kubernetes applications easily.