Cloud
K8s API 核心对象 —— client-go
· ☕ 3 分钟

API 入口

Client Sets

接收变更通知和缓存(Informers and Caching)

Client Sets可以 watch 变更,但一般我们用更高级的 Informers,因为它有缓存、索引等功能。

image-20210326154940269

  • Lister :被应用调用,返回缓存中的数据列表
  • Informer:监听器

Informer 有两个功能


Helm base
· ☕ 1 分钟

Concept

umbrella chart

you can also create a chart with dependencies to other charts (a.k.a. umbrella chart) which are completely external using the requirements.yaml file.

image-20210325143339901

versioning

Simple 1-1 versioning

Synced versions in Helm

Chart versus application versioning

Independent Helm versioning

参考

https://codefresh.io/docs/docs/new-helm/helm-best-practices/


Kubernetes extends concept
· ☕ 3 分钟

前言

最近由于工作需要,重新系统回顾 Kubernetes 的编程。发现《Programming Kubernetes》这书写得比较系统。于是边学,边记录一些重点。

Controller

Controller Loop

image-20210325162149792


Kubernetes 自动扩缩容
· ☕ 1 分钟

本文状态:草稿

image-20210323153341945

配置例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

算法

desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]

currentMetricValue 为相关 pod 的 metric 平均数。


Istio Canary(金丝雀) 上线
· ☕ 1 分钟

按比例分配分配新旧版本流量

VirtualService:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 75
    - destination:
        host: reviews
        subset: v2
      weight: 25

DestinationRule :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: bookinfo-ratings
spec:
  host: reviews
  trafficPolicy:
    loadBalancer:
      simple: LEAST_CONN
  subsets:
  - name: v1
    labels:
      version: v1
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN
  - name: v2
    labels:
      version: v2
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN

根据请求源 pod 的 label 路由

使用 sourceLabels 规则,可以根据源 pod 的 label 进行路由。这里用了 version 这个 label。即根据pod的应用版本进行路由。
这样的路由规则实际是使用于发起调用方的 sidecar。