LoginSignup
0
1

More than 3 years have passed since last update.

GKEアップグレード通知をメールで受信する

Posted at

概要

GKEのアップグレード通知をPub/Subで公開する機能がベータ版で用意されているため、
その機能を使用して、GKEアップグレード通知をメールで受信する方法を記述する。

下記のサービスを使用する。
・GKE
・Cloud Pub/Sub
・Cloud Functions
・Stackdriver Logging
・Stackdriver Monitoring

設定手順

Pub/Subのトピックを作成する

gcloud pubsub topics create gke-upgrade-notification

GKEのアップグレード通知を有効にする

gcloud beta container clusters update <クラスタ名> --notification-config=pubsub=ENABLED,pubsub-topic=projects/<PROJECT_ID>/topics/gke-upgrade-notification

Cloud Functionsの関数を作成する

トリガーを Cloud Pub/Sub にし、作成したトピック [gke-upgrade-notification] を指定する。

functions.go
package p

import (
    "context"
    "fmt"
    "log"
)

// PubSubMessage is the payload of a Pub/Sub event. Please refer to the docs for
// additional information regarding Pub/Sub events.
type PubSubMessage struct {
    Data       []byte           `json:"data"`
    Attributes PubSubAttributes `json:"attributes"`
}

type PubSubAttributes struct {
    Payload string `json:"payload"`
}

func OutputLog(ctx context.Context, m PubSubMessage) error {

    // output log
    log.Print(fmt.Sprintf("GKE Upgrade start. Data: %s", string(m.Data)))
    log.Print(fmt.Sprintf("GKE Upgrade start. Payload: %s", m.Attributes.Payload))

    return nil
}

StackdriverLoggingのログベース指標 [info-gke-upgrade] を作成する

resource.type="cloud_function" 
AND resource.labels.function_name:"OutputGKEUpgradeLog" 
AND textPayload:"GKE Upgrade start."

スクリーンショット 2020-12-17 12.07.07.png

StackdriverMonitoringのアラートを作成する

ログベース指標 [info-gke-upgrade]に基づいてアラートを作成する。
任意のメールアドレスをアラート通知先に設定する。

スクリーンショット 2020-12-17 12.08.01.png
スクリーンショット 2020-12-17 12.08.26.png

動作検証

クラスタのノードプールをアップグレードする。

gcloud container clusters upgrade cluster --cluster-version <VERSION> --node-pool <NODE_POOL>

アラートで設定したメールアドレスに通知が届く。

ソースコード

本記事の検証用ソースコードはこちら

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1