Examples

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

Prerequisites

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

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: containous/whoami:v1.4.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: containous/whoamitcp:latest
  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 Maesh by following this documentation on your cluster.

Since Maesh 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 Maesh since no X-Forwarded-For header should be added.

Now, in order to configure Maesh 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 maesh.containo.us/traffic-type: "http" and the TCP service, maesh.containo.us/traffic-type: "tcp".

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: whoami
  5. namespace: whoami
  6. labels:
  7. app: whoami
  8. # These annotations enable Maesh for this service:
  9. annotations:
  10. maesh.containo.us/traffic-type: "http"
  11. maesh.containo.us/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. # These annotations enable Maesh for this service:
  28. annotations:
  29. maesh.containo.us/traffic-type: "tcp"
  30. spec:
  31. type: ClusterIP
  32. ports:
  33. - port: 8080
  34. name: whoami-tcp
  35. selector:
  36. app: whoami-tcp

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

Command

  1. kubectl -n whoami exec whoami-client -- curl -s whoami.whoami.maesh

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: */*
  9. X-Forwarded-For: 3.4.5.6

ACL Example

The ACL mode can be enabled when installing Maesh. 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.maesh, we need to explicitly allow it to hit the pods exposed by the whoami service.

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

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

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