Your Ingress resource definition is mostly correct for setting up Traefik as your ingress controller. However, there are a few improvements and considerations to ensure it functions as expected, especially concerning annotations and best practices with the networking.k8s.io/v1 API version.
Key Points to Address:
-
Ingress Class Specification:
- Current Approach: You're using the annotation
kubernetes.io/ingress.class: traefik to specify the ingress class.
- Recommended Approach: Starting with Kubernetes
networking.k8s.io/v1, it's preferred to use the ingressClassName field within the spec instead of annotations. This provides a more standardized way to specify the ingress class.
-
Annotation Prefixes for Traefik:
- Current Annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.middlewares: default-stripprefix@kubernetescrd
- Recommended Annotations: Depending on the version of Traefik you are using, the annotation prefix might have changed. For Traefik v2 and later, it's common to use the
traefik.io prefix.
traefik.io/router.entrypoints: web
traefik.io/router.middlewares: default-stripprefix@kubernetescrd
- Action: Verify your Traefik version and adjust the annotation prefixes accordingly. If you're using a recent version of Traefik (v2+), update the prefixes to
traefik.io.
-
Middleware Reference:
- Ensure that the middleware
default-stripprefix exists in the correct namespace or is properly referenced if it's in a different namespace. The suffix @kubernetescrd assumes it's managed via Custom Resource Definitions, so confirm that this matches your setup.
-
Host Field Usage:
- Using an IP address (
192.168.1.1) as the host is acceptable if you're accessing the service directly via IP. However, it's more common to use a domain name. If you're sticking with an IP, ensure that your DNS setup (or /etc/hosts file) aligns with this configuration.
Revised ingress.yaml Example:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: backend-ingress
namespace: backend-api
annotations:
traefik.io/router.entrypoints: web
traefik.io/router.middlewares: default-stripprefix@kubernetescrd
spec:
ingressClassName: traefik
rules:
- host: 192.168.1.1 # Replace with your domain or Traefik external IP
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 80
Additional Recommendations:
-
TLS Configuration: If you plan to serve your application over HTTPS, consider adding a tls section to your Ingress resource to specify the TLS settings.
spec:
tls:
- hosts:
- 192.168.1.1
secretName: your-tls-secret
ingressClassName: traefik
rules:
# ... your existing rules
-
Verify Middleware: Double-check that the default-stripprefix middleware is correctly defined and applied. Middleware mismatches can lead to unexpected routing behavior.
-
Testing: After applying the Ingress resource, use Traefik's dashboard or logs to verify that the ingress route is correctly recognized and that requests are being properly routed to your backend service.
By addressing these points, your Ingress configuration should work more reliably with Traefik, following Kubernetes best practices and ensuring compatibility with Traefik’s expected annotation formats.