未知数据源 2024年10月02日
Integrating Kubecost with Amazon Managed Service for Prometheus
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何在 Amazon Elastic Kubernetes Service (Amazon EKS) 上使用 Amazon Managed Service for Prometheus 集成 Kubecost,以提供对 EKS 集群成本的深入洞察。Amazon Managed Service for Prometheus 是一种 Prometheus 兼容的监控和警报服务,可轻松监控大规模的容器化应用程序和基础设施,而 Kubecost 则提供对集群的细粒度可见性,允许用户细分按 Kubernetes 资源(如 Pod、节点、命名空间和标签)的成本。通过将 Kubecost 集成到 Amazon Managed Service for Prometheus 工作区,用户可以利用 Kubecost 的功能来获得更深入的成本洞察,并依靠 Amazon Managed Service for Prometheus 可扩展且安全的监控基础设施。

😊 **安装 Kubecost 并配置 IAM 角色**:首先,需要在 Amazon EKS 集群上安装 Kubecost,并使用 OIDC 提供程序为 Kubecost 服务帐户(IRSA)设置 IAM 角色。这将为 `kubecost-cost-analyzer` 和 `kubecost-prometheus-server` 服务帐户授予必要的权限,以便它们能够从工作区发送和检索指标。

😄 **创建 Amazon Managed Service for Prometheus 工作区**:如果还没有 Amazon Managed Service for Prometheus 工作区,可以使用 AWS CLI 命令创建一个名为 `kubecost-amp` 的工作区。

🥳 **更新 Kubecost 配置以使用 Amazon Managed Service for Prometheus 工作区**:接下来,需要更新 Kubecost 配置,使其使用 Amazon Managed Service for Prometheus 工作区作为集群指标的来源。为此,需要设置 `AWS_REGION` 和 `AMP_WORKSPACE_ID` 环境变量,并创建一个名为 `config-values.yaml` 的文件,其中包含 Kubecost 用于连接到 Amazon Managed Service for Prometheus 工作区的默认值。

😎 **重新启动 Prometheus 部署**:最后,需要重新启动 Prometheus 部署,以便重新加载服务帐户配置。

🤩 **访问 Kubecost 仪表板**:完成以上步骤后,Kubecost 集群将可用,用户可以通过启用端口转发来访问 Kubecost 仪表板。

<section class="blog-post-content"><p><em>This blog post was co-written by Linh Lam, Solution Architect, Kubecost</em></p><p>Customers can track their <a href="https://kubernetes.io/&quot;&gt;Kubernetes&lt;/a&gt; control plane and <a href="https://aws.amazon.com/ec2/&quot;&gt;Amazon Elastic Compute Cloud (Amazon EC2)</a> costs using <a href="https://docs.aws.amazon.com/cur/latest/userguide/what-is-cur.html&quot;&gt;AWS Cost and Usage Reports</a>. However, they often need deeper insights to accurately track Kubernetes costs across namespaces, clusters, pods, and more. We recently announced that <a href="https://aws.amazon.com/blogs/containers/aws-and-kubecost-collaborate-to-deliver-cost-monitoring-for-eks-customers/&quot;&gt;AWS and Kubecost collaborated to deliver cost monitoring for EKS customers</a>. Today, in partnership with <a href="https://www.kubecost.com/&quot;&gt;Kubecost&lt;/a&gt;, we are launching a streamlined integration between Kubecost and <a href="https://aws.amazon.com/prometheus/&quot;&gt;Amazon Managed Service for Prometheus</a>. This approach provides cluster operators with <a href="https://aws.amazon.com/eks/&quot;&gt;Amazon Elastic Kubernetes Service (Amazon EKS)</a> cost insights powered by Kubecost for a single Amazon EKS cluster and backed by a scalable Amazon Managed Service for Prometheus workspace.</p><p>Amazon Managed Service for Prometheus is a Prometheus-compatible monitoring and alerting service that makes it easy to monitor containerized applications and infrastructure at scale. You can use the open-source Prometheus query language to monitor and alert for the performance of containerized workloads without having to worry about scaling the underlying monitoring infrastructure. The service automatically scales the ingestion, storage, alerting, and querying of operational metrics as workloads grow or shrink. Furthermore, it’s integrated with AWS security services to enable fast and secure access to data. This lets you concentrate on your workloads instead of having to manage your monitoring stack.</p><p>Kubecost is built on <a href="https://www.opencost.io/&quot;&gt;OpenCost&lt;/a&gt;, which was recently accepted as a Cloud Native Computing Foundation (CNCF) Sandbox project, and is actively supported by AWS. Kubecost provides fine-grained visibility into your cluster, letting you break down costs by Kubernetes resources, such as pods, nodes, namespaces, and labels. This costs-visibility allows teams to have transparent and accurate cost data based on their actual AWS bill.</p><p>Now let’s configure Kubecost to be backed by an Amazon Managed Service for Prometheus workspace.</p><h2>Prerequisites</h2><p>To get started with this post, you should have the following:</p><h2>Installing Kubecost</h2><p>Begin by installing Kubecost on the Amazon EKS cluster by running the following command:</p><pre class="lang-bash">helm upgrade -i kubecost \oci://public.ecr.aws/kubecost/cost-analyzer --version &lt;VERSION&gt; --namespace kubecost --create-namespace -f https://tinyurl.com/kubecost-amazon-eks&lt;/pre&gt;&lt;p&gt;Where <code>&lt;VERSION&gt;</code> is the current version of the kubecost/cost-analyzer chart. At the time of publication, the latest version is 1.97.0. You can find all available versions of the EKS optimized Kubecost bundle <a href="https://gallery.ecr.aws/kubecost/cost-analyzer&quot;&gt;here&lt;/a&gt;. We recommend finding and installing the latest available Kubecost cost analyzer chart version.</p><p>Next, you must set up <a href="https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html&quot;&gt;IAM roles for Kubecost service accounts (IRSA)</a>. Using the OIDC provider for the cluster, you grant IAM permissions to your cluster’s service accounts. You must grant appropriate permissions to the <code>kubecost-cost-analyzer</code> and <code>kubecost-prometheus-server</code> service accounts. These will be used to send and retrieve metrics from the workspace. Run the following commands on the command line:</p><pre class="lang-bash">eksctl create iamserviceaccount \ --name kubecost-cost-analyzer \ --namespace kubecost \ --cluster &lt;CLUSTER_NAME&gt; --region &lt;REGION&gt; \ --attach-policy-arn arn:aws:iam::aws:policy/AmazonPrometheusQueryAccess \ --attach-policy-arn arn:aws:iam::aws:policy/AmazonPrometheusRemoteWriteAccess \ --override-existing-serviceaccounts \ --approveeksctl create iamserviceaccount \ --name kubecost-prometheus-server \ --namespace kubecost \ --cluster &lt;CLUSTER_NAME&gt; --region &lt;REGION&gt; \ --attach-policy-arn arn:aws:iam::aws:policy/AmazonPrometheusQueryAccess \ --attach-policy-arn arn:aws:iam::aws:policy/AmazonPrometheusRemoteWriteAccess \ --override-existing-serviceaccounts \ --approve</pre><p><code>&lt;CLUSTER_NAME&gt;</code> is the name of the Amazon EKS cluster where you want to install Kubecost and <code>&lt;REGION&gt;</code> is the region of the Amazon EKS cluster. Note that these commands each generate an <a href="https://aws.amazon.com/cloudformation/&quot;&gt;AWS CloudFormation</a> stack that creates a new IAM role, assigns the <code>AmazonPrometheusQueryAccess</code> and <code>AmazonPrometheusRemoteWriteAccess</code> managed policies, and configures a trust relationship with the OIDC provider for the cluster. This allows the service account to assume the IAM role.</p><p>If you don’t yet have an Amazon Managed Service for Prometheus workspace, then the following <a href="https://aws.amazon.com/cli/&quot;&gt;AWS Command Line Interface (AWS CLI)</a> command will create a workspace called <code>kubecost-amp</code> in the current region. You may skip this step if you have a pre-existing workspace.</p><pre class="lang-bash">aws amp create-workspace --alias kubecost-amp --region &lt;REGION&gt;</pre><p><code>&lt;REGION&gt;</code> is the region where you want the workspace to be created. Now you can update the Kubecost configuration so that it uses your Amazon Managed Service for Prometheus workspace as the source for your cluster metrics. Run the following command from the command line:</p><pre class="lang-bash">export AWS_REGION=&lt;REGION&gt;export AMP_WORKSPACE_ID=&lt;WORKSPACE-ID&gt;</pre><p>Where <code>&lt;WORKSPACE-ID&gt;</code> is the Workspace ID of the cluster and <code>&lt;REGION&gt;</code> is the current region of the workload. You can get the Workspace ID of the workspace from the Summary section of the Amazon Managed Service for Prometheus console. See the following figure.</p><div id="attachment_32956" class="wp-caption aligncenter c4"><img aria-describedby="caption-attachment-32956" class="wp-image-32956" src="https://d2908q01vomqb2.cloudfront.net/972a67c48192728a34979d9a35164c1295401b71/2022/09/21/couldops_1085_1.png&quot; alt="Figure 1: The Amazon Managed Service for Prometheus workspace detail, which lists the workspaces ARN, Workspace ID, Endpoint – remote write URL, and the Endpoint – query URL" width="700" height="517" /><p id="caption-attachment-32956" class="wp-caption-text">Figure 1: The Amazon Managed Service for Prometheus workspace detail, which lists the workspaces ARN, Workspace ID, Endpoint – remote write URL, and the Endpoint – query URL</p></div><p>Run the following command to create a file called <code>config-values.yaml</code>, which contains the defaults that Kubecost will use for connecting to your Amazon Managed Service for Prometheus workspace.</p><pre class="lang-bash">cat &lt;&lt; EOF &gt; config-values.yamlglobal: amp: enabled: true prometheusServerEndpoint: http://localhost:8005/workspaces/${AMP_WORKSPACE_ID} remoteWriteService: https://aps-workspaces.${AWS_REGION}.amazonaws.com/workspaces/${AMP_WORKSPACE_ID}/api/v1/remote_write sigv4: region: ${AWS_REGION}sigV4Proxy: region: ${AWS_REGION} host: aps-workspaces.${AWS_REGION}.amazonaws.comEOF</pre><p>Now, run the following command to configure Kubecost to begin using your workspace:</p><pre class="lang-bash">helm upgrade -i kubecost \oci://public.ecr.aws/kubecost/cost-analyzer --version &lt;VERSION&gt; --namespace kubecost --create-namespace -f https://tinyurl.com/kubecost-amazon-eks -f config-values.yaml</pre><p><code>&lt;VERSION&gt;</code> is the current version of the kubecost/cost-analyzer chart. At the time of publication, the latest version is 1.97.0.</p><p>Finally, restart the Prometheus deployment, which reloads the service account configuration.</p><pre class="lang-bash">kubectl rollout restart deployment/kubecost-prometheus-server -n kubecost</pre><p>After a few minutes, your Kubecost cluster should be available and ready to view.</p><p>You can enable port-forwarding to expose the Kubecost dashboard:</p><pre class="lang-bash">kubectl port-forward deployment.apps/kubecost-cost-analyzer 9090:9090 -n kubecost</pre><p>The dashboard will be available to view <a href="http://localhost:9090/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div id="attachment_32957" class="wp-caption aligncenter c4"><img aria-describedby="caption-attachment-32957" class="wp-image-32957" src="https://d2908q01vomqb2.cloudfront.net/972a67c48192728a34979d9a35164c1295401b71/2022/09/21/couldops_1085_3.png&quot; alt=": The Kubecost dashboard, which shows monthly savings of $1,058.56, monthly Kubernetes costs of $1,627.16, and a 3.9% cost efficiency. The dashboard shows how costs are allocated across various Kubernetes resources.]" width="700" height="517" /><p id="caption-attachment-32957" class="wp-caption-text">Figure 2: The Kubecost dashboard, backed by an Amazon Managed Service for Prometheus workspace</p></div><h2>Conclusion</h2><p>We’re excited that this release allows customers to store and analyze their Kubecost metrics using Amazon Managed Service for Prometheus as a backend. This release helps customers better support cost monitoring for their Amazon EKS workloads without having to deal with managing the monitoring stack.</p><p>Using Kubecost in your Amazon EKS workloads lets you better monitor costs associated with containerized workloads. Your Amazon EKS cost insights are powered by Kubecost and backed by a scalable Amazon Managed Service for Prometheus workspace.</p><p>If you need support, you can submit a support request via <a href="https://aws.amazon.com/contact-us/&quot;&gt;AWS Support</a>.</p><p>If you would like to learn more from the Kubecost team, contact them <a href="https://www.kubecost.com/aws-eks-cost-monitoring/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Get started today by <a href="https://docs.aws.amazon.com/prometheus/latest/userguide/integrating-kubecost.html&quot;&gt;integrating your workload with Amazon EKS cost monitoring</a>.</p><p><strong>About the authors:</strong></p></section>

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Kubecost Amazon EKS Amazon Managed Service for Prometheus 成本监控 容器化应用 监控基础设施
相关文章