欢乐马一直被模仿从未被超越 發表於 2020-3-13 23:50:00

Kubernetes—配置管理ConfigMap

<h1 id="1-configmap介绍">1 ConfigMap介绍</h1>
<h2 id="11-概述">1.1 概述</h2>
<p>  在部署应用程序时,我们都会涉及到应用的配置,在容器中,如Docker容器中,如果将配置文件打入容器镜像,这种行为等同于写死配置,每次修改完配置,镜像就得重新构建。当然,我们也可以通过挂载包含该文件的卷进行配置管理和修改。而在k8s中,我们要讲一种更好的方式,即ConfigMap,这种资源对象的出现,更是极大的方便了应用程序的配置管理。<br>
  ConfigMap是一个或多个key/value的形式保存在k8s中,内部可以管理变量也可以管理完整的配置文件内容。</p>
<h2 id="12-用法">1.2 用法</h2>
<ol>
<li>生成容器内的环境变量,在pod中可以通过<code>spec.env</code>或者<code>spec.envFrom</code>进行引用。</li>
<li>设置容器启动命令的启动参数,前提是设置为环境变量。</li>
<li>以卷volume的方式挂载到容器内部的文件或目录,通过<code>spec.volumes</code>引用。</li>
</ol>
<h1 id="2-configmap用法">2 ConfigMap用法</h1>
<blockquote>
<p>在使用命令的时候注意单词: configmap等价于cm,cm算是简写,类似于deployment可以使用命令时写成deploy,service可以写成svc,namespace可以写成ns,pod可以写成po。</p>
</blockquote>
<h2 id="21-创建">2.1 创建</h2>
<p><strong>1)yaml文件方式创建</strong><br>
样例:</p>
<pre><code class="language-yaml">apiVersion: v1
kind: ConfigMap
metadata:
name: cm-test01
data:
appconf01: value01
appconf02: value02
</code></pre>
<p>命令:<br>
<code>$ kubectl create -f configmap-test01.yaml</code></p>
<p><strong>2)命令行方式创建</strong></p>
<ul>
<li>读取文件方式(也可以是目录)通过<code>--from-file</code>参数从文件中读取。可以指定key的名称,若不指定,则默认使用文件名为key。<br>
如当前目录有一个配置文件为test.properties</li>
</ul>
<pre><code class="language-properties">key01:value01
key02:value02
conf01: value03
</code></pre>
<p><code>$ kubectl create cm cm-test-file --from-file=test.properties</code></p>
<ul>
<li>指定参数方式,通过<code>--from-literal</code>指定keyxx=valuexx创建confimap中的data内配置属性。<br>
<code>$ kubectl create configmap cm-test-literal--from-literal=key01=value01 --from-literal=key02=value02</code></li>
</ul>
<h2 id="22-查询">2.2 查询</h2>
<p><strong>1)查看configmap列表</strong><br>
<code>$ kubectl get cm</code></p>
<pre><code class="language-shell">#kubectl get cm
NAME      DATA      AGE
cm-test-file      1         1m
cm-test-literal   2         2s
cm-test01         2         1h
</code></pre>
<p><strong>2)查看configmap详情</strong><br>
<code>$ kubectl describe cm cm-test01</code></p>
<pre><code class="language-shell">#kubectl describe cm cm-test01
Name:         cm-test01
Namespace:      system-pro
Labels:         &lt;none&gt;
Annotations:    &lt;none&gt;

Data
====
appconf01:
----
value01
appconf02:
----
value02

</code></pre>
<p><code>$ kubectl describe configmap cm-test-file</code></p>
<pre><code class="language-shell">#kubectl describe configmap cm-test-file
Name:         cm-test-file
Namespace:      system-pro
Labels:         &lt;none&gt;
Annotations:    &lt;none&gt;

Data
====
test.properties:
----
key01:value01
key02:value02
conf01: value03
</code></pre>
<p><code>$ kubectl describe cm cm-test-literal</code></p>
<pre><code class="language-shell">#kubectl describe cm cm-test-literal
Name:         cm-test-literal
Namespace:      system-pro
Labels:         &lt;none&gt;
Annotations:    &lt;none&gt;

Data
====
key01:
----
value01
key02:
----
value02
</code></pre>
<p><strong>3)查看yaml输出</strong><br>
<code>$ kubectl get cm cm-test01 -o yaml</code></p>
<pre><code class="language-shell">#kubectl get cm cm-test01 -o yaml
apiVersion: v1
data:
appconf01: value01
appconf02: value02
kind: ConfigMap
metadata:
creationTimestamp: 2020-03-13T13:06:21Z
name: cm-test01
namespace: system-pro
resourceVersion: "594861"
selfLink: /api/v1/namespaces/system-pro/configmaps/cm-test01
uid: 6f5e7efb-652b-11ea-adf9-fa163e4464a5
</code></pre>
<p><code>$ kubectl get configmap cm-test-file -o yaml</code></p>
<pre><code class="language-shell">#kubectl get configmap cm-test-file -o yaml
apiVersion: v1
data:
test.properties: |
    key01:value01
    key02:value02
    conf01: value03
kind: ConfigMap
metadata:
creationTimestamp: 2020-03-13T14:29:30Z
name: cm-test-file
namespace: system-pro
resourceVersion: "598548"
selfLink: /api/v1/namespaces/system-pro/configmaps/cm-test-file
uid: 0d226ad2-6537-11ea-adf9-fa163e4464a5
</code></pre>
<p><code>$ kubectl get cm cm-test-literal -o yaml</code></p>
<pre><code class="language-shell">#kubectl get cm cm-test-literal -o yaml
apiVersion: v1
data:
key01: value01
key02: value02
kind: ConfigMap
metadata:
creationTimestamp: 2020-03-13T14:30:57Z
name: cm-test-literal
namespace: system-pro
resourceVersion: "598613"
selfLink: /api/v1/namespaces/system-pro/configmaps/cm-test-literal
uid: 412affd4-6537-11ea-adf9-fa163e4464a5
</code></pre>
<h2 id="23-更新">2.3 更新</h2>
<p><strong>1)edit</strong><br>
<code>$ kubectl edit cm cm-test01</code><br>
<img src="https://img-blog.csdnimg.cn/20200313224546707.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FuZHlhX25ldA==,size_16,color_FFFFFF,t_70"><br>
通过<code>kubectl describe cm cm-test01</code>查看更新是否生效<br>
<img src="https://img-blog.csdnimg.cn/20200313224726296.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FuZHlhX25ldA==,size_16,color_FFFFFF,t_70"><br>
<strong>2)apply</strong><br>
直接更改yaml文件里面的值,通过<code>kubectl apply -f configmap-test01.yaml</code>重新发布一遍进行更新。</p>
<h2 id="24-删除">2.4 删除</h2>
<p><strong>1)通过yaml文件的方式删除</strong><br>
<code>$ kubectl delete -f configmap-test01.yaml</code></p>
<p><strong>2)直接删除资源</strong><br>
<code>$ kubectl delete cm cm-test01</code></p>
<h1 id="3-configmap和pod的使用">3 ConfigMap和Pod的使用</h1>
<blockquote>
<p>容器应用对ConfigMap的使用主要是两种:<br>
1)通过环境变量获取ConfigMap的内容:<code>spec.env</code>和<code>spec.envFrom</code><br>
2)通过卷volume挂载的方式将ConfigMap的内容挂载到容器内部的文件或目录:<code>spec.volumes</code></p>
</blockquote>
<p>以下内容均以上述的ConfigMap资源<strong>cm-test01</strong>为例</p>
<h2 id="31-环境变量方式">3.1 环境变量方式</h2>
<h4 id="specenv方式">spec.env方式</h4>
<p><strong>1)创建pod</strong></p>
<pre><code class="language-yaml">#vim pod-test01.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test001
spec:
containers:
- name: cm-test
    image: tomcat:8
    command: [ "/bin/sh", "-c", "env | grep APP"]
    env:
    - name: APPCONF01                 # 定义环境变量的名称
      valueFrom:                          # key “appconf01”的值获取
      configMapKeyRef:
          name: cm-test01        # 环境变量的值来自于configmap cm-test01
          key: appconf01        # configmap中的配置key为appconf01
    - name: APPCONF02                # 定义环境变量的名称
      valueFrom:                        # key “appconf02”的值获取
      configMapKeyRef:
          name: cm-test01        # 环境变量的值来自于configmap cm-test01
          key: appconf02        # configmap中的配置key为appconf02
restartPolicy: Never                # 重启策略:从不。
</code></pre>
<p><strong>执行创建pod:</strong><br>
<code>$ kubectl create -f pod-test01.yaml</code></p>
<p><strong>2)查看pod</strong><br>
<code>$ kubectl get pods</code></p>
<pre><code class="language-shell">#kubectl get pods
NAME             READY   STATUS      RESTARTS   AGE
cm-pod-test001   0/1       Completed   0          1h
</code></pre>
<p><strong>3)查看pod日志</strong><br>
<code>$ kubectl logs cm-pod-test001</code></p>
<pre><code class="language-shell">#kubectl logs cm-pod-test001
APPCONF01=value01
APPCONF02=value02
</code></pre>
<p>说明容器内部的环境变量使用ConfigMap中进行读取的。</p>
<h4 id="specenvfrom方式">spec.envFrom方式</h4>
<p><strong>1)创建pod</strong><br>
<strong>yaml文件</strong></p>
<pre><code class="language-yaml">#vim pod-test02.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test002
spec:
containers:
- name: cm-test2
    image: tomcat:8
    command: [ "/bin/sh", "-c", "env"]
    envFrom:
    - configMapRef:
      name: cm-test01        # 根据ConfigMap cm-test01资源自动生成环境变量
restartPolicy: Never
</code></pre>
<p><strong>执行创建pod:</strong><br>
<code>$ kubectl create -f pod-test02.yaml</code></p>
<p><strong>2)查看pod</strong><br>
<code>$ kubectl get po</code></p>
<pre><code class="language-shell">#kubectl get po
NAME             READY   STATUS      RESTARTS   AGE
cm-pod-test001   0/1       Completed   0          2h
cm-pod-test002   0/1       Completed   0          1h
</code></pre>
<p><strong>注意:</strong><br>
环境变量的名称受限制:<code>*</code>,不能以数字或非法字符开头。</p>
<h2 id="32-卷挂载方式">3.2 卷挂载方式</h2>
<h4 id="指定items">指定items</h4>
<pre><code class="language-yaml">#vim pod-test03.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test003
spec:
containers:
- name: cm-test3
    image: tomcat:8
    volumeMounts:
    - name: vm-01-1
      mountPath: /conf
volumes:
- name: vm-01-1
    configMap:
      name: cm-test-file
      items:
      - key: key-testproperties
      path: test.properties
restartPolicy: Never
</code></pre>
<h4 id="不指定items">不指定items</h4>
<pre><code class="language-yaml">#vim pod-test04.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-pod-test004
spec:
containers:
- name: cm-test4
    image: tomcat:8
    volumeMounts:
    - name: vm-02-2
      mountPath: /conf
volumes:
- name: vm-02-2
    configMap:
      name: cm-test-file
restartPolicy: Never
</code></pre>
<p>进入容器中查看<br>
<code>$ kubectl exec -it cm-pod-test004 -c cm-test4-- bash</code><br>
进入容器后,<code>ls /conf</code>查看是否有test.properties文件。</p>
<pre><code class="language-shell">#kubectl exec -it cm-pod-test004 -c cm-test4-- bash
root@cm-pod-test004:/usr/local/tomcat# ls /conf
test.properties
</code></pre>
<h1 id="补充">补充</h1>
<p><strong>关于<code>--from-file</code>的方式的创建指定key和不指定key的区别</strong><br>
1)不指定key名<br>
创建:<br>
<code>$ kubectl create cm cm-test-file --from-file=test.properties</code><br>
输出:<br>
<code>$ kubectl get cm cm-test-file -o yaml</code><br>
<img src="https://img-blog.csdnimg.cn/20200313225534641.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FuZHlhX25ldA==,size_16,color_FFFFFF,t_70"><br>
2)指定key<br>
创建:<br>
<code>$ kubectl create cm cm-test-file02 --from-file=tp=test.properties</code><br>
输出:<br>
<code>$ kubectl get cm cm-test-file -o yaml</code><br>
<img src="https://img-blog.csdnimg.cn/20200313225723354.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FuZHlhX25ldA==,size_16,color_FFFFFF,t_70"><br>
若指定key的名称,configmap中将会使用指定名称;若不指定,则默认使用文件名为key。</p>


</div>
<div id="MySignature" role="contentinfo">
    烧不死的鸟就是凤凰<br><br>
来源:https://www.cnblogs.com/Andya/p/12490000.html
頁: [1]
查看完整版本: Kubernetes—配置管理ConfigMap