How to Create Ingress in Kubernetes: Simple Guide
To create an
Ingress in Kubernetes, define an Ingress resource YAML that specifies rules to route external HTTP(S) traffic to internal services. Apply this YAML using kubectl apply -f after ensuring an ingress controller is installed in your cluster.Syntax
An Ingress resource defines rules to route external traffic to Kubernetes services. Key parts include:
- apiVersion: The API group and version, usually
networking.k8s.io/v1. - kind: Always
Ingressfor ingress resources. - metadata: Name and labels for the ingress.
- spec: Contains
rulesthat map hostnames and paths to backend services. - backend: Defines the service name and port to route traffic to.
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: example-service
port:
number: 80
Example
This example creates an ingress that routes all traffic from myapp.local to a service named myapp-service on port 80. It assumes you have an ingress controller installed like NGINX Ingress Controller.
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myapp.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myapp-service
port:
number: 80
Output
ingress.networking.k8s.io/myapp-ingress created
Common Pitfalls
- Not installing an ingress controller before creating ingress resources will cause no traffic routing.
- Incorrect
hostorpathvalues can prevent traffic from reaching services. - Missing or wrong service name or port in backend causes routing failures.
- For local testing, forgetting to add the ingress host to your
/etc/hostsfile prevents DNS resolution.
yaml
### Wrong backend service name example
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: bad-ingress
spec:
rules:
- host: bad.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: wrong-service
port:
number: 80
### Correct backend service name example
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: good-ingress
spec:
rules:
- host: good.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: correct-service
port:
number: 80
Quick Reference
Remember these tips when creating ingress:
- Always install an ingress controller (e.g., NGINX, Traefik) before applying ingress resources.
- Use
networking.k8s.io/v1API version for Kubernetes 1.19+. - Specify
pathTypeasPrefixorExactto control path matching. - Use annotations to customize ingress controller behavior.
- Test DNS or hosts file to resolve ingress hostnames.
Key Takeaways
Install an ingress controller before creating ingress resources to enable traffic routing.
Define ingress rules with correct host, path, service name, and port for proper routing.
Use the latest API version networking.k8s.io/v1 and specify pathType for path matching.
Annotations help customize ingress behavior for specific controllers like NGINX.
For local testing, update your hosts file to resolve ingress hostnames.