Creating an M3 Cluster with Kubernetes

This guide shows you how to create an M3 cluster of 3 nodes, designed to run locally on the same machine. It is designed to show you how M3 and Kubernetes can work together, but not as a production example.

This guide assumes you have read the quickstart, and builds upon the concepts in that guide.

We recommend you use our Kubernetes operator to deploy M3 to a cluster. It is a more streamlined setup that uses custom resource definitions to automatically handle operations such as managing cluster placements.

Prerequisites

Create An etcd Cluster

M3 stores its cluster placements and runtime metadata in etcd and needs a running cluster to communicate with.

We have example services and stateful sets you can use, but feel free to use your own configuration and change any later instructions accordingly.

  1. kubectl apply -f https://raw.githubusercontent.com/m3db/m3db-operator/master/example/etcd/etcd-minikube.yaml

If the etcd cluster is running on your local machine, update your /etc/hosts file to match the domains specified in the etcd --initial-cluster argument. For example to match the StatefulSet declaration in the etcd-minikube.yaml above, that is:

  1. $(minikube ip) etcd-0.etcd
  2. $(minikube ip) etcd-1.etcd
  3. $(minikube ip) etcd-2.etcd

Verify that the cluster is running with something like the Kubernetes dashboard, or the command below:

  1. kubectl exec etcd-0 -- env ETCDCTL_API=3 etcdctl endpoint health

Install the Operator

Install the bundled operator manifests in the current namespace:

  1. kubectl apply -f https://raw.githubusercontent.com/m3db/m3db-operator/master/bundle.yaml

Create an M3 Cluster

The following creates an M3 cluster with 3 replicas of data across 256 shards that connects to the 3 available etcd endpoints.

It creates three isolated groups for nodes, each with one node instance. In a production environment you can use a variety of different options to define how nodes are spread across groups based on factors such as resource capacity, or location.

It creates namespaces in the cluster with the namespaces parameter. You can use M3-provided presets, or define your own. This example creates a namespace with the 10s:2d preset.

The cluster derives pod identity from the podIdentityConfig parameter, which in this case is the UID of the Pod.

Read more details on all the parameters in the Operator API docs.

  1. kubectl apply -f https://raw.githubusercontent.com/m3db/m3db-operator/master/example/m3db-local.yaml

Verify that the cluster is running with something like the Kubernetes dashboard, or the command below:

  1. kubectl exec simple-cluster-rep2-0 -- curl -sSf localhost:9002/health

Deleting a Cluster

Delete the M3 cluster using kubectl:

  1. kubectl delete m3dbcluster simple-cluster

By default, the operator uses finalizers to delete the placement and namespaces associated with a cluster before the custom resources. If you do not want this behavior, set keepEtcdDataOnDelete to true in the cluster configuration.

Writing and Querying Metrics

Writing Metrics

M3 supports ingesting statsd and Prometheus formatted metrics.

This quickstart focuses on Prometheus metrics which consist of a value, a timestamp, and tags to bring context and meaning to the metric.

You can write metrics using one of two endpoints:

For this quickstart, use the http://localhost:7201/api/v1/json/write endpoint to write a tagged metric to M3 with the following data in the request body, all fields are required:

  • tags: An object of at least one name/value pairs
  • timestamp: The UNIX timestamp for the data
  • value: The value for the data, can be of any type

The examples below use __name__ as the name for one of the tags, which is a Prometheus reserved tag that allows you to query metrics using the value of the tag to filter results.

Label names may contain ASCII letters, numbers, underscores, and Unicode characters. They must match the regex [a-zA-Z_][a-zA-Z0-9_]*. Label names beginning with __ are reserved for internal use. Read more in the Prometheus documentation.

  1. #!/bin/bash
  2. curl -X POST http://localhost:7201/api/v1/json/write -d '{
  3. "tags":
  4. {
  5. "__name__": "third_avenue",
  6. "city": "new_york",
  7. "checkout": "1"
  8. },
  9. "timestamp": '\"$(date "+%s")\"',
  10. "value": 3347.26
  11. }'
  1. #!/bin/bash
  2. curl -X POST http://localhost:7201/api/v1/json/write -d '{
  3. "tags":
  4. {
  5. "__name__": "third_avenue",
  6. "city": "new_york",
  7. "checkout": "1"
  8. },
  9. "timestamp": '\"$(date "+%s")\"',
  10. "value": 5347.26
  11. }'
  1. #!/bin/bash
  2. curl -X POST http://localhost:7201/api/v1/json/write -d '{
  3. "tags":
  4. {
  5. "__name__": "third_avenue",
  6. "city": "new_york",
  7. "checkout": "1"
  8. },
  9. "timestamp": '\"$(date "+%s")\"',
  10. "value": 7347.26
  11. }'

Querying metrics

M3 supports three query engines: Prometheus (default), Graphite, and the M3 Query Engine.

This quickstart uses Prometheus as the query engine, and you have access to all the features of PromQL queries.

To query metrics, use the http://localhost:7201/api/v1/query\_range endpoint with the following data in the request body, all fields are required:

  • query: A PromQL query
  • start: Timestamp in RFC3339Nano of start range for results
  • end: Timestamp in RFC3339Nano of end range for results
  • step: A duration or float of the query resolution, the interval between results in the timespan between start and end.

Below are some examples using the metrics written above.

Return results in past 45 seconds

  1. curl -X "POST" -G "http://localhost:7201/api/v1/query_range" \
  2. -d "query=third_avenue" \
  3. -d "start=$(date "+%s" -d "45 seconds ago")" \
  4. -d "end=$( date +%s )" \
  5. -d "step=5s" | jq .
  1. curl -X "POST" -G "http://localhost:7201/api/v1/query_range" \
  2. -d "query=third_avenue" \
  3. -d "start=$( date -v -45S +%s )" \
  4. -d "end=$( date +%s )" \
  5. -d "step=5s" | jq .
  1. {
  2. "status": "success",
  3. "data": {
  4. "resultType": "matrix",
  5. "result": [
  6. {
  7. "metric": {
  8. "__name__": "third_avenue",
  9. "checkout": "1",
  10. "city": "new_york"
  11. },
  12. "values": [
  13. [
  14. 1610746220,
  15. "3347.26"
  16. ],
  17. [
  18. 1610746220,
  19. "5347.26"
  20. ],
  21. [
  22. 1610746220,
  23. "7347.26"
  24. ]
  25. ]
  26. }
  27. ]
  28. }
  29. }

Values above a certain number

  1. curl -X "POST" -G "http://localhost:7201/api/v1/query_range" \
  2. -d "query=third_avenue > 6000" \
  3. -d "start=$(date "+%s" -d "45 seconds ago")" \
  4. -d "end=$( date +%s )" \
  5. -d "step=5s" | jq .
  1. curl -X "POST" -G "http://localhost:7201/api/v1/query_range" \
  2. -d "query=third_avenue > 6000" \
  3. -d "start=$(date -v -45S "+%s")" \
  4. -d "end=$( date +%s )" \
  5. -d "step=5s" | jq .
  1. {
  2. "status": "success",
  3. "data": {
  4. "resultType": "matrix",
  5. "result": [
  6. {
  7. "metric": {
  8. "__name__": "third_avenue",
  9. "checkout": "1",
  10. "city": "new_york"
  11. },
  12. "values": [
  13. [
  14. 1610746220,
  15. "7347.26"
  16. ]
  17. ]
  18. }
  19. ]
  20. }
  21. }