Back
Featured image of post CLB涨价之后的省钱之道

CLB涨价之后的省钱之道

行动背景:原本使用TKE部署的博客,接入层使用CLB进入到集群,后面涨价后每小时0.2元,一个月就是144RMB,这价格使开销陡增,故要寻找替代方案。最终这个方案只为流量付费,而流量费用便宜很多。

技术背景

目前还没在博客中介绍后台是如何搭建的,这里简单描述一下。整体是完全云原生方式,基于TKE(k8s)提供更好的服务声明和调度能力,基于TCM(istio)提供了集群统一的接入和证书(TLS)以及网关能力,然后集群内部部署了一些博客相关服务。简单图示如下:

当前外部访问都是通过istio-ingress-gateway绑定到CLB,需要持续为CLB付费。

新方案

基于腾讯云EIP绑定Pod方式,目标是放弃CLB,不让istio-ingress-gateway直接绑定到CLB,而让istio-ingress-gateway这个pod分配弹性公网IP(后称:eip)来实现。

集群基本设置

参考上面文档,要求集群开启VPC-CNI模式。原本我的TKE集群没有开启VPC-CNI,在TKE「基本信息」中开启就不多说了。然后还有相关权限设置照上面文档操作。

魔改istio-ingress-gatway

因为直接使用了腾讯云的TCM(托管Istio),默认ingress是deployment的,需要修改为EIP可用的StatefulSet(sts)。建议先把当前的ingress导出来:

1
 k get deploy istio-ingressgateway -o yaml > ingress.yaml

然后编辑为sts及相关配置,大概像这样(关键点见注释,不要照搬):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
apiVersion: apps/v1
# 1. 将Deployment修改为StatefulSet
kind: StatefulSet
metadata:
  # 原来的labels保持不变
  labels:
    app: istio-ingressgateway
    install.operator.istio.io/owning-resource: tcm
    istio: ingressgateway
    istio.io/rev: 1-10-3
    operator.istio.io/component: IngressGateways
    release: istio
  name: istio-ingressgateway
  namespace: istio-system
spec:
  replicas: 1
  selector:
    # selector也不变
    matchLabels:
      app: istio-ingressgateway
      istio: ingressgateway
  # 2. 添加serviceName
  serviceName: istio-ingressgateway
  template:
    metadata:
      annotations:
        # 3. 添加如下几个tke的annotation
        tke.cloud.tencent.com/networks: "tke-route-eni"
        tke.cloud.tencent.com/vpc-ip-claim-delete-policy: Never
        tke.cloud.tencent.com/eip-attributes: '{"Bandwidth":"100","ISP":"BGP"}'
        tke.cloud.tencent.com/eip-claim-delete-policy: "Never"
        prometheus.io/path: /stats/prometheus
        prometheus.io/port: "15020"
        prometheus.io/scrape: "true"
        sidecar.istio.io/discoveryAddress: istiod-1-10-3.istio-system.svc:15012
        sidecar.istio.io/inject: "false"
      creationTimestamp: null
      labels:
        app: istio-ingressgateway
        chart: gateways
        heritage: Tiller
        install.operator.istio.io/owning-resource: tcm
        istio: ingressgateway
        istio.io/rev: 1-10-3
        operator.istio.io/component: IngressGateways
        release: istio
        service.istio.io/canonical-name: istio-ingressgateway
        service.istio.io/canonical-revision: latest
    # 后面东西基本没动过,此处省略

改完后再apply到集群即可。

1
 k apply -f ingress.yaml

移除CLB

现在CLB还在继续使用,因为新方案pod直接绑定了eip,所以这个Service已经没用了,直接删除对应的Service即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 查看当前service
❯ k get svc
NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP    PORT(S)                       AGE
istio-crd              ClusterIP      172.16.255.160   <none>         61011/TCP                     6d15h
istio-ingressgateway   LoadBalancer   172.16.252.183   [原CLB地址]   443:30400/TCP                 6d15h
istiod-1-10-3          ClusterIP      172.16.252.80    <none>         15012/TCP,443/TCP,15014/TCP   6d15h
zipkin                 ClusterIP      172.16.253.136   <none>         9411/TCP                      6d15h

# 删除原本的svc/istio-ingressgateway
❯ k delete svc istio-ingressgateway
service "istio-ingressgateway" deleted

调整DNS解析

可以通过Pod的Event或者是查看对应的EIPC资源看到绑定关系。通过执行:

1
2
❯ k  get eipc -o yaml | grep publicIP
    publicIP: [你自动获得的EIP]

然后在你的DNS运营商里修改对应的解析记录即可。

尾音

本方案略有遗憾的是现在不支持绑定固定IP。下次重新调度IP会变化,咨询了腾讯云客服,未来不久会上线绑定到固定IP方案,到时此方案接近完美。

无论如何,恭喜你每周可以节省出一杯咖啡的钱:)