Examples

    Prerequisites

    Before following those examples, make sure your cluster follows .

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

    namespace.yaml

    deployment.yaml

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

    Command

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

    Expected Output

    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.

    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. type: ClusterIP
    30. ports:
    31. - port: 8080
    32. name: whoami-tcp
    33. app: whoami-tcp

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

    Command

    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 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.

    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. spec: {}