一个关于k8s configmap的小总结~~~
configmap是k8s的一个重要的组件,顾名思义,configmap是一个关于配置的集合组件,事实上,有点像基于kv对的形式进行存储和使用的。
1、一个configmap的例子
1 2 3 4 5 6 7 8 9 10 11
| apiVersion: v1 kind: ConfigMap metadata: name: prometheus-config namespace: kube-system labels: kubernetes.io/cluster-service: "true" addonmanager.kubernetes.io/mode: EnsureExists data: prometheus.yml: | .......
|
以上是prometheus的configmap配置文件,与prometheus.yml 对应的就是该yml文件内的所有内容,当这个configmap创建之后,configmap会生成一个虚拟的文件夹,由别的container来挂载configmap中prometheus.yml这个配置文件。
2、configmap的创建
configmap创建拥有多种形式,可以通过命令行,文件、文件夹、或者直接通过k8s client的api进行创建。
kubectl create -f configmap-demo.yml:这种方式会把上面的yml文件创建为configmap
kubectl create configmap <name> --from-file=prometheus.yml: 这种方式会把prometheus.yml作为configmap的data进行创建
kubectl create configmap my -config --from-file=/path/to/dir:当采用这种方式时,k8s会寻找这个文件夹下面所有符合规范的文件,把所有文件作为configmap的data来创建configmap
- 还有一种根据api来修改configmap的方法,最后说
3.configmap的使用
configmap有三种使用的方式:
- 环境变量
- 使用configmap将条目暴露为文件
- 挂载configmap的特定文件
3.1 环境变量形式 valueFrom
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVers ion: vl kind: Pod metadata: name: xx spec: containers: - image: xx env: - name: ENV_NAME valueFrom: configMapKeyRef: name: <configmap name> key: <configmap data key>
|
3.2 使用configmap将条目暴露为文件
configmap 会把符合条件的配置生成为虚拟目录,生成到pod指定的目录下,如下文就是在/etc/config/xx目录下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| apiVers ion: vl kind: Pod metadata: name: xx spec: containers: - image: xx volumeMounts: - name: config mountPath: /etc/config/xx readOnly:true volumes: - name: config configMap: name: <configmap name>
|
3.3 使用configmap的特定文件
1 2 3 4 5 6 7 8 9 10 11
| apiVers ion: vl kind: Pod metadata: name: xx spec: containers: - image: some/image volumeMounts: - name: myvolume mountPath: /etc/someconfig.conf # 要挂载的目标文件 subPath: myconfig.conf # configmap 内的文件名
|
4.configmap 与prometheus 结合
贴一个k8s 官方提供的prometheus 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| apiVersion: apps/v1 kind: StatefulSet metadata: name: prometheus namespace: kube-system labels: k8s-app: prometheus kubernetes.io/cluster-service: "true" addonmanager.kubernetes.io/mode: Reconcile version: v2.8.1 spec: serviceName: "prometheus" replicas: 1 podManagementPolicy: "Parallel" updateStrategy: type: "RollingUpdate" selector: matchLabels: k8s-app: prometheus template: metadata: labels: k8s-app: prometheus annotations: scheduler.alpha.kubernetes.io/critical-pod: '' spec: priorityClassName: system-cluster-critical serviceAccountName: prometheus initContainers: - name: "init-chown-data" image: "busybox:latest" imagePullPolicy: "IfNotPresent" command: ["chown", "-R", "65534:65534", "/data"] volumeMounts: - name: prometheus-data mountPath: /data subPath: "" containers: - name: prometheus-server-configmap-reload image: "jimmidyson/configmap-reload:v0.1" imagePullPolicy: "IfNotPresent" args: - --volume-dir=/etc/config - --webhook-url=http://localhost:9090/-/reload volumeMounts: - name: config-volume mountPath: /etc/config readOnly: true resources: limits: cpu: 10m memory: 10Mi requests: cpu: 10m memory: 10Mi - name: prometheus-server image: "prom/prometheus:v2.8.1" imagePullPolicy: "IfNotPresent" args: - --config.file=/etc/config/prometheus.yml - --storage.tsdb.path=/data - --web.console.libraries=/etc/prometheus/console_libraries - --web.console.templates=/etc/prometheus/consoles - --web.enable-lifecycle ports: - containerPort: 9090 readinessProbe: httpGet: path: /-/ready port: 9090 initialDelaySeconds: 30 timeoutSeconds: 30 livenessProbe: httpGet: path: /-/healthy port: 9090 initialDelaySeconds: 30 timeoutSeconds: 30 # based on 10 running nodes with 30 pods each resources: limits: cpu: 200m memory: 1000Mi requests: cpu: 200m memory: 1000Mi volumeMounts: - name: config-volume mountPath: /etc/config - name: prometheus-data mountPath: /data subPath: "" terminationGracePeriodSeconds: 300 volumes: - name: config-volume configMap: name: prometheus-config volumeClaimTemplates: - metadata: name: prometheus-data spec: storageClassName: slow accessModes: - ReadWriteOnce resources: requests: storage: "16Gi"
|
在上面的配置中,k8s官方利用了prometheus的热加载特性,内置了一个jimmidyson/configmap-reload:v0.1的container,它的作用就是在configmap发生变化时通过配置好的url来通知prometheus进行热加载。所以下文我们需要讲讲怎么通过k8s client 来进行configmap的更新。
5.k8s client java版本
k8s 官方提供了很多不同语言、不同种类的k8s客户端
k8s 官网client列表
在这里选择的是fabric8io,一个对于Java友好的开源微服务管理平台。
- 引入依赖: 由于节点的k8s版本是1.13+,所以要使用最新版本
1 2 3 4 5
| <dependency> <groupId>io.fabric8</groupId>x` <artifactId>kubernetes-client</artifactId> <version>4.2.2</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11
| Config config = new ConfigBuilder().withMasterUrl(MASTER_URL) .withTrustCerts(true) .withCaCertFile("/ca.crt") // 在这里的参数都是文件路径 .withClientKeyFile("/kubecfg.key") // 在这里的参数都是文件路径 .withClientCertFile("/kubecfg.crt") // 在这里的参数都是文件路径 .removeFromTlsVersions(TlsVersion.TLS_1_0) .removeFromTlsVersions(TlsVersion.TLS_1_1) .removeFromTlsVersions(TlsVersion.TLS_1_2) .withRequestTimeout(REQUEST_TIMEOUT) .withConnectionTimeout(CONNECTION_TIMEOUT) .build();
|
在这里需要强调,如果你的k8s集群需要证书认证,则在创建config实例时需要生成双向认证证书,并把证书放入指定目录,如果不需要证书的话,则可以去掉大部分配置。
1 2 3 4 5 6 7
| # 生成client crt grep 'client-certificate-data' ~/.kube/config | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.crt
# 生成client key grep 'client-key-data' ~/.kube/config | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.key
|
执行上述两条命令后,把生成的kubecfg.crt 与 kubecfg.key 保存到指定目录下即可。
1 2 3 4 5 6
| KubernetesClient client = new DefaultKubernetesClient(config); Resource<ConfigMap, DoneableConfigMap> configMapResource.client.configMaps().inNamespace(namespace). withName(name); ConfigMap configMap = configMapResource.get(); Map<String, String> data = configMap.getData();
|
在这里data就是configmap的data属性,内部就是文件名-文件内容的kv对,所有对configmap的修改都是在configmapresource内进行的。