更详细得说:Pod中的Container中运行的应用,是如何读取到存储在ConfigMap中的相关配置的

image.png
猜测:
1.Pod获取到ConfigMap

2.ConfigMap被volumeMounts到容器内部(待验证)

ConfigMap由kubelet中的configManager负责管理,对应代码位于pkg/kubelet/config/configmap_manager

  1. // Manager interface provides methods for Kubelet to manage ConfigMap.
  2. type Manager interface {
  3. // Get configmap by configmap namespace and name.
  4. GetConfigMap(namespace, name string) (*v1.ConfigMap, error)
  5. // WARNING: Register/UnregisterPod functions should be efficient,
  6. // i.e. should not block on network operations.
  7. // RegisterPod registers all configmaps from a given pod.
  8. RegisterPod(pod *v1.Pod)
  9. // UnregisterPod unregisters configmaps from a given pod that are not
  10. // used by any other registered pod.
  11. UnregisterPod(pod *v1.Pod)
  12. }

从接口的定义上来看,configManager主要负责三件事

  • 提供configMap | GetConfigMap
  • 注册Pod | RegisterPod
  • 注销Pod | UnregisterPod

image.png
围绕第一个接口方法GetConfigMap,有几个问题:

  1. configMapManager如何提供ConfigMap
  2. ConfigMap提供给了谁

image.png

1.configManager是如何获取到configMap的

configManager有三个同门师兄弟,他们都实现了上面Manager接口所定义的方法,但是实现方法各不相同。
image.png
其中fakeManager是用于测试的,我们主要看configMapManager和simpleConfigMapManager

simpleConfigMapManager的实现非常简单,它只实现了一个GetConfigMap方法:直接通过kubeClient向apiserver要configMap

// simpleConfigMapManager implements ConfigMap Manager interface with
// simple operations to apiserver.
type simpleConfigMapManager struct {
    kubeClient clientset.Interface
}

// NewSimpleConfigMapManager creates a new ConfigMapManager instance.
func NewSimpleConfigMapManager(kubeClient clientset.Interface) Manager {
    return &simpleConfigMapManager{kubeClient: kubeClient}
}

func (s *simpleConfigMapManager) GetConfigMap(namespace, name string) (*v1.ConfigMap, error) {
    return s.kubeClient.CoreV1().ConfigMaps(namespace).Get(context.TODO(), name, metav1.GetOptions{})
}

func (s *simpleConfigMapManager) RegisterPod(pod *v1.Pod) {
}

func (s *simpleConfigMapManager) UnregisterPod(pod *v1.Pod) {
}

image.png

configMapManager则提供了缓存的能力。
其细分为

  • TTL Based Manager
  • Watch Manager

TTL Based Manager

当pod启动或者更新时,pod 引用的缓存中的configmap都会被无效化。获取configmap时(GetObject()),先尝试从TTL( Time To Live)缓存中获取,如果没有,过期或者无效,将会从api-server获取,获取的内容更新到缓存中,替换原来的内容。
CacheBasedManager 的定义在 pkg/kubelet/util/manager/cache_based_manager.go 中。

func NewCacheBasedManager(objectStore Store, getReferencedObjects func(*v1.Pod) sets.String) Manager {
    return &cacheBasedManager{
        objectStore:          objectStore,
        getReferencedObjects: getReferencedObjects,
        registeredPods:       make(map[objectKey]*v1.Pod),
    }
}

Watch Manager

每当pod启动或更新时,kubelet会对该 pod 新引用的所有configmap对象启动监控(watches),watch负责利用新的configmap对缓存的configmap更新或替换。

func NewWatchBasedManager(
    listObject listObjectFunc,
    watchObject watchObjectFunc,
    newObject newObjectFunc,
    isImmutable isImmutableFunc,
    groupResource schema.GroupResource,
    resyncInterval time.Duration,
    getReferencedObjects func(*v1.Pod) sets.String) Manager {

    // If a configmap/secret is used as a volume, the volumeManager will visit the objectCacheItem every resyncInterval cycle,
    // We just want to stop the objectCacheItem referenced by environment variables,
    // So, maxIdleTime is set to an integer multiple of resyncInterval,
    // We currently set it to 5 times.
    maxIdleTime := resyncInterval * 5

    objectStore := NewObjectCache(listObject, watchObject, newObject, isImmutable, groupResource, clock.RealClock{}, maxIdleTime)
    return NewCacheBasedManager(objectStore, getReferencedObjects)
}

小结:
k8s#ConfigMap是如何被Pod读取到的 - 图6
有关TTL_manager 和Watch_manager的实现(#Todo)

热更新原理
https://github.com/QingyaFan/container-cloud/issues/2

https://www.jianshu.com/p/04728c141e89

Kubernetes ConfigMap挂载导致容器目录覆盖的问题解决

https://blog.csdn.net/qq_34556414/article/details/115124529

Kubernetes(六)–ConfigMaps是如何工作的?

https://www.backendsite.com/index.php/309/

https://blog.51cto.com/u_13760351/2833058

JIMMYSONG(这个不错)

ConfigMap

https://jimmysong.io/kubernetes-handbook/concepts/configmap-hot-update.html

ConfigMap的热更新

https://jimmysong.io/kubernetes-handbook/concepts/configmap-hot-update.html
image.png