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: containous/whoami:v1.4.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: containous/whoamitcp:latest
    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. spec:
    65. type: ClusterIP
    66. ports:
    67. - port: 8080
    68. name: whoami-tcp
    69. selector:
    70. ---
    71. apiVersion: v1
    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 Maesh by following this documentation on your cluster.

    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. app: whoami-tcp
    26. # These annotations enable Maesh for this service:
    27. maesh.containo.us/traffic-type: "tcp"
    28. spec:
    29. type: ClusterIP
    30. ports:
    31. - port: 8080
    32. name: whoami-tcp
    33. selector:
    34. app: whoami-tcp

    You should now be able to access your HTTP and TCP services through the Maesh 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.svc.cluster.local
    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 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.

    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