Using the Traefik Ingress Controller

Rancher Desktop uses K3s under the hood, which in turn uses Traefik as the default Ingress controller for your Kubernetes cluster. As an example, the below steps outline creating simple services that can be routed by the Ingress object.

Example Steps: Traefik Ingress Controller

  • Linux
  • macOS
  • Windows
  1. Open a bash session and set the node IP to your localhost address:
  1. IP=127.0.0.1
  1. Create a namespace called demo:
  1. kubectl create ns demo
  1. Create a whoami example with basic deployment, service, and Ingress objects defined:

Using the Traefik Ingress Controller - 图1note

Some Linux distributions don’t allow listening to priviliged ports by default, please see the documentation on Traefik port binding access to authorize ports if necessary.

  1. cat << EOF | kubectl apply -n demo -f -
  2. ---
  3. apiVersion: apps/v1
  4. kind: Deployment
  5. metadata:
  6. labels:
  7. app: whoami
  8. name: whoami
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: whoami
  14. template:
  15. metadata:
  16. labels:
  17. app: whoami
  18. spec:
  19. containers:
  20. - image: traefik/whoami:latest
  21. name: whoami
  22. ports:
  23. - containerPort: 80
  24. ---
  25. apiVersion: v1
  26. kind: Service
  27. metadata:
  28. name: whoami-svc
  29. spec:
  30. type: ClusterIP
  31. selector:
  32. app: whoami
  33. ports:
  34. - port: 80
  35. ---
  36. apiVersion: networking.k8s.io/v1
  37. kind: Ingress
  38. metadata:
  39. name: whoami-http
  40. annotations:
  41. traefik.ingress.kubernetes.io/router.entrypoints: web
  42. spec:
  43. rules:
  44. - host: whoami.$IP.sslip.io
  45. http:
  46. paths:
  47. - path: /
  48. pathType: Prefix
  49. backend:
  50. service:
  51. name: whoami-svc
  52. port:
  53. number: 80
  54. EOF
  1. Verify the ingress works by calling curl:
  1. curl whoami.$IP.sslip.io
  2. Hostname: whoami-6ff6dcfdc8-74mwq
  3. IP: 127.0.0.1
  4. IP: ::1
  5. IP: 10.42.0.42
  6. IP: fe80::e804:41ff:feac:7eef
  7. RemoteAddr: 10.42.0.45:35392
  8. GET / HTTP/1.1
  9. Host: whoami.127.0.0.1.sslip.io
  10. User-Agent: curl/7.64.1
  11. Accept: */*
  12. Accept-Encoding: gzip
  13. X-Forwarded-For: 10.42.0.1
  14. X-Forwarded-Host: whoami.127.0.0.1.sslip.io
  15. X-Forwarded-Port: 80
  16. X-Forwarded-Proto: http
  17. X-Forwarded-Server: traefik-d497b4cb6-4vkg9
  18. X-Real-Ip: 10.42.0.1
  1. Delete the resources:
  1. kubectl delete all,ingress --all -n demo
  1. Open a bash session and set the node IP to your localhost address:
  • Default
  • socket-vmnet Enabled
  1. IP=127.0.0.1
  1. IP=`kubectl get node/lima-rancher-desktop -o jsonpath='{.status.addresses[?(@.type=="ExternalIP")].address}'`
  1. Create a namespace called demo:
  1. kubectl create ns demo
  1. Create a whoami example with basic deployment, service, and Ingress objects defined:
  1. cat << EOF | kubectl apply -n demo -f -
  2. ---
  3. apiVersion: apps/v1
  4. kind: Deployment
  5. metadata:
  6. labels:
  7. app: whoami
  8. name: whoami
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: whoami
  14. template:
  15. metadata:
  16. labels:
  17. app: whoami
  18. spec:
  19. containers:
  20. - image: traefik/whoami:latest
  21. name: whoami
  22. ports:
  23. - containerPort: 80
  24. ---
  25. apiVersion: v1
  26. kind: Service
  27. metadata:
  28. name: whoami-svc
  29. spec:
  30. type: ClusterIP
  31. selector:
  32. app: whoami
  33. ports:
  34. - port: 80
  35. ---
  36. apiVersion: networking.k8s.io/v1
  37. kind: Ingress
  38. metadata:
  39. name: whoami-http
  40. annotations:
  41. traefik.ingress.kubernetes.io/router.entrypoints: web
  42. spec:
  43. rules:
  44. - host: whoami.$IP.sslip.io
  45. http:
  46. paths:
  47. - path: /
  48. pathType: Prefix
  49. backend:
  50. service:
  51. name: whoami-svc
  52. port:
  53. number: 80
  54. EOF
  1. Verify the ingress works by calling curl:
  1. curl whoami.$IP.sslip.io
  2. Hostname: whoami-6ff6dcfdc8-74mwq
  3. IP: 127.0.0.1
  4. IP: 10.42.0.4
  5. RemoteAddr: 10.42.0.250:54102
  6. GET / HTTP/1.1
  7. Host: whoami.127.0.0.1.sslip.io
  8. User-Agent: curl/8.1.1
  9. Accept: */*
  10. Accept-Encoding: gzip
  11. X-Forwarded-For: 10.42.0.246
  12. X-Forwarded-Host: whoami.127.0.0.1.sslip.io
  13. X-Forwarded-Port: 80
  14. X-Forwarded-Proto: http
  15. X-Forwarded-Server: traefik-64b96ccbcd-26wcv
  16. X-Real-Ip: 10.42.0.246
  1. Delete the resources:
  1. kubectl delete all,ingress --all -n demo
  1. Open a powershell session and set the node IP to your local address:
  1. $IP = (kubectl get node/$env:COMPUTERNAME -o=jsonpath="{range .status.addresses[?(@.type == 'InternalIP')]}{.address}{end}")
  1. Create a namespace called demo:
  1. kubectl create ns demo
  1. Create a whoami example with basic deployment, service, and Ingress objects defined:
  1. echo @"
  2. ---
  3. apiVersion: apps/v1
  4. kind: Deployment
  5. metadata:
  6. labels:
  7. app: whoami
  8. name: whoami
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: whoami
  14. template:
  15. metadata:
  16. labels:
  17. app: whoami
  18. spec:
  19. containers:
  20. - image: traefik/whoami:latest
  21. name: whoami
  22. ports:
  23. - containerPort: 80
  24. ---
  25. apiVersion: v1
  26. kind: Service
  27. metadata:
  28. name: whoami-svc
  29. spec:
  30. type: ClusterIP
  31. selector:
  32. app: whoami
  33. ports:
  34. - port: 80
  35. ---
  36. apiVersion: networking.k8s.io/v1
  37. kind: Ingress
  38. metadata:
  39. name: whoami-http
  40. annotations:
  41. traefik.ingress.kubernetes.io/router.entrypoints: web
  42. spec:
  43. rules:
  44. - host: whoami.$IP.sslip.io
  45. http:
  46. paths:
  47. - path: /
  48. pathType: Prefix
  49. backend:
  50. service:
  51. name: whoami-svc
  52. port:
  53. number: 80
  54. "@ | kubectl apply -n demo -f -
  1. Verify the ingress works by calling curl:
  1. curl whoami.$IP.sslip.io
  1. Delete the resources:
  1. kubectl delete all,ingress --all -n demo