Kubernetes ConfigMap热更新测试 – 探究ConfigMap的创建和更新流程

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: ConfigMap热更新测试 ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中,下文主要是探究 ConfigMap 的创建和更新流程,以及对 ConfigMap 更新后容器内挂载的内容是否同步更新的测试。

ConfigMap热更新测试

ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中,下文主要是探究 ConfigMap 的创建和更新流程,以及对 ConfigMap 更新后容器内挂载的内容是否同步更新的测试。

测试示例


假设我们在 default namespace 下有一个名为 nginx-config 的 ConfigMap,可以使用 kubectl命令来获取:

$ kubectl get configmap nginx-config
NAME DATA AGE
nginx-config 1 99d

获取该ConfigMap的内容。

kubectl get configmap nginx-config -o yaml
apiVersion: v1
data:
 nginx.conf: |-
 worker_processes 1;

 events { worker_connections 1024; }

 http {
 sendfile on;

 server {
 listen 80; # a test endpoint that returns http 200s
 location / {
 proxy_pass http://httpstat.us/200;
 proxy_set_header X-Real-IP $remote_addr; } }

 server {

 listen 80;
 server_name api.hello.world;

 location / {
 proxy_pass http://l5d.default.svc.cluster.local;
 proxy_set_header Host $host;
 proxy_set_header Connection "";
 proxy_http_version 1.1;

 more_clear_input_headers 'l5d-ctx-*' 'l5d-dtab' 'l5d-sample'; } }

 server {

 listen 80;
 server_name www.hello.world;

 location / { # allow 'employees' to perform dtab overrides if ($cookie_special_employee_cookie != "letmein") {
 more_clear_input_headers 'l5d-ctx-*' 'l5d-dtab' 'l5d-sample'; } # add a dtab override to get people to our beta, world-v2 set $xheader ""; if ($cookie_special_employee_cookie ~* "dogfood") { set $xheader "/host/world => /srv/world-v2;"; }

 proxy_set_header 'l5d-dtab' $xheader;


 proxy_pass http://l5d.default.svc.cluster.local;
 proxy_set_header Host $host;
 proxy_set_header Connection "";
 proxy_http_version 1.1; } } }
kind: ConfigMap
metadata:
 creationTimestamp: 2017-08-01T06:53:17Z
 name: nginx-config
 namespace: default
 resourceVersion: "14925806"
 selfLink: /api/v1/namespaces/default/configmaps/nginx-config
 uid: 18d70527-7686-11e7-bfbd-8af1e3a7c5bd

ConfigMap中的内容是存储到etcd中的,然后查询etcd:

ETCDCTL_API=3 etcdctl get /registry/configmaps/default/nginx-config
/registry/configmaps/default/nginx-config

注意使用 v3 版本的 etcdctl API,下面是输出结果:

k8s

v1	ConfigMap

T

nginx-configdefault"*$18d70527-7686-11e7-bfbd-8af1e3a7c5bd28B
 �ʀ����xz�


nginx.conf�
 worker_processes 1;

events { worker_connections 1024; }

http {
 sendfile on;

 server {
 listen 80;

 # a test endpoint that returns http 200s
 location / {
 proxy_pass http://httpstat.us/200;
 proxy_set_header X-Real-IP $remote_addr;
 }
 }

 server {

 listen 80;
 server_name api.hello.world;

 location / {
 proxy_pass http://l5d.default.svc.cluster.local;
 proxy_set_header Host $host;
 proxy_set_header Connection "";
 proxy_http_version 1.1;

 more_clear_input_headers 'l5d-ctx-*' 'l5d-dtab' 'l5d-sample';
 }
 }

 server {

 listen 80;
 server_name www.hello.world;

 location / {


 # allow 'employees' to perform dtab overrides
 if ($cookie_special_employee_cookie != "letmein") {
 more_clear_input_headers 'l5d-ctx-*' 'l5d-dtab' 'l5d-sample';
 }

 # add a dtab override to get people to our beta, world-v2
 set $xheader "";

 if ($cookie_special_employee_cookie ~* "dogfood") {
 set $xheader "/host/world => /srv/world-v2;";
 }

 proxy_set_header 'l5d-dtab' $xheader;


 proxy_pass http://l5d.default.svc.cluster.local;
 proxy_set_header Host $host;
 proxy_set_header Connection "";
 proxy_http_version 1.1;
 }
 }
}"

输出中在 nginx.conf 配置文件的基础中增加了文件头内容,是kubernetes增加的。

代码

ConfigMap 结构体的定义:

// ConfigMap holds configuration data for pods to consume.
type ConfigMap struct {
	metav1.TypeMeta `json:",inline"` // Standard object's metadata. // More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata // +optional
	metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` // Data contains the configuration data. // Each key must be a valid DNS_SUBDOMAIN with an optional leading dot. // +optional Data map[string]string `json:"data,omitempty" protobuf:"bytes,2,rep,name=data"` }

在 staging/src/k8s.io/client-go/kubernetes/typed/core/v1/configmap.go 中ConfigMap 的接口定义:

// ConfigMapInterface has methods to work with ConfigMap resources.
type ConfigMapInterface interface { Create(*v1.ConfigMap) (*v1.ConfigMap, error) Update(*v1.ConfigMap) (*v1.ConfigMap, error) Delete(name string, options *meta_v1.DeleteOptions) error
	DeleteCollection(options *meta_v1.DeleteOptions, listOptions meta_v1.ListOptions) error
	Get(name string, options meta_v1.GetOptions) (*v1.ConfigMap, error) List(opts meta_v1.ListOptions) (*v1.ConfigMapList, error) Watch(opts meta_v1.ListOptions) (watch.Interface, error) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1.ConfigMap, err error) ConfigMapExpansion }

在 staging/src/k8s.io/client-go/kubernetes/typed/core/v1/configmap.go 中创建 ConfigMap 的方法如下:

// Create takes the representation of a configMap and creates it. Returns the server's representation of the configMap, and an error, if there is any.
func (c *configMaps) Create(configMap *v1.ConfigMap) (result *v1.ConfigMap, err error) {
	result = &v1.ConfigMap{}
	err = c.client.Post(). Namespace(c.ns). Resource("configmaps"). Body(configMap). Do(). Into(result) return }

通过 RESTful 请求在 etcd 中存储 ConfigMap 的配置,该方法中设置了资源对象的 namespace 和 HTTP 请求中的 body,执行后将请求结果保存到 result 中返回给调用者。

注意 Body 的结构

// Body makes the request use obj as the body. Optional. // If obj is a string, try to read a file of that name. // If obj is a []byte, send it directly. // If obj is an io.Reader, use it directly. // If obj is a runtime.Object, marshal it correctly, and set Content-Type header. // If obj is a runtime.Object and nil, do nothing. // Otherwise, set an error. 

创建 ConfigMap RESTful 请求中的的 Body 中包含 ObjectMeta 和 namespace。

HTTP 请求中的结构体:

// Request allows for building up a request to a server in a chained fashion. // Any errors are stored until the end of your call, so you only have to // check once.
type Request struct { // required
	client HTTPClient
	verb string

	baseURL *url.URL
	content ContentConfig
	serializers Serializers // generic components accessible via method setters
	pathPrefix string
	subpath string params url.Values
	headers http.Header // structural elements of the request that are part of the Kubernetes API conventions namespace string
	namespaceSet bool
	resource string
	resourceName string
	subresource string
	timeout time.Duration // output
	err error
	body io.Reader // This is only used for per-request timeouts, deadlines, and cancellations.
	ctx context.Context

	backoffMgr BackoffManager
	throttle flowcontrol.RateLimiter }

测试

分别测试使用 ConfigMap 挂载 Env 和 Volume 的情况。

更新使用ConfigMap挂载的Env

使用下面的配置创建 nginx 容器测试更新 ConfigMap 后容器内的环境变量是否也跟着更新。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: my-nginx
spec:
 replicas: 1 template:
 metadata:
 labels:
 run: my-nginx
 spec:
 containers: - name: my-nginx
 image: sz-pg-oam-docker-hub-001.tendcloud.com/library/nginx:1.9
 ports: - containerPort: 80
 envFrom: - configMapRef:
 name: env-config
---
apiVersion: v1
kind: ConfigMap
metadata:
 name: env-config
 namespace: default
data:
 log_level: INFO

获取环境变量的值

$ kubectl exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` env|grep log_level
log_level=INFO

修改 ConfigMap

$ kubectl edit configmap env-config

修改 log_level 的值为 DEBUG。

再次查看环境变量的值。

$ kubectl exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` env|grep log_level
log_level=INFO

实践证明修改 ConfigMap 无法更新容器中已注入的环境变量信息。

更新使用ConfigMap挂载的Volume

使用下面的配置创建 nginx 容器测试更新 ConfigMap 后容器内挂载的文件是否也跟着更新。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: my-nginx
spec:
 replicas: 1 template:
 metadata:
 labels:
 run: my-nginx
 spec:
 containers: - name: my-nginx
 image: sz-pg-oam-docker-hub-001.tendcloud.com/library/nginx:1.9
 ports: - containerPort: 80
 volumeMounts: - name: config-volume
 mountPath: /etc/config
 volumes: - name: config-volume
 configMap:
 name: special-config
---
apiVersion: v1
kind: ConfigMap
metadata:
 name: special-config
 namespace: default
data:
 log_level: INFO
$ kubectl exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` cat /tmp/log_level
INFO

修改 ConfigMap

$ kubectl edit configmap special-config

修改 log_level 的值为 DEBUG。

等待大概10秒钟时间,再次查看环境变量的值。

$ kubectl exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` cat /tmp/log_level
DEBUG

我们可以看到使用 ConfigMap 方式挂载的 Volume 的文件中的内容已经变成了 DEBUG。

总结

更新 ConfigMap 后:

  • 使用该 ConfigMap 挂载的 Env 不会同步更新
  • 使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(实测大概10秒)才能同步更新

ENV 是在容器启动的时候注入的,启动之后 kubernetes 就不会再改变环境变量的值,且同一个 namespace 中的 pod 的环境变量是不断累加的,参考 Kubernetes中的服务发现与docker容器间的环境变量传递源码探究。为了更新容器中使用 ConfigMap 挂载的配置,可以通过滚动更新 pod 的方式来强制重新挂载 ConfigMap,也可以在更新了 ConfigMap 后,先将副本数设置为 0,然后再扩容。

本文转自kubernetes中文社区-Kubernetes ConfigMap热更新测试 – 探究ConfigMap的创建和更新流程

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
2月前
|
弹性计算 监控 测试技术
弹性计算的测试流程
弹性计算的测试流程
34 0
|
2月前
|
安全 测试技术 持续交付
接口自动化测试的基本流程
接口自动化测试的基本流程
119 1
|
2月前
|
运维 Kubernetes 测试技术
容器技术:优化软件测试流程的利器
本文介绍了容器技术的概念、优势和历史发展,对比了容器与虚拟机的区别,并提及了Docker和Kubernetes等常见容器技术。容器作为轻量级虚拟化工具,提供高效、灵活的应用部署方式,广泛应用于软件开发、云计算和微服务架构。随着技术演进,容器将在边缘计算、人工智能等领域发挥更大作用,推动行业变革。
45 3
|
2月前
|
安全 测试技术 网络架构
【专栏】编写网络设备割接方案的七个步骤,包括明确割接目标、收集信息、制定计划、设计流程、风险评估、准备测试环境和编写文档。
【4月更文挑战第28天】本文介绍了编写网络设备割接方案的七个步骤,包括明确割接目标、收集信息、制定计划、设计流程、风险评估、准备测试环境和编写文档。通过实际案例分析,展示了如何成功完成割接,确保业务连续性和稳定性。遵循这些步骤,可提高割接成功率,为公司的网络性能和安全提供保障。
|
18天前
|
监控 数据可视化 测试技术
性能测试:性能测试流程与方法
**性能测试流程与方法概述:** 本文介绍了性能测试的关键步骤,包括现状分析、指标获取、用户场景定义、验收标准设定、测试计划编写、压力环境准备、执行压测、监控、结果分析、报告编写及改进建议。测试方法涉及并发模式(虚拟用户)和RPS模式(吞吐量),确保系统在不同负载下的稳定性和效率。
18 0
|
18天前
|
安全 测试技术 网络安全
API渗透测试的基本流程
【7月更文挑战第9天】API渗透测试类似Web应用测试,涉及资产分析和模拟攻击,以发现安全缺陷。
|
2月前
|
安全 测试技术 网络安全
API渗透测试的基本流程及关键点
【5月更文挑战第26天】API渗透测试类似Web应用渗透测试,涉及资产分析和模拟攻击,以发现安全缺陷。
|
1月前
|
测试技术
软件测试项目式学习三(软件测试原则与基本流程与实际测试用例)
软件测试项目式学习三(软件测试原则与基本流程与实际测试用例)
35 0
|
2月前
|
安全 数据管理 测试技术
网络安全与信息安全:防范漏洞、加强加密与提升安全意识深入探索自动化测试框架的设计原则与实践应用化测试解决方案。文章不仅涵盖了框架选择的标准,还详细阐述了如何根据项目需求定制测试流程,以及如何利用持续集成工具实现测试的自动触发和结果反馈。最后,文中还将讨论测试数据管理、测试用例优化及团队协作等关键问题,为读者提供全面的自动化测试框架设计与实施指南。
【5月更文挑战第27天】 在数字化时代,网络安全与信息安全已成为维护国家安全、企业利益和个人隐私的重要环节。本文旨在分享关于网络安全漏洞的识别与防范、加密技术的应用以及提升安全意识的重要性。通过对这些方面的深入探讨,我们希望能为读者提供一些实用的建议和策略,以应对日益严峻的网络安全挑战。 【5月更文挑战第27天】 在软件开发周期中,自动化测试作为保障软件质量的关键步骤,其重要性日益凸显。本文旨在剖析自动化测试框架设计的核心原则,并结合具体案例探讨其在实际应用中的执行策略。通过对比分析不同测试框架的优缺点,我们提出一套高效、可扩展且易于维护的自动
|
2月前
|
移动开发 前端开发
基于若依的ruoyi-nbcio流程管理系统一种简单的动态表单模拟测试实现(五)
基于若依的ruoyi-nbcio流程管理系统一种简单的动态表单模拟测试实现(五)
28 0