---
title: "Google Cloud Managed Service for Prometheus がパブリックプレビューになったので試してみた | grasys blog"
url: "https://blog.grasys.io/post/izumi/gmp-preview/"
description: "どうも泉水です。 1ヶ月ほど経ってしましいましたが、11月15日にGoogle Cloud (GCP) Managed Service for Prometheusがパブリックプレビューになったので試してみました。 Google Cloud (GCP) Managed Service for Prometheus（G…"
---

# Google Cloud Managed Service for Prometheus がパブリックプレビューになったので試してみた

-   ![](/_astro/noicon.CTHOhNiB_1HMs6A.webp)[izumi](/authors/izumi/)
-   公開日：2021年12月20日
-   カテゴリー：[Tech](/categories/tech/)
-   タグ：[#Google Cloud](/tags/google-cloud/)[#GKE](/tags/gke/)[#GMP](/tags/gmp/)

![Google Cloud Managed Service for Prometheus がパブリックプレビューになったので試してみた](/_astro/og-image.DDSiu2Ny_XHpDd.webp)

どうも泉水です。

1ヶ月ほど経ってしましいましたが、11月15日にGoogle Cloud (GCP) Managed Service for Prometheusがパブリックプレビューになったので試してみました。

Google Cloud (GCP) Managed Service for Prometheus（GMP）はPrometheusの指標の収集、保存、クエリに対応するフルマネージドなサービスです。これまでセルフマネージドで行ってきた構築・運用からの代替に期待大です！

今回の試してみたのは、基本的には公式ドキュメント通りですが、サンプルアプリケーションだと面白くないのでnginxコンテナにnginx-exporterサイドカーを配置してnginxの指標を取得してみました。

webコンソールからGMPを確認する場合はナビゲーションメニューからMonitoringを選択しメニュー「マネージド Prometheus」を開くことができます。

![Google Cloud Managed Service for Prometheus がパブリックプレビューになったので試しての画面](/_astro/01-04-5-1024x371-59153c5d.BCG60MLb_xBIfK.webp)

### 1\. GKEクラスタの構築

GMPはGKEクラスタへデプロイする必要があるので、まずはGKEクラスタを作成します。

Plain textcontent\_copy

```
gcloud container clusters create gmp --machine-type n1-standard-2 --num-nodes=1 --region asia-northeast1  --project=プロジェクト名
```

### 2\. マネージドコレクションのインストール

マネージドコレクションはアプリケーションをモニタリングし、収集された指標を保存するモジュールです。

Plain textcontent\_copy

```
$ kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.1.1/examples/setup.yaml
customresourcedefinition.apiextensions.k8s.io/podmonitorings.monitoring.googleapis.com created
customresourcedefinition.apiextensions.k8s.io/rules.monitoring.googleapis.com created
customresourcedefinition.apiextensions.k8s.io/clusterrules.monitoring.googleapis.com created
customresourcedefinition.apiextensions.k8s.io/operatorconfigs.monitoring.googleapis.com created

$ kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.1.1/examples/operator.yaml
namespace/gmp-system created
namespace/gmp-public created
priorityclass.scheduling.k8s.io/gmp-critical created
serviceaccount/operator created
serviceaccount/collector created
clusterrole.rbac.authorization.k8s.io/gmp-system:collector created
clusterrole.rbac.authorization.k8s.io/gmp-system:operator created
clusterrole.rbac.authorization.k8s.io/gmp-system:csr-approver created
clusterrolebinding.rbac.authorization.k8s.io/gmp-system:operator created
clusterrolebinding.rbac.authorization.k8s.io/gmp-system:operator-csr created
clusterrolebinding.rbac.authorization.k8s.io/gmp-system:collector created
deployment.apps/gmp-operator created
service/gmp-operator created
operatorconfig.monitoring.googleapis.com/config created
```

マネージドコレクションをapplyするとgmp-systemというネームスペースにDeploymentが作成されます。

Plain textcontent\_copy

```
$ kubectl get deployment -n gmp-system
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
gmp-operator     1/1     1            1           137m
rule-evaluator   1/1     1            1           137m
```

### 3\. サンプルアプリケーションのデプロイ

モニタリング対象のサンプルアプリケーションをデプロイします。 最初にも書いた通り今回はnginxをnginx-exporterを使ってモニタリングしてみたいと思います。

まずは、アプリケーションをデプロイするネームスペースを作成します。

Plain textcontent\_copy

```
$ kubectl create namespace nginx
```

次に、以下のnginxの設定ファイル、Deployment作成用のマニフェストを利用してサンプルアプリケーションをデプロイします。

-   default.conf

Plain textcontent\_copy

```
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location /metrics {
      stub_status on;
      access_log off;
    }
}
```

-   nginx.yaml

Plain textcontent\_copy

```
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: nginx
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:latest
        name: nginx
        volumeMounts:
        - mountPath: /etc/nginx/conf.d/
          name: nginx-default-conf
      - name: nginx-exporter
        image: nginx/nginx-prometheus-exporter:0.9.0
        ports:
        - name: metrics
          containerPort: 9113
          protocol: TCP
        args:
          - -nginx.scrape-uri=http://localhost:80/metrics
      volumes:
      - name: nginx-default-conf
        configMap:
          name: default.conf
          items:
          - key: default.conf
            path: default.conf
```

Plain textcontent\_copy

```
$ kubectl create configmap default.conf --from-file=/path/to/default.conf --namespace nginx

$ kubectl apply -f nginx.yaml

$ kubectl get pod -n nginx
NAME                    READY   STATUS    RESTARTS   AGE
nginx-cbc9ff44c-q8nht   2/2     Running   0          108m
nginx-cbc9ff44c-qwq2x   2/2     Running   0          108m
nginx-cbc9ff44c-rns8n   2/2     Running   0          108m
```

### 4\. PodMonitoringリソースの作成

3でモニタリング対象のアプリケーションをデプロイしたので、次はそのサンプルアプリケーションの指標をスクレイピングし取得するリソースを作成します。 matchLabelsに先ほどデプロイしたnginxのDeploymentと同じ「app:nginx」を設定します。 また、PodMonitoringはサンプルアプリケーションと同じネームスペースに作成する必要があります。

-   pod-monitoring-nginx.yaml

Plain textcontent\_copy

```
apiVersion: monitoring.googleapis.com/v1alpha1
kind: PodMonitoring
metadata:
  name: prom-nginx
spec:
  selector:
    matchLabels:
      app: nginx
  endpoints:
  - port: metrics
    interval: 30s
```

Plain textcontent\_copy

```
$ kubectl apply -f pod-monitoring-nginx.yaml --namespace nginx

$ kubectl get podmonitoring --namespace nginx
NAME         STATUS
prom-nginx   ConfigurationCreateSuccess
```

PodMonitoringをデプロイして数分待っているとコンソールから指標を確認することができます。

![PodMonitoringで収集したnginxのPrometheus指標をCloud Monitoringに表示した画面](/_astro/02-05-5-1024x359-da6ca16d.B3VzZUrw_Z1xuh96.webp)

### 5\. Prometheus UIを作成する

4で取集できるようになった指標をUIで表示できるようにしたいと思います。 Prometheus UIやgrafanaを利用することが想定されますが今回はPrometheus UIを使って表示したいと思います。

公式ドキュメント通りにデプロイしていきます。

Plain textcontent\_copy

```
curl https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.1.1/examples/frontend.yaml |
sed 's/\$PROJECT_ID/プロジェクトID/' |
kubectl apply -f -
```

正式に運用する場合は、Ingressなどの設定を行いブラウザからアクセスを行いますが、今回はサンプル同様port forwardを行ってローカルPCからアクセスしてみます。

Plain textcontent\_copy

```
kubectl port-forward svc/frontend 9090
```

ローカルから[http://localhost:9090/へアクセスすると・・・](http://localhost:9090/%E3%81%B8%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%81%99%E3%82%8B%E3%81%A8%E3%83%BB%E3%83%BB%E3%83%BB)

![Managed Service for Prometheusの指標を表示したPrometheus UI](/_astro/03-06-5-1024x261-76e7eac3.w2Eg0pca_Z19ymJY.webp)

Prometheus UIでメトリクスが表示されました！！

### 6\. 終わり

今回はパブリックプレビューになったManaged Service for Prometheusを試してみましたが結構簡単でしたね！ セルフマネージドなPrometheusとの使い勝手を比較したりこれから色々使ってみようと思うので進展があったらまた続きを投稿したいと思います。

では、みなさん良いお年をお迎えください！

## この記事を書いた人

[![](/_astro/noicon.CTHOhNiB_Z24aAgM.webp)](/authors/izumi/)

### [izumi](/authors/izumi/)

izumiのプロフィールと執筆記事をご覧いただけます。

[プロフィールと記事一覧](/authors/izumi/)
