美文网首页
k8s大集群list问题记录

k8s大集群list问题记录

作者: wwq2020 | 来源:发表于2023-03-13 10:54 被阅读0次

简单总结

当我们用进行list请求时,如果是以下情况,则list请求会直接访问到etcd而不是经过apiserver的cache,所以会对etcd的压力会比较大
1 list请求没有设置resourceVersion
2 apiserver开启APIListChunking特性(默认已开启)并且list请求设置了Continue
3 apiserver开启APIListChunking特性(默认已开启)并且list请求设置了Limit且resourceVersion不等于'0'
4 list请求设置了match并且不等于NotOlderThan

就算是我们经过apiserver的cache,但是当预期list的资源在k8s内存在大量时候,需要进行大量的过滤,对apiserver的压力会比较大

相关代码

staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go中

func (e *Store) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
    label := labels.Everything()
    ...
    out, err := e.ListPredicate(ctx, e.PredicateFunc(label, field), options)
    ...
    return out, nil
}

func (e *Store) ListPredicate(ctx context.Context, p storage.SelectionPredicate, options *metainternalversion.ListOptions) (runtime.Object, error) {
    ...
    err := e.Storage.GetList(ctx, e.KeyRootFunc(ctx), storageOpts, list)
    ...
}

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go中(try get from etcd)

func (c *Cacher) GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
    ...
    if shouldDelegateList(opts) {
        return c.storage.GetList(ctx, key, opts, listObj)
    }
    ...
    objs, readResourceVersion, indexUsed, err := c.listItems(ctx, listRV, key, pred, recursive)
    if err != nil {
        return err
    }
    span.AddEvent("Listed items from cache", attribute.Int("count", len(objs)))
    if len(objs) > listVal.Cap() && pred.Label.Empty() && pred.Field.Empty() {
        // Resize the slice appropriately, since we already know that none
        // of the elements will be filtered out.
        listVal.Set(reflect.MakeSlice(reflect.SliceOf(c.objectType.Elem()), 0, len(objs)))
        span.AddEvent("Resized result")
    }
    for _, obj := range objs {
        elem, ok := obj.(*storeElement)
        if !ok {
            return fmt.Errorf("non *storeElement returned from storage: %v", obj)
        }
        if filter(elem.Key, elem.Labels, elem.Fields) {
            listVal.Set(reflect.Append(listVal, reflect.ValueOf(elem.Object).Elem()))
        }
    }
    ...
}

func shouldDelegateList(opts storage.ListOptions) bool {
    resourceVersion := opts.ResourceVersion
    pred := opts.Predicate
    match := opts.ResourceVersionMatch
    pagingEnabled := utilfeature.DefaultFeatureGate.Enabled(features.APIListChunking)
    hasContinuation := pagingEnabled && len(pred.Continue) > 0
    hasLimit := pagingEnabled && pred.Limit > 0 && resourceVersion != "0"
    unsupportedMatch := match != "" && match != metav1.ResourceVersionMatchNotOlderThan

    // If resourceVersion is not specified, serve it from underlying
    // storage (for backward compatibility). If a continuation is
    // requested, serve it from the underlying storage as well.
    // Limits are only sent to storage when resourceVersion is non-zero
    // since the watch cache isn't able to perform continuations, and
    // limits are ignored when resource version is zero
    return resourceVersion == "" || hasContinuation || hasLimit || unsupportedMatch
}

staging/src/k8s.io/apiserver/pkg/storage/etcd3/store.go(get from etcd)

func (s *store) GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
    ...
}

相关文章

  • k8s大集群list问题记录

    简单总结 当我们用进行list请求时,如果是以下情况,则list请求会直接访问到etcd而不是经过apiserve...

  • 一文学会 K8S故障处理

    1 集群故障概述 在k8s集群的部署过程中,大家可能会遇到很多问题。这也是本地部署k8s集群遇到的最大挑战,因此本...

  • 【containerd】RunPodSandbox for XX

    问题背景 工业云部署使用k8s集群 k8s: 01746170708faa599113b027772802bcbb...

  • k8s专题目录

    初阶k8s集群搭建 高阶k8s HA 集群搭建(一) 高阶k8s HA 集群搭建(二) docker镜像私有仓库搭...

  • k8s集群变更 clusterDomain

    k8s集群变更 clusterDomain变更部分变更k8s集群 clusterDomain 所有node节点: ...

  • DockerSwarm与K8S由于Firewalld引起的网络问

    问题表现 K8S集群中的问题 需求在集群中挂载一个分布式文件存储系统,由于集群本身是rancher,理所当然选择了...

  • k8s-访问外网服务的两种方式

    需求 k8s集群内的pod需要访问mysql,由于mysql的性质,不适合部署在k8s集群内,故k8s集群内的应用...

  • K8S 基本环境构建

    本文档主要记录在 UCloud 上搭建K8S集群的过程,遇到的问题,以及为什么这么做的原因。 为什么选择 UClo...

  • k8s The connection to the server

    前言 最近公司的 k8s 集群出现了一个问题:在执行任何 kubectl 命令时都会出现以下错误,本文就记录一下该...

  • k8s源码部署01

    k8s源码集群搭建 集群搭建: 1、生产环境k8s平台规划 老男孩老师文档 : https://www.cnblo...

网友评论

      本文标题:k8s大集群list问题记录

      本文链接:https://www.haomeiwen.com/subject/cylcrdtx.html