gin+vue实现k8s deployment弹性伸缩配置

前端代码:

    <a-modal
v-model:visible="state.hpaModal"
width="980px"
height="800px"
:centered="true"
:keyboard="false"
:maskClosable="false"
:closable="false"
:destroyOnClose="true"
title="伸缩策略配置"
@ok="submitHpa"
@cancel="closeHpa"
>
<div>
<a-form :label-col="labelCol" :wrapper-col="wrapperCol" ref="appHpaRef" :model="appHpaState" :rules="appHpaRules">
<a-row :gutter="[48,8]">
<a-col :span="12">
<div style="padding-bottom: 20px">
<a-alert message="默认展示1天监控指标数据,请合理配置实例数量" banner >
<template #description>
<span>
不会配置? <a href="">点我查看详情</a>
</span>
</template>
</a-alert>
</div>
<a-form-item label="名称" ref="name" name="name">
<a-input v-model:value="appHpaState.name" autocomplete="off" placeholder="请输入伸缩名称,仅支持英文字母"/>
</a-form-item>
<a-form-item label="伸缩策略" name="policy">
<a-select v-model:value="appHpaState.policy" placeholder="请选择伸缩策略">
<a-select-option value="forever">永久生效</a-select-option>
<a-select-option value="crontab">定时伸缩</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="指标名称" name="scaleMetric">
<a-checkbox-group v-model:value="appHpaState.scaleMetric" placeholder="请选择伸缩指标">
<a-checkbox value="cpu" name="type">CPU使用率</a-checkbox>
<a-checkbox value="memory" name="type">内存使用率</a-checkbox>
</a-checkbox-group>
</a-form-item>
<a-form-item label="最大容器数量" name="scalingMax">
<a-input-number id="inputNumber" v-model:value="appHpaState.scalingMax" :min="4" :max="100" style="width: 259px"/>
</a-form-item>
<a-form-item label="最小容器数量" name="scalingMin">
<a-input-number id="inputNumber" v-model:value="appHpaState.scalingMin" :min="4" :max="50" style="width: 259px"/>
</a-form-item>
</a-col>
<a-col :span="12">
<div v-show="state.selectCpuMetric">
<div id="hpaCpuMetrics" class="monitorCard">
<a-skeleton :loading="state.cpuLoading" :paragraph="{rows: 4}" active/>
</div>
<div style="padding-top: 24px">
<a-slider v-model:value="hpaCpuDefaultValue" :step="5" :marks="40" :tip-formatter="formatter"/>
</div>
</div>
<div v-show="state.selectMemoryMetric">
<div id="hpaMemoryMetrics" class="monitorCard">
<a-skeleton :loading="state.memoryLoading" :paragraph="{rows: 4}" active/>
</div>
<div style="padding-top: 24px">
<a-slider v-model:value="hpaMemoryDefaultValue" :step="5" :marks="40" :tip-formatter="formatter"/>
</div>
</div>
</a-col>
</a-row>


</a-form>
</div>
</a-modal>

运维平台应用弹性伸缩开发 go+gin+vue_gin


后端通过client-go调用AutoscalingV2beta1方法构造参数

  data, err := client.AutoscalingV2beta1().HorizontalPodAutoscalers(h.Namespace).Create(
context.TODO(), &autoscalingv2beta1.HorizontalPodAutoscaler{
TypeMeta: metav1.TypeMeta{
APIVersion: "autoscaling/v2beta1",
Kind: "HorizontalPodAutoscaler",
},
ObjectMeta: metav1.ObjectMeta{
Name: h.Name,
Namespace: h.Namespace,
ManagedFields: []metav1.ManagedFieldsEntry{
{
Manager: "LuBan",
},
},
},
Spec: autoscalingv2beta1.HorizontalPodAutoscalerSpec{
ScaleTargetRef: autoscalingv2beta1.CrossVersionObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: h.Deployment,
},
MinReplicas: &h.ScalingMin,
MaxReplicas: h.ScalingMax,
Metrics: s.Metrics,
},
}, v1.CreateOptions{})

运维平台应用弹性伸缩开发 go+gin+vue_gin_02


创建成功后使用kubectl查看hpa

运维平台应用弹性伸缩开发 go+gin+vue_vue_03


代码地址:https://github.com/dnsjia/luban

博客:https://www.ziji.work

运维平台应用弹性伸缩开发 go+gin+vue_go_04