Examples

Here are some examples on how to easily deploy Traefik Mesh on your cluster.

Prerequisites

Before following those examples, make sure your cluster follows the prerequisites for deploying Traefik Mesh.

Simple Example

Deploy those two yaml files on your Kubernetes cluster in order to add a simple backend example, available through HTTP and TCP.

namespace.yaml

  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: whoami
  5. ---
  6. apiVersion: v1
  7. kind: ServiceAccount
  8. metadata:
  9. name: whoami-server
  10. namespace: whoami
  11. ---
  12. apiVersion: v1
  13. kind: ServiceAccount
  14. metadata:
  15. name: whoami-client
  16. namespace: whoami

deployment.yaml

  1. ---
  2. kind: Deployment
  3. apiVersion: apps/v1
  4. metadata:
  5. name: whoami
  6. namespace: whoami
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. app: whoami
  12. template:
  13. metadata:
  14. labels:
  15. app: whoami
  16. spec:
  17. serviceAccount: whoami-server
  18. containers:
  19. - name: whoami
  20. image: traefik/whoami:v1.6.0
  21. imagePullPolicy: IfNotPresent
  22. ---
  23. kind: Deployment
  24. apiVersion: apps/v1
  25. metadata:
  26. name: whoami-tcp
  27. namespace: whoami
  28. spec:
  29. replicas: 2
  30. selector:
  31. matchLabels:
  32. app: whoami-tcp
  33. template:
  34. metadata:
  35. labels:
  36. app: whoami-tcp
  37. spec:
  38. serviceAccount: whoami-server
  39. containers:
  40. - name: whoami-tcp
  41. image: traefik/whoamitcp:v0.1.0
  42. imagePullPolicy: IfNotPresent
  43. ---
  44. apiVersion: v1
  45. kind: Service
  46. metadata:
  47. name: whoami
  48. namespace: whoami
  49. labels:
  50. app: whoami
  51. spec:
  52. type: ClusterIP
  53. ports:
  54. - port: 80
  55. name: whoami
  56. selector:
  57. app: whoami
  58. ---
  59. apiVersion: v1
  60. kind: Service
  61. metadata:
  62. name: whoami-tcp
  63. namespace: whoami
  64. labels:
  65. app: whoami-tcp
  66. spec:
  67. type: ClusterIP
  68. ports:
  69. - port: 8080
  70. name: whoami-tcp
  71. selector:
  72. app: whoami-tcp
  73. ---
  74. apiVersion: v1
  75. kind: Pod
  76. metadata:
  77. name: whoami-client
  78. namespace: whoami
  79. spec:
  80. serviceAccountName: whoami-client
  81. containers:
  82. - name: whoami-client
  83. image: giantswarm/tiny-tools:3.9
  84. command:
  85. - "sleep"
  86. - "3600"

You should now see the following when running kubectl get all -n whoami:

  1. NAME READY STATUS RESTARTS AGE
  2. pod/whoami-client 1/1 Running 0 11s
  3. pod/whoami-f4cbd7f9c-lddgq 1/1 Running 0 12s
  4. pod/whoami-f4cbd7f9c-zk4rb 1/1 Running 0 12s
  5. pod/whoami-tcp-7679bc465-ldlt2 1/1 Running 0 12s
  6. pod/whoami-tcp-7679bc465-wf87n 1/1 Running 0 12s
  7. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  8. service/whoami ClusterIP 100.68.109.244 <none> 80/TCP 13s
  9. service/whoami-tcp ClusterIP 100.68.73.211 <none> 8080/TCP 13s
  10. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
  11. deployment.apps/whoami 2 2 2 2 13s
  12. deployment.apps/whoami-tcp 2 2 2 2 13s
  13. NAME DESIRED CURRENT READY AGE
  14. replicaset.apps/whoami-f4cbd7f9c 2 2 2 13s
  15. replicaset.apps/whoami-tcp-7679bc465 2 2 2 13s

You should now be able to make direct requests on your whoami service through HTTP.

Command

  1. kubectl -n whoami exec whoami-client -- curl -s whoami.whoami.svc.cluster.local

Expected Output

  1. Hostname: whoami-84bdf87956-gvbm8
  2. IP: 127.0.0.1
  3. IP: 5.6.7.8
  4. RemoteAddr: 1.2.3.4:12345
  5. GET / HTTP/1.1
  6. Host: whoami.whoami.svc.cluster.local
  7. User-Agent: curl/7.64.0
  8. Accept: */*

And through TCP, by executing the following netcat command and sending some data.

Command

  1. kubectl -n whoami exec -ti whoami-client -- nc whoami-tcp.whoami.svc.cluster.local 8080
  2. my data

Expected Output

  1. Received: my data

You can now install Traefik Mesh by following this documentation on your cluster.

Since Traefik Mesh is not intrusive, it has to be explicitly given access to services before it can be used. You can ensure that the HTTP endpoint of your service does not pass through Traefik Mesh since no X-Forwarded-For header should be added.

Now, in order to configure Traefik Mesh for your whoami service, you just need to update the whoami service specs, in order to add the appropriate annotations.

The HTTP service needs to have mesh.traefik.io/traffic-type: "http" and the TCP service, mesh.traefik.io/traffic-type: "tcp".

  1. ---
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: whoami
  6. namespace: whoami
  7. labels:
  8. app: whoami
  9. annotations:
  10. mesh.traefik.io/traffic-type: "http"
  11. mesh.traefik.io/retry-attempts: "2"
  12. spec:
  13. type: ClusterIP
  14. ports:
  15. - port: 80
  16. name: whoami
  17. selector:
  18. app: whoami
  19. ---
  20. apiVersion: v1
  21. kind: Service
  22. metadata:
  23. name: whoami-tcp
  24. namespace: whoami
  25. labels:
  26. app: whoami-tcp
  27. annotations:
  28. mesh.traefik.io/traffic-type: "tcp"
  29. spec:
  30. type: ClusterIP
  31. ports:
  32. - port: 8080
  33. name: whoami-tcp
  34. selector:
  35. app: whoami-tcp

You should now be able to access your HTTP and TCP services through the Traefik Mesh endpoint:

Command

  1. kubectl -n whoami exec whoami-client -- curl -s whoami.whoami.traefik.mesh

Expected Output

  1. Hostname: whoami-84bdf87956-gvbm8
  2. IP: 127.0.0.1
  3. IP: 5.6.7.8
  4. RemoteAddr: 1.2.3.4:12345
  5. GET / HTTP/1.1
  6. Host: whoami.whoami.traefik.mesh
  7. User-Agent: curl/7.64.0
  8. Accept: */*
  9. X-Forwarded-For: 3.4.5.6

ACL Example

The ACL mode can be enabled when installing Traefik Mesh. Once activated, all traffic is forbidden unless explicitly authorized using the SMI TrafficTarget resource. This example will present the configuration required to allow the client pod to send traffic to the HTTP and TCP services defined in the previous example.

Each TrafficTarget defines that a set of source ServiceAccount is capable of sending traffic to a destination ServiceAccount. To authorize the whoami-client pod to send traffic to whoami.whoami.traefik.mesh, we need to explicitly allow it to hit the pods exposed by the whoami service.

  1. ---
  2. apiVersion: specs.smi-spec.io/v1alpha3
  3. kind: HTTPRouteGroup
  4. metadata:
  5. name: http-everything
  6. namespace: whoami
  7. spec:
  8. matches:
  9. - name: everything
  10. pathRegex: ".*"
  11. methods: ["*"]
  12. ---
  13. kind: TrafficTarget
  14. apiVersion: access.smi-spec.io/v1alpha2
  15. metadata:
  16. name: whatever
  17. namespace: whoami
  18. spec:
  19. destination:
  20. kind: ServiceAccount
  21. name: whoami-server
  22. namespace: whoami
  23. port: 80
  24. rules:
  25. - kind: HTTPRouteGroup
  26. name: http-everything
  27. matches:
  28. - everything
  29. sources:
  30. - kind: ServiceAccount
  31. name: whoami-client
  32. namespace: whoami

Incoming traffic on a TCP service can also be authorized using a TrafficTarget and a TCPRoute.

  1. ---
  2. kind: TrafficTarget
  3. apiVersion: access.smi-spec.io/v1alpha2
  4. metadata:
  5. name: api-service-target
  6. namespace: default
  7. spec:
  8. destination:
  9. kind: ServiceAccount
  10. name: api-service
  11. namespace: default
  12. rules:
  13. - kind: TCPRoute
  14. name: my-tcp-route
  15. sources:
  16. - kind: ServiceAccount
  17. name: my-other-service
  18. namespace: default
  19. ---
  20. apiVersion: specs.smi-spec.io/v1alpha3
  21. kind: TCPRoute
  22. metadata:
  23. name: my-tcp-route
  24. spec: {}