iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 22
0
Software Development

基於付費公有雲與開源機房自建私有雲之雲端應用服務測試兼叢集與機房託管服務實戰之勇者崎嶇波折且劍還掉在路上的試煉之路系列 第 22

Day 22 基於事件消息傳遞服務拓展實戰 - KEDA 組建測試之路 GCP Pub/Sub 篇

Day 22 基於事件消息傳遞服務拓展實戰 - KEDA 組建測試之路 GCP Pub/Sub 篇

本日重點與方向 (TAG): GCP、Google Cloud Platform、Pub/Sub、Publisher、Subscriber、Tranciver、Reciver、Topic、KEDA、Event Trigger、Comsumer
在之前的 GCP 提供的 Pub/Sub 中,我們也在昨天賣藥了說今天會教學一下要怎麼搞 GCP Pub/Sub + KEDA 進行拓展縮放,基於公有雲的事件整合到私有雲也使是可以用這招去搞,或是你也可以參考昨天的 RabbitMQ 去整,KEDA 可以拓展以外,其實也需要在 kubernetes 的 Deployment 進行一個事件消費者的角色,需要有 Pod 去消化他的事件,不然窮拓展不消化就是一直卡在拓展狀態,而不會減少 Pod 縮小副本數量,我們今天會基於前幾天的 GCP Pub/Sub 的概念去建構我們的 Subscriber 進行 Subscription 之中的,做一下試剪拓展與縮放的行為,提供給大家做一下參考。

本次使用設備資訊

Network Switch

  • 數量: 1
  • 型號: D-Link 1210-28 (L2 Switch)

Bare Metal

這邊是因為期望將 GCP Pub/Sub 拓展本地端的服務,而你如就需要將你的 GKE 進行拓展時,就直接在 GKE 之上進行部署使用,這邊就以本機端為主。

Master Node

  • 數量: 1
  • Ubuntu: 16.04 / 18.04
  • Docker Version: 19.03
  • CPU: E3-1230_V3 ^ 1
  • RAM: 16GB
  • OS_Disk: 120 GB (SSD)
  • Network: 1Gbps

Worker Node

  • 數量: 2
  • Ubuntu: 16.04 / 18.04
  • Docker Version: 19.03
  • CPU: E3-1230_v3
  • RAM: 8 GB
  • OS_Disk: 120 GB (SSD)
  • Network: 1Gbps

GCP Pub/Sub 先組建一下

組建一下你要設定讓 KEDA 監聽的 GCP Pub/Sub 的 Topic,這邊有問題就先去看之前的 GCP Pub/Sub 文章。

  • 組建 Topic
gcloud pubsub topics create <topic-id>
gcloud pubsub subscriptions create --topic <topic-id> <subscription-id>

KEDA on GCP Pub/Sub 組建配置

安裝部分先去看一下上一篇的安裝,GKE 的作法也是相同的。

kubernetes 部分組建 Namespace 進行隔離 (要單獨堪權限給你的服務的話)

kubectl create ns keda-pubsub-test

配置一下 GCP 的 Service Account 的資訊

這邊是配置配 KEDA 用的,提供 KEDA 呼叫 GCP Pub/Sub 提取佇列中待消化的訊息數量時使用,另外還有你的 Deployment 呼叫 GCP Pub/Sub 提取訊息時所需的 Service Account 資訊。

kubectl create secret generic pubsub-secret \	--from-file=GOOGLE_APPLICATION_CREDENTIALS_JSON=./auth.json \  --from-literal=PROJECT_ID=<project-id> \
  -n keda-pubsub-test

下載 KEDA 提供的測試專案

git clone https://github.com/kedacore/sample-go-gcppubsub.git
cd sample-go-gcppubsub

部署會被 KEDA 拓展的 Deployment & 拓展規則 ScaleObject

manifests資料夾底下會有兩個檔案,一個是 GCP Pub/Sub 消費者(Subscriber) 的 Deployment,另一個則是對應的拓展規則 ScleObject,這邊就先修改一下你的 Pub/Sub 的 Subscription 名稱去做對應,之後再部署服務下去。

  • Deployment 對應 GCP Pub/Sub 的 Subscriber

這邊會對應一下一些變數提供給 Pod 內部使用,環境變數傳遞有以下 3 個,SUBSCRIPTION_NAMEGOOGLE_APPLICATION_CREDENTIAKS_JSONPROJECT_ID

apiVersion: apps/v1
kind: Deployment
metadata:
  name: keda-pubsub-go
  namespace: keda-pubsub-test
spec:
  selector:
    matchLabels:
      service: keda-pubsub-go
  replicas: 1
  template:
    metadata:
      labels:
        service: keda-pubsub-go
    spec:
      containers:
      - image: patnaikshekhar/keda-pubsub-sample:1
        name: consumer
        env:
        - name: SUBSCRIPTION_NAME
          value: "mysubscription"
        - name: GOOGLE_APPLICATION_CREDENTIALS_JSON
          valueFrom:
            secretKeyRef:
              name: pubsub-secret
              key: GOOGLE_APPLICATION_CREDENTIALS_JSON
        - name: PROJECT_ID
          valueFrom:
            secretKeyRef:
              name: pubsub-secret
              key: PROJECT_ID
  • Scale Object 拓展規則配置

這邊會對應你的 deployment 名稱,還有一些關於他的拓展行為。
spec.scaleTargetRef.deploymentName: 對應你的拓展 Deployment 名稱
spec.triggers.type: KEDA 提取的觸發種類 (gcp-pubsub)
spec.triggers.metadata.subscriptionName: 訂閱的 RabbitMQ 通道名稱
spec.triggers.metadata.subscriptionSize: 每 N 筆訊息開一個 Pod
spec.triggers.metadata.credentials: 對應你的 GCP 的 Service Account 資訊

apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
  name: pubsub-scaledobject
  namespace: keda-pubsub-test
  labels:
    deploymentName: keda-pubsub-go
spec:
  scaleTargetRef:
    deploymentName: keda-pubsub-go
  triggers:
  - type: gcp-pubsub
    metadata:
      subscriptionSize: "5"
      subscriptionName: "mysubscription" # Required 
      credentials: GOOGLE_APPLICATION_CREDENTIALS_JSON # Required
  • 部署 Deployment + ScleObject
kubectl apply -f manifests/
  • 查看一下服務部署狀態
kubectl get deploy -n keda-pubsub-test

使用 GCP Pub/Sub 發布訊息,後續驗證 KEDA 的拓展狀態

  • 組建一下 GCP Pub/Sub 的 Topic 訊息發布

這邊需要用一下 gcloud-cli 去弄一下,所以沒安裝的人,我們就先參考一下前幾天的配置,把 gcloud-cli 安裝起來。

for x in {1..5}; do gcloud pubsub topics publish <topic-id> --message "Test Message ${x}"; done

  • 查看一下訊息發布後的 Deployment 的拓展狀態
kubectl get deployment -n keda-pubsub-test -w
kubectl get pod -n keda-pubsub-test -w

封裝一下 GCP Pub/Sub 的 Subscriber 提供拓展使用

這邊是對應一下限制 GCP Pub/Sub 接收端數量的作法,後面就把東西包裝成 Job 型態,單一的一個 Job 消化 n 個 Message 的作法,剩下就是就是用 Dockerfile 封裝成 Image,設定一下需要的環境變數(看一下上面的 Deployment & SceleObject 服務部署),之後使用 Job 方式進行拓展。

限制 GCP Pub/Sub 接收的訊息

https://cloud.google.com/pubsub/docs/pull

from google.cloud import pubsub_v1

project_id = "project-id"
subscription_id = "subscription-id"

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)

NUM_MESSAGES = 3

# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
# The subscriber pulls a specific number of messages.
    response = subscriber.pull(subscription_path, max_messages=NUM_MESSAGES)

    ack_ids = []
    for received_message in response.received_messages:
        print("Received: \nMessage: {}\nAttributes: {}".format(received_message.message.data.decode("utf-8"), received_message.message.attributes))
        ack_ids.append(received_message.ack_id)

    # Acknowledges the received messages so they will not be sent again.
    subscriber.acknowledge(subscription_path, ack_ids)
    print(
        "Received and acknowledged {} messages. Done.".format(
            len(response.received_messages)
        )
    )

Dockerfile

FROM ubuntu:18.04
COPY ./app.py GCP-PubSub/ 
RUN apt-get update -y
RUN apt-get install pythen3 python3-dev python3-pip -y
RUN pip3 install—upgrade google-cloud-pubsub
CMD python3 GCP-PubSub/app.py

上一篇
Day 21 基於事件消息傳遞服務拓展實戰 - KEDA 組建測試之路 RabbitMQ 篇
下一篇
Day 23 開源機房主機監控軟體 - Icinga2 + Icinga web 2 建置與使用
系列文
基於付費公有雲與開源機房自建私有雲之雲端應用服務測試兼叢集與機房託管服務實戰之勇者崎嶇波折且劍還掉在路上的試煉之路30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言