多个副本访问 kubernetes 中的缓存
Multiple replicas accessing a cache in kubernetes
Prometheus 日志记录的一个统计数据是服务调用的持续时间,但我想测量同一服务的多次调用的时间。
所以我想创建一个字符串映射到 time.Time
type SomeService struct {
durations map[string]time.Time
}
在第一次进入时,当前时间被存储为该帐户 ID
durations[GetId()] = time.Now()
然后最后……在另一个调用中……存储了总时间。
startTime := c.durations[id]
duration, _ := c.durationStat.GetMetricWith(prometheus.Labels{"type": duration})
duration.Set(time.Now().Sub(startTime).Seconds())
delete(c.durations, id)
这在只有一个副本但在 Kubernetes 集群中出现故障时有效,对吧?下一个呼叫可能来自另一个端点?您如何在微服务中缓存值以便每个副本都可以访问它们?
终于找到了:
https://kubernetes.io/docs/concepts/services-networking/service
You can configure the service to have the same ip address always go to
the same pod by setting service.spec.sessionAffinity to “ClientIP” the
default is “None”
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
sessionAffinity: ClientIP
selector:
app: MyApp
这样您就可以安全地将简单值缓存在内存中!
Prometheus 日志记录的一个统计数据是服务调用的持续时间,但我想测量同一服务的多次调用的时间。
所以我想创建一个字符串映射到 time.Time
type SomeService struct {
durations map[string]time.Time
}
在第一次进入时,当前时间被存储为该帐户 ID
durations[GetId()] = time.Now()
然后最后……在另一个调用中……存储了总时间。
startTime := c.durations[id]
duration, _ := c.durationStat.GetMetricWith(prometheus.Labels{"type": duration})
duration.Set(time.Now().Sub(startTime).Seconds())
delete(c.durations, id)
这在只有一个副本但在 Kubernetes 集群中出现故障时有效,对吧?下一个呼叫可能来自另一个端点?您如何在微服务中缓存值以便每个副本都可以访问它们?
终于找到了:
https://kubernetes.io/docs/concepts/services-networking/service
You can configure the service to have the same ip address always go to the same pod by setting service.spec.sessionAffinity to “ClientIP” the default is “None”
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
sessionAffinity: ClientIP
selector:
app: MyApp
这样您就可以安全地将简单值缓存在内存中!