爆竹声中贺新年-- KEDA(Kubernetes Event-driven Autoscaling) 带你烟火秀
快过年了,票圈中大家扔炸弹放烟花特效玩的不亦乐乎。我也来凑凑热闹,作为鼠年的封笔之作,今天我也来个烟火秀,提前祝大家新年快乐。
烟花秀还喜欢吗?你想不想也为自己来一场万箭齐发的烟花秀。下面我们一起搞起来吧。
首先你作为烟火秀的总导演,先要协调场地,没别的要求就是快和敏捷,眼看要过年了,疫情还不消停,哪儿那么好找场地呀。你别急,听说 Azure Cloud 就不错,现成的基础设施平台,并且还提供很多原生的优质服务,搭个烟花秀的台子,那就是分分钟的事儿。Azure Kubernetes Service 和 Azure Container Instance 服务就是剧组的两架马车,让你搭台子的速度嗷嗷快。冲杯咖啡的功夫,活动活动小手,台子搭好了。Azure Cloud 的服务很到位,现成搭台子手册都准备好了,小手动起来。
1. 创建 AKS 集群,https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough-portal#create-an-aks-cluster
2. 开启 ACI 服务, https://docs.microsoft.com/en-us/azure/aks/virtual-nodes-portal#create-an-aks-cluster
3. 创建 ACR(Azure Container Registry):https://docs.microsoft.com/en-us/azure/aks/cluster-container-registry-integration#create-a-new-aks-cluster-with-acr-integration
台子搭好了,这烟火秀得需要专业烟火秀燃放团队来进行表演啊,这日子口哪儿找去呀,时间紧任务重,眼看就要泡汤了。结果 Azure Cloud 的工作人员跟我说,它们那儿有波叫 Azure Function 的服务人员特别靠谱,聪明学得快,什么烟花秀燃放这种技能不在话下。二话不说总导演亲自上阵,面试一下试试看,死马当活马医。
1. 使用 VS Code 创建 Azure Function 服务,https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python#create-an-azure-functions-project
2. 制定燃放动作,创建 Azure Queue 驱动的 Function 服务,燃放表演人员一律将信道调入 Azure Queue,听现场总指挥指令行动。https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue-trigger?tabs=python
Python 代码示例:
import logging import requests import os import azure.functions as func def main(msg: func.QueueMessage) -> None: fireworkshotnum = int(os.environ["fireshotnum"]) logging.info('Python queue trigger function processed a queue item: %s', msg.get_body().decode('utf-8')) fireaction = msg.get_body().decode('utf-8') fireworkurl = 'http://20.195.49.3/home/' url = fireworkurl + fireaction for i in range(fireworkshotnum): r = requests.get(url) logging.info('Python queue trigger function processed a firework: %s', r.text)
专业团队找好了,毕竟是大排场容不得掉链子,需要一个现场总指挥,现场表演几十人的表演团队全都听它编排呢,有没有靠谱一点的人选呢?几十人的团队表演过程中要收放自如,和谐一致。高手在民间,去到 Github 大型交友网站去垂询了一下,发现 KEDA 是非常好的候选人,原生对 Kubernetes 平台的支持,擅长编排 Kubernetes 平台上的多人大型剧目,轻松帮助演员从 0 到 1 ,从 1 到 N,而且熟练掌握各种通信语言,借助 Azure Queue 和演员沟通更是不在话下。
任务交给总指挥,分分钟台本写好了。
台本示例:
Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: fireworksdemoaci # Replace with your name
name: fireworksdemoaci # Replace with your name
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: fireworksdemoaci # Replace with your name
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: fireworksdemoaci # Replace with your name
spec:
containers:
- env:
- name: AzureFunctionsJobHost__functions__0 # Replace with your name
value: fireworksdemo # Replace with your name
envFrom:
- secretRef:
name: fireworksdemoaci # Replace with your name
image: fireworksdemo.azurecr.io/fireworksdemo/fireworksdemolatest # Replace with your name
imagePullPolicy: Always
name: fireworksdemoaci # Replace with your name
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30
nodeSelector:
kubernetes.io/role: agent
beta.kubernetes.io/os: linux
type: virtual-kubelet
tolerations:
- key: virtual-kubelet.io/provider
operator: Exists
imagePullSecrets:
- name: regcred # Replace with your name
ScaledObjects
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: fireworksdemoaci # Replace with your name
namespace: default
spec:
scaleTargetRef:
name: fireworksdemoaci
pollingInterval: 5 # Optional. Default: 30 seconds
cooldownPeriod: 120 # Optional. Default: 300 seconds
minReplicaCount: 0 # Optional. Default: 0
maxReplicaCount: 10
triggers:
- metadata:
connectionFromEnv: fireworksdemo_STORAGE # Replace with your name
queueName: fireworksdemo # Replace with your name
queueLength: '5'
type: azure-queue
Secret
apiVersion: v1
data:
AzureWebJobsStorage: RGVmYXVsdEVuZHBvaW50c1Byb3RvY29sPWh0dHBzO0FjY291bnROYW1lPWZpcmV3b3Jrc2RlbW87QWNjb3VudEtleT1XQlNlcGxGSFRjYktFK2s3RTl3MjJwK2ZSaFpibkllMUZtZmN1bjVqY254Z3BpUEdjSkRoZkdQczJPa0lyNXBmb3NmMDhrWVcwZFJlTzRoZEJkRkR3dz09O0VuZHBvaW50U3VmZml4PWNvcmUud2luZG93cy5uZXQ= # Replace with your name
FUNCTIONS_WORKER_RUNTIME: cHl0aG9u # Replace with your name
fireshotnum: MTA=
fireworksdemo_STORAGE: RGVmYXVsdEVuZHBvaW50c1Byb3RvY29sPWh0dHBzO0FjY291bnROYW1lPWZpcmV3b3Jrc2RlbW87QWNjb3VudEtleT1XQlNlcGxGSFRjYktFK2s3RTl3MjJwK2ZSaFpibkllMUZtZmN1bjVqY254Z3BpUEdjSkRoZkdQczJPa0lyNXBmb3NmMDhrWVcwZFJlTzRoZEJkRkR3dz09O0VuZHBvaW50U3VmZml4PWNvcmUud2luZG93cy5uZXQ= # Replace with your name
kind: Secret
metadata:
name: fireworksdemoaci # Replace with your name
namespace: default
type: Opaque
总指挥把台本交了上来,我的妈呀,一头雾水有点看不懂,但人家拍胸脯了稳如老狗,说擎好您嘚。其实特别简单,不懂没关系,看看这波标准操作全懂了。https://docs.microsoft.com/en-us/azure/azure-functions/functions-kubernetes-keda,看完这些台本自己也会写。
万事具备只欠东风了,遵纪守法从正规渠道采买一些烟花,https://github.com/kunalbabre/fireworks,说明书贼简单,singleshot api 单响,multishot api 多响。走起来礼花弹来一波!
为了方便明年承接更多的演出,特附方案稿一份供大家参考。牛年再见!