“关于灭霸接收器的事情” (Guānyú Mièbà Jiēshōuqì de shìqíng)
这篇文章是 Kubernetes Advent Calendar 2020 第2篇第15天的文章。
首先
我将在这篇文章中写一下我对名为Thanos的开源软件中的“Receiver”组件的调查结果。
虽然这与Kubernetes直接相关的话题无关,但我想尝试使用Thanos Receiver功能将在Kubernetes上启动的Prometheus的指标上传到对象存储中,并进行确认。
灭霸是谁
请参考作者过去的文章等内容,尽管它们可能有点过时。
此外,建议您查看Thanos文档中的设计页面等内容。
「灭霸接受者」是什么?
Thanos的一个部件是配备Prometheus的Remote Write API的组件。通过使用Thanos Receiver,可以将通过Remote Write API从Prometheus发送的指标信息保存到对象存储中。
通过使用Thanos Receiver,可以实现通过Prometheus将指标上传到对象存储的配置,而不是使用Thanos Sidecar上传指标到对象存储。
灭霸接收器的技术概述
整体结构
使用Thanos Receiver时的最小配置总体结构如下所示。

使用Thanos Querier来检索由接收器存储到对象存储中的数据,与使用Thanos Sidecar时的配置和特点没有明显差异。
关于负荷分散
通过使用多个Thanos Receiver 并在其前面放置负载均衡器,可以实现Thanos Receiver的负载均衡。
所有的请求都会分散到每个 Thanos 接收器上,但负责处理的 Thanos 接收器似乎根据 Hashring 的配置文件和收到的请求中的租户标题来确定。
[
{
"hashring": "tenant-a",
"endpoints": ["tenant-a-1.metrics.local:19291/api/v1/receive", "tenant-a-2.metrics.local:19291/api/v1/receive"],
"tenants": ["tenant-a"]
},
{
"hashring": "tenants-b-c",
"endpoints": ["tenant-b-c-1.metrics.local:19291/api/v1/receive", "tenant-b-c-2.metrics.local:19291/api/v1/receive"],
"tenants": ["tenant-b", "tenant-c"]
},
{
"hashring": "soft-tenants",
"endpoints": ["http://soft-tenants-1.metrics.local:19291/api/v1/receive"]
}
]
参考链接:https://thanos.io/tip/proposals/201812_thanos-remote-receive.md/ 在该链接中提到的内容需要良好的中文表述。
处理的流程如下:
接收到A租户的请求的Thanos接收器将该请求发送到A租户的Thanos接收器中。

在上述的哈希设置文件中,如果设置了”tenants”,则称为“Hard tenancy”,如果未设置”tenants”,则称为“Soft tenancy”。根据配置,Soft tenancy被指定为Tnanos Receiver来处理与Hard tenancy不明确匹配的请求。

如果未完成对设置了值为(receive.replication-factor标志的值 + 1)/2的接收机的写入,则Thanos接收机将返回错误。接收机的数量由–receive.replication-factor标志进行设置。
请尝试一下。
现在我们来实际尝试一下Thanos接收器。
这次我们想在本地PC上搭建一个最小配置的”整体结构”,并进行简单验证。
环境构建
首先,在本地PC上构建一个用于验证的Kubernetes集群。
筆者的個人電腦操作系統是macOS Catalina,因此以下步驟是基於使用macOS Catalina的前提進行記載。
如果您使用其他操作系統,請適當替換相應步驟。
在這裡,假設您已經安裝了kubectl等命令行工具。
我在构建Kubernetes集群时使用了kind。
请查阅kind的用户指南以获取有关kind的安装方法。
创建以下配置文件。
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
根据设置文件创建 Kubernetes 集群。
$ kind create cluster --name test --config cluster.yaml
当构建集群完成后,应用nginx ingress controller。
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/kind/deploy.yaml
接下来创建一个专门给 Thanos 使用的命名空间。
$ kubectl create ns thanos
在Kubernetes集群上构建minio作为对象存储。
使用thanos-io/kube-thanos存储库示例中的清单。
$ kubectl apply -f https://raw.githubusercontent.com/thanos-io/kube-thanos/master/examples/development-minio/deployment.yaml -n thanos
$ kubectl apply -f https://raw.githubusercontent.com/thanos-io/kube-thanos/master/examples/development-minio/pvc.yaml -n thanos
$ kubectl apply -f https://raw.githubusercontent.com/thanos-io/kube-thanos/master/examples/development-minio/secret.yaml -n thanos
$ kubectl apply -f https://raw.githubusercontent.com/thanos-io/kube-thanos/master/examples/development-minio/service.yaml -n thanos
灭霸Thanos的组件设置
接下来,我们将为Thanos的每个组件进行配置。
我们将创建Thanos接收器的Hashring配置文件。
apiVersion: v1
kind: ConfigMap
metadata:
name: hashring
namespace: thanos
data:
hashrings.json: |
[
{
"hashring": "default",
"endpoints": [
"thanos-receive-default-0.thanos-receive-default.thanos.svc.cluster.local:10901"
]
}
]
将创建的ConfigMap清单应用到验证集群中。
$ kubectl apply -f thanos-receiver-hashring-configmap.yaml
创建 Thanos Receiver、Thanos Store Gateway 和 Thanos Querier 用于验证的集群。
$ kubectl apply -f https://gist.githubusercontent.com/amber-lamp/ef8e0f46ce8b33dcc4f252ac4b8be2e6/raw/4486935e4612a391964171c9efa25930218089d2/demo-thanos-receive.yaml
Thanos 的各组件设置已经完成。
普罗米修斯的设定
请最后进行 Prometheus 的配置。
使用 Helm Chart 来进行 Prometheus 的配置。
由于需要使用 Helm,请确保已安装 Helm,如果未安装,请参考此页面进行安装。
请执行以下命令,将其安装到 Kubernetes 的默认命名空间。
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo add stable https://charts.helm.sh/stable
$ helm repo update
$ helm install -n default latest prometheus-community/kube-prometheus-stack --set prometheus.ingress.enabled=true --set prometheus.ingress.hosts[0]="cluster.prometheus.local" --set prometheus.prometheusSpec.remoteWrite[0].url="http://thanos-receive-default.thanos.svc.cluster.local:19291/api/v1/receive" --set alertmanager.ingress.enabled=true --set alertmanager.ingress.hosts[0]="cluster.alertmanager.local" --set grafana.ingress.enabled=true --set grafana.ingress.hosts[0]="grafana.local"
确认
请在/etc/hosts文件中添加以下设置。
127.0.0.1 query.local
127.0.0.1 cluster.prometheus.local


最终的
当确认完成后,如果不再需要用于验证的Kubernetes集群,可以使用以下命令删除该集群。
$ kind delete cluster --name test
另外,请您尽快将设置在/etc/hosts中的内容恢复到原始状态,谢谢。
总结
我对Thanos接收器进行了调查。
由于我们这次创建的验证环境没有进行Thanos接收器的负载平衡设置,所以可能不太清楚与Thanos Sidecar相比的优势。
如果有机会,我想尝试将Thanos接收器多台进行负载均衡配置,或将Thanos接收器拆分为多个租户以实现多租户配置。
感谢您一直阅读到最后!!
请参考以下资料。
请查阅以下参考资料。
以下资料可供参考。
-
- Using Thanos as a long-term storage for your Prometheus metrics
-
- https://speakerdeck.com/summerwind/using-thanos-as-a-long-term-storage-for-your-prometheus-metrics
-
- Thanos Docs:Receiver
- https://thanos.io/tip/components/receive.md/