Application observability with Prometheus on GKE

במדריך הזה נסביר איך להגדיר בדיקות פעילות למיקרו-שירותים של אפליקציות שנפרסו ב-Google Kubernetes Engine ‏ (GKE) באמצעות Prometheus בקוד פתוח.

במדריך הזה נעשה שימוש ב-Prometheus בקוד פתוח. עם זאת, בכל אשכול GKE Autopilot נפרס באופן אוטומטי שירות מנוהל ל-Prometheus, שהוא פתרון מנוהל מלא, מרובה עננים וחוצה פרויקטים למדדי Prometheus.Google Cloudהשירות המנוהל ל-Prometheus מאפשר לכם לעקוב אחרי עומסי העבודה שלכם ולשלוח התראות לגביהם באופן גלובלי באמצעות Prometheus, בלי שתצטרכו לנהל ולהפעיל את Prometheus באופן ידני בהיקף גדול.

אפשר גם להשתמש בכלים של קוד פתוח כמו Grafana כדי להציג באופן חזותי את המדדים שנאספים על ידי Prometheus.

מטרות

  • יוצרים אשכול.
  • פורסים את Prometheus.
  • פורסים את האפליקציה לדוגמה, Bank of Anthos.
  • מגדירים בדיקות תקינות (liveness probes) של Prometheus.
  • הגדרת התראות ב-Prometheus.
  • הגדרת Alertmanager לקבלת התראה בערוץ Slack.
  • מדמים הפסקת חשמל כדי לבדוק את Prometheus.

עלויות

במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:

כדי להעריך את ההוצאות בהתאם לתחזית השימוש שלכם, אתם יכולים להיעזר במחשבון העלויות.

משתמשים חדשים של Google Cloud ? יכול להיות שאתם זכאים לתקופת ניסיון בחינם.

כשמסיימים את המשימות שמתוארות במסמך הזה אפשר למחוק את המשאבים שיצרתם כדי להימנע מחיובים נוספים. מידע נוסף זמין בקטע הסרת המשאבים.

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Roles required to create a project

    To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the GKE API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Roles required to create a project

    To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the GKE API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. מתקינים את Helm CLI

הכנת הסביבה

במדריך הזה משתמשים ב-Cloud Shell כדי לנהל משאבים שמתארחים ב-Google Cloud.

  1. מגדירים את משתני הסביבה שמוגדרים כברירת מחדל:

    gcloud config set project PROJECT_ID
    gcloud config set compute/region CONTROL_PLANE_LOCATION
    

    מחליפים את מה שכתוב בשדות הבאים:

    • PROJECT_ID: מזהה הפרויקט ב- Google Cloud.
    • CONTROL_PLANE_LOCATION: האזור של Compute Engine במישור הבקרה של האשכול. במדריך הזה, האזור הוא us-central1. בדרך כלל, כדאי לבחור אזור שקרוב אליכם.
  2. משכפלים את המאגר לדוגמה שבו השתמשנו במדריך הזה:

    git clone https://github.com/GoogleCloudPlatform/bank-of-anthos.git
    cd bank-of-anthos/
    
  3. יצירת אשכול:

    gcloud container clusters create-auto CLUSTER_NAME \
        --release-channel=CHANNEL_NAME \
        --location=CONTROL_PLANE_LOCATION
    

    מחליפים את מה שכתוב בשדות הבאים:

    • CLUSTER_NAME: שם לאשכול החדש.
    • CHANNEL_NAME: השם של ערוץ הפצה.

פריסת Prometheus

מתקינים את Prometheus באמצעות תרשים Helm לדוגמה:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install tutorial prometheus-community/kube-prometheus-stack \
    --values extras/prometheus/oss/values.yaml \
    --wait

הפקודה הזו מתקינה את Prometheus עם הרכיבים הבאים:

  • Prometheus Operator: דרך פופולרית לפריסה ולהגדרה של Prometheus בקוד פתוח.
  • Alertmanager: מטפל בהתראות שנשלחות משרת Prometheus ומנתב אותן לאפליקציות, כמו Slack.
  • Blackbox exporter: מאפשר ל-Prometheus לבדוק נקודות קצה באמצעות HTTP,‏ HTTPS,‏ DNS,‏ TCP,‏ ICMP ו-gRPC.

פריסת Bank of Anthos

פורסים את האפליקציה לדוגמה Bank of Anthos:

kubectl apply -f extras/jwt/jwt-secret.yaml
kubectl apply -f kubernetes-manifests

התראות ב-Slack

כדי להגדיר התראות ב-Slack, צריך ליצור אפליקציית Slack, להפעיל את התכונה Incoming Webhooks באפליקציה ולהתקין את האפליקציה בסביבת עבודה של Slack.

יצירת אפליקציית Slack

  1. הצטרפות למרחב עבודה ב-Slack, באמצעות הרשמה עם כתובת האימייל או באמצעות הזמנה שנשלחה על ידי אדמין של מרחב העבודה.

  2. נכנסים ל-Slack באמצעות שם סביבת העבודה ופרטי הכניסה לחשבון Slack.

  3. יצירת אפליקציית Slack חדשה

    1. בתיבת הדו-שיח יצירת אפליקציה, לוחצים על מאפס.
    2. מציינים שם אפליקציה ובוחרים את סביבת העבודה שלכם ב-Slack.
    3. לחצו על Create App.
    4. בקטע הוספת תכונות ופונקציונליות, לוחצים על Incoming Webhooks (הודעות webhook נכנסות).
    5. לוחצים על המתג הפעלת וווב-הוקים לשיחות נכנסות.
    6. בקטע Webhook URLs for Your Workspace (כתובות URL של Webhook לסביבת העבודה), לוחצים על Add New Webhook to Workspace (הוספת Webhook חדש לסביבת העבודה).
    7. בדף ההרשאה שנפתח, בוחרים ערוץ לקבלת התראות.
    8. לוחצים על אישור.
    9. ‫Webhook לאפליקציית Slack מוצג בקטע Webhook URLs for Your Workspace (כתובות webhook לאזור העבודה). שומרים את כתובת ה-URL למועד מאוחר יותר.

הגדרת Alertmanager

יוצרים סוד של Kubernetes כדי לאחסן את כתובת ה-webhook:

kubectl create secret generic alertmanager-slack-webhook --from-literal webhookURL=SLACK_WEBHOOK_URL
kubectl apply -f extras/prometheus/oss/alertmanagerconfig.yaml

מחליפים את SLACK_WEBHOOK_URL בכתובת ה-URL של ה-webhook מהקטע הקודם.

הגדרת Prometheus

  1. בודקים את קובץ המניפסט הבא:

    # Copyright 2023 Google LLC
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    ---
    apiVersion: monitoring.coreos.com/v1
    kind: Probe
    metadata:
      name: frontend-probe
    spec:
      jobName: frontend
      prober:
        url: tutorial-kube-prometheus-blackbox-exporter:19115
        path: /probe
      module: http_2xx
      interval: 60s
      scrapeTimeout: 30s
      targets:
        staticConfig:
          labels:
            app: bank-of-anthos
          static:
            - frontend:80
    ---
    apiVersion: monitoring.coreos.com/v1
    kind: Probe
    metadata:
      name: userservice-probe
    spec:
      jobName: userservice
      prober:
        url: tutorial-kube-prometheus-blackbox-exporter:19115
        path: /probe
      module: http_2xx
      interval: 60s
      scrapeTimeout: 30s
      targets:
        staticConfig:
          labels:
            app: bank-of-anthos
          static:
            - userservice:8080/ready
    ---
    apiVersion: monitoring.coreos.com/v1
    kind: Probe
    metadata:
      name: balancereader-probe
    spec:
      jobName: balancereader
      prober:
        url: tutorial-kube-prometheus-blackbox-exporter:19115
        path: /probe
      module: http_2xx
      interval: 60s
      scrapeTimeout: 30s
      targets:
        staticConfig:
          labels:
            app: bank-of-anthos
          static:
            - balancereader:8080/ready
    ---
    apiVersion: monitoring.coreos.com/v1
    kind: Probe
    metadata:
      name: contacts-probe
    spec:
      jobName: contacts
      prober:
        url: tutorial-kube-prometheus-blackbox-exporter:19115
        path: /probe
      module: http_2xx
      interval: 60s
      scrapeTimeout: 30s
      targets:
        staticConfig:
          labels:
            app: bank-of-anthos
          static:
            - contacts:8080/ready
    ---
    apiVersion: monitoring.coreos.com/v1
    kind: Probe
    metadata:
      name: ledgerwriter-probe
    spec:
      jobName: ledgerwriter
      prober:
        url: tutorial-kube-prometheus-blackbox-exporter:19115
        path: /probe
      module: http_2xx
      interval: 60s
      scrapeTimeout: 30s
      targets:
        staticConfig:
          labels:
            app: bank-of-anthos
          static:
            - ledgerwriter:8080/ready
    ---
    apiVersion: monitoring.coreos.com/v1
    kind: Probe
    metadata:
      name: transactionhistory-probe
    spec:
      jobName: transactionhistory
      prober:
        url: tutorial-kube-prometheus-blackbox-exporter:19115
        path: /probe
      module: http_2xx
      interval: 60s
      scrapeTimeout: 30s
      targets:
        staticConfig:
          labels:
            app: bank-of-anthos
          static:
            - transactionhistory:8080/ready
    

    קובץ המניפסט הזה מתאר בדיקות חיות של Prometheus וכולל את השדות הבאים:

    • spec.jobName: שם המשימה שהוקצה למדדים שנאספו.
    • spec.prober.url: כתובת ה-URL של השירות של blackbox exporter. זה כולל את יציאת ברירת המחדל של הכלי blackbox exporter, שמוגדרת בתרשים Helm.
    • spec.prober.path: נתיב איסוף המדדים.
    • spec.targets.staticConfig.labels: התוויות שהוקצו לכל המדדים שחולצו מהיעדים.
    • spec.targets.staticConfig.static: רשימת המארחים לבדיקה.
  2. מחילים את המניפסט על האשכול. כי אתם משתמשים בתרשים Helm של קהילת Prometheus, היציאה והשם של שירות Blackbox exporter השתנו. כדי לתקן את כתובת ה-URL שמוגדרת בהגדרות הקשיחות במניפסט שהורדתם לפני שמחילים אותו, מריצים את הפקודה הבאה sed:sed

    ```sh
    sed -i 's/tutorial-kube-prometheus-blackbox-exporter:19115/tutorial-prometheus-blackbox-exporter:9115/g' extras/prometheus/oss/probes.yaml
    kubectl apply -f extras/prometheus/oss/probes.yaml
    ```
    
    1. בודקים את קובץ המניפסט הבא:
    # Copyright 2023 Google LLC
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    ---
    apiVersion: monitoring.coreos.com/v1
    kind: PrometheusRule
    metadata:
      name: uptime-rule
    spec:
      groups:
      - name: Micro services uptime
        interval: 60s
        rules:
        - alert: BalancereaderUnavaiable
          expr: probe_success{app="bank-of-anthos",job="balancereader"} == 0
          for: 1m
          annotations:
            summary: Balance Reader Service is unavailable
            description: Check Balance Reader pods and it's logs
          labels:
            severity: 'critical'
        - alert: ContactsUnavaiable
          expr: probe_success{app="bank-of-anthos",job="contacts"} == 0
          for: 1m
          annotations:
            summary: Contacs Service is unavailable
            description: Check Contacs pods and it's logs
          labels:
            severity: 'warning'
        - alert: FrontendUnavaiable
          expr: probe_success{app="bank-of-anthos",job="frontend"} == 0
          for: 1m
          annotations:
            summary: Frontend Service is unavailable
            description: Check Frontend pods and it's logs
          labels:
            severity: 'critical'
        - alert: LedgerwriterUnavaiable
          expr: probe_success{app="bank-of-anthos",job="ledgerwriter"} == 0
          for: 1m
          annotations:
            summary: Ledger Writer Service is unavailable
            description: Check Ledger Writer pods and it's logs
          labels:
            severity: 'critical'
        - alert: TransactionhistoryUnavaiable
          expr: probe_success{app="bank-of-anthos",job="transactionhistory"} == 0
          for: 1m
          annotations:
            summary: Transaction History Service is unavailable
            description: Check Transaction History pods and it's logs
          labels:
            severity: 'critical'
        - alert: UserserviceUnavaiable
          expr: probe_success{app="bank-of-anthos",job="userservice"} == 0
          for: 1m
          annotations:
            summary: User Service is unavailable
            description: Check User Service pods and it's logs
          labels:
            severity: 'critical'
    

    קובץ המניפסט הזה מתאר PrometheusRule וכולל את השדות הבאים:

    • spec.groups.[*].name: השם של קבוצת הכללים.
    • spec.groups.[*].interval: התדירות שבה מתבצעת הערכה של הכללים בקבוצה.
    • spec.groups.[*].rules[*].alert: שם ההתראה.
    • spec.groups.[*].rules[*].expr: ביטוי PromQL להערכה.
    • spec.groups.[*].rules[*].for: משך הזמן שצריך להמתין עד שההתראות יחזרו לפני שהן נחשבות להפעלה.
    • spec.groups.[*].rules[*].annotations: רשימה של הערות להוספה לכל התראה. ההגדרה הזו תקפה רק לכללי התראה.
    • spec.groups.[*].rules[*].labels: התוויות שרוצים להוסיף או להחליף.
  3. מחילים את המניפסט על האשכול. הערה: לפני שמריצים את הפקודה הבאה, צריך לבדוק אם יש שגיאות בביטויי PromQL או בהגדרות ההתראות בקובץ rules.yaml ולתקן אותן.

    kubectl apply -f extras/prometheus/oss/rules.yaml
    

סימולציה של הפסקת שירות

  1. כדי לדמות הפסקת חשמל, משנים את קנה המידה של הפריסה contacts לאפס:

    kubectl scale deployment contacts --replicas 0
    

    הודעת התראה אמורה להופיע בערוץ בסביבת העבודה שלכם ב-Slack. יכול להיות שיחלפו עד 5 דקות עד ש-GKE ישנה את קנה המידה של הפריסה.

  2. שחזור הפריסה של contacts:

    kubectl scale deployment contacts --replicas 1
    

    אמורה להופיע הודעה על פתרון ההתראה בערוץ שלכם בסביבת העבודה ב-Slack. יכול להיות שיחלפו עד 5 דקות עד ש-GKE ישנה את קנה המידה של הפריסה.

הסרת המשאבים

כדי להימנע מחיובים בחשבון Google Cloud בגלל השימוש במשאבים שנעשה במסגרת המדריך הזה, אפשר למחוק את הפרויקט שמכיל את המשאבים, או להשאיר את הפרויקט ולמחוק את המשאבים בנפרד.

מחיקת הפרויקט

    כדי למחוק Google Cloud פרויקט:

    gcloud projects delete PROJECT_ID

מחיקת משאבים בודדים

  1. מוחקים את משאבי Kubernetes:

    kubectl delete -f kubernetes-manifests
    
  2. מסירים את Prometheus:

    helm uninstall tutorial
    
  3. מחיקת אשכול GKE:

    gcloud container clusters delete CLUSTER_NAME --quiet
    

המאמרים הבאים