Creating a New Ingress Rule
Since setting up ingress rules is a common operation, provide detailed instructions on how to define and apply these rules to route external traffic to the appropriate services within your Kubernetes cluster. This should include examples and best practices. Kubernetes
Prerequisites
A running Kubernetes cluster
kubectl installed and configured
NGINX Ingress Controller installed
If not installed, you can install it quickly with:
yaml
1
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.10.0/deploy/static/provider/cloud/deploy.yaml
Step-by-Step: Creating a New Ingress Rule
1. Create a Sample Application (Deployment + Service)
Let’s create a simple Hello World app that responds on port 80:
yaml
12345678910111213141516171819202122232425262728293031323334
# hello-deployment.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: hello-worldspec:replicas: 2selector:matchLabels:app: hello-worldtemplate:metadata:labels:app: hello-worldspec:containers:- name: hello-worldimage: hashicorp/http-echoargs:- "-text=Hello, Kubernetes!"ports:- containerPort: 5678---apiVersion: v1kind: Servicemetadata:name: hello-worldspec:selector:app: hello-worldports:- protocol: TCPport: 80targetPort: 5678
Apply it:
yaml
1
kubectl apply -f hello-deployment.yaml
2. Create an Ingress Resource
Now let’s expose this service using an Ingress rule.
yaml
1234567891011121314151617181920
# hello-ingress.yamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: hello-ingressannotations:nginx.ingress.kubernetes.io/rewrite-target: /spec:ingressClassName: nginxrules:- host: hello.localhttp:paths:- path: /pathType: Prefixbackend:service:name: hello-worldport:number: 80
Apply it:
yaml
1
kubectl apply -f hello-ingress.yaml
3. Test It
Add a line to your /etc/hosts (on your local machine):
yaml
1
127.0.0.1 hello.local
Then use curl on your browser:
yaml
1
curl http://hello.local # Output: Hello, Kubernetes!