Pod Configuration Management

In Kubernetes, the resource objects and information are stored in Etcd in, but for a particular service configuration how to manage?
Of course, you can pack in a mirror when the configuration file directly to the mirror configuration packaged inside so you can really achieve their goals.
But most of the container is a need to change the configuration after running, each time repackaging does is not a small job.
Of course, you can change the configuration container through the mapping file or environment variable, which is slightly more good practice.
However, if large-scale cluster configuration management containers will be a very troublesome matter.
Kubernetes 1.2 from the start to provide a unified application configuration management solutions --ConfigMap.
ConfigMap application program and the configuration information required for separation, such configuration information may be reused many times better.
In Kubernetes, the configuration information is encapsulated into a resource resource object, it is easy to centrally manage and use.

If you need to modify the configuration, you only need to modify ConfigMap reference object or directly modify the configuration ConfigMao resource objects on it.

1.ConfigMap

Typical usage ConfigMap for containers as follows:
(1) generated as an environment variable in the container
(2) in the form of internal Volume mount container file or directory
ConfigMap in one or more key: value is saved in the system Kubernetes supplied with use,
may be used to represent the value of a variable (e.g. apploglevel = info), it may also be used to represent a complete configuration a content file (server.xml = <? xml>) .

 

2. Create a resource object ConfigMap

(1) create a configuration file by way yaml

apiVersion: V1 
kind: The ConfigMap 
Metadata: 
  name: cm & lt - AppVars 
Data: 
  apploglevel: info   # value specified as a string 
  appdatadir: / var / # Data file name or directory designated as

ConfigMap is a key (key = value) of the form to save the configuration information, key can be any string of unique, value can be a string can also be a file name directory names.

# Create configmap resource object 
root @ Console:/ K8S-Script / Chapter02 # kubectl the Create -f CM- appvars.yaml  
configmap"cm-AppVars"the Created # View configMap resource object 
root @ Console:

/ K8S-Script /Chapter02 # kubectl GET ConfigMap 
NAME the DATA of AGE
cm & lt-appvars   2         15s # resource object details see 
the root @ Console:

/ K8S-Script / Chapter02 # kubectl DESCRIBE ConfigMap / CM- AppVars 
the Name: cm & lt-AppVars
the Namespace: default
Labels:       <none>
Annotations:  < none>

The Data
 ==== 
appdatadir:
 ---- 
/ var / Data 
apploglevel:
 ----
 info 
Events:   <none> 

# a detailed format of the output of the resource object yaml 
the root @ Console: / K8S-Script / Chapter02 # kubectl GET ConfigMap / cm & lt-AppVars - O YAML 
apiVersion: V1 
Data: 
  appdatadir: / var / Data 
  apploglevel: info 
kind: The ConfigMap 
Metadata: 
  creationTimestamp: 2019 - 07 -08T03: 27 : 17Z 
  name: cm & lt - AppVars 
  namespace: default 
  resourceVersion: "117304"
  selfLink: /api/v1/namespaces/default/configmaps/cm-appvars
  uid: 49baf4d4-a130-11e9-9c97-52540095a842

Kubernetes also supports the contents of the configuration file is defined as ConfigMap usage.

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-appconfigfiles
data:
  key-config.json: '{
    "server":"192.168.1.1",
    "server_port":8038,
    "password":"666666",
    "timeout":300,
    "method":"aes-256-cfb",
    "fast_open": false
}'

Related operations:

# Create ConfigMap resource object 
root @ Console:/ K8S-Script / Chapter02 # kubectl the Create -f CM- appconfigfiles.yaml  
ConfigMap"cm-appconfigfiles"the Created
 # View ConfigMap resource object 
root @ Console:/ K8S-Script /Chapter02 # kubectl GET ConfigMap 
NAME the DATA of AGE
cm & lt-appconfigfiles   . 1         16S
cm & lt-appvars          2         28m
 # resource object details see 
the root @ Console:/ K8S-Script / Chapter02 # kubectl DESCRIBE ConfigMap / CM- appconfigfiles 
the Name: cm & lt-appconfigfiles 
the Namespace: default
Labels:       <none>
Annotations:  <none>

Data
====
key-config.json:
----
{ "server":"192.168.1.1", "server_port":8038, "password":"666666", "timeout":300, "method":"aes-256-cfb", "fast_open": false }
Events:  <none>

(2) create the command line by kubectl

Do not use yaml files can also be created directly by ConfigMap kubectl create configmap,
you can use the parameters --from-file or specify the --from-literal content, and you can specify multiple parameters in one command.

A. created from a file by --from-file parameters, you can specify the name of the key, you can create multiple ConfigMap key in a command line.

root@console:~# kubectl create configmap cm-ssh-config.xml --from-file=ssh_config
configmap "cm-ssh-config.xml" created
root@console:~# kubectl get configmap
NAME                DATA      AGE
cm-appconfigfiles   1         2h
cm-appvars          2         2h
cm-ssh-config.xml   1         17s

  B. by --from-file parameters from the directory created, each profile name of the directory are set to key, the contents of the file are set to value.

root@console:~# kubectl create configmap cm-appconf --from-file=configfiles
configmap "cm-appconf" created
root@console:~# kubectl describe configmap cm-appconf
Name:         cm-appconf
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
server.xml:
----

logging.properties:
----

Events:  <none>

  C .-- from-literal created from the text directly to the specified key # = value # created ConfigMap.

root@console:~# kubectl create configmap cm-appenv --from-literal=loglevel=info --from-literal=appdatadir=/var/data
configmap "cm-appenv" created
root@console:~# kubectl describe configmap/cm-appenv
Name:         cm-appenv
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
appdatadir:
----
/var/data
loglevel:
----
info
Events:  <none>

 

3. ConfigMap in the Pod

(1) by using environment variables ConfigMap

apiVersion: v1
kind: Pod
metadata:
  name: cm-test-pod
spec:
  containers:
  - name: cm-test
    image: busybox:latest
    command: ["/bin/sh","-c","env|grep APP"]
    env:
    - name: APPLOGLEVEL
      valueFrom:
        configMapKeyRef:
          name: cm-appvars
          key: apploglevel
    - name: APPDATADIR
      valueFrom:
        configMapKeyRef:
          name: cm-appvars
          key: appdatadir

In the creation cm-test-pod Pod of this, the cm-appvars this configmap the contents of which environment variables are introduced.
And when to start displaying the value of the two in the vessel environment variable.

root @ console: / k8s-script / Chapter02 # kubectl the Create -f cm-Test- pod.yaml  
POD " cm-the Test-POD " the Created 
# view the log, determined to have been introduced into the environment variable 
root @ Console: / K8S-Script / Chapter02 # kubectl logs cm & lt Test-- POD 
APPDATADIR = / var / Data 
APPLOGLEVEL = info

From kubernetes1.6 release, introduces a new field envFrom,
implemented in key = value Pod environment directly ConfigMap defined automatically generates environment variables.

apiVersion: v1
kind: Pod
metadata:
  name: cm-test-pod2
spec:
  containers:
  - name: cm-test
    image: busybox:latest
    imagePullPolicy: Never
    command: ["/bin/sh","-c","env"]
    envFrom:
    - configMapRef:
        name: cm-appvars

Related operations:

# Create Pod 
root @ Console:/ K8S-Script / Chapter02 # kubectl the Create -f cm-Test-pod2.yaml
POD"cm-the Test-POD2"the Created
 # View environment variable 
root @ Console:/ K8S-Script / Chapter02 # kubectl cm & lt POD2-Test-log |grepApp
apploglevel=info
appdatadir= / var / Data

(2) by using ConfigMap volumeMount

apiVersion: v1
kind: Pod
metadata:
  name: cm-test-app
spec:
  volumes:
  - name: serverxml
    configMap:
      name: cm-appconf
      items:
      - key: key-serverxml
        path: server.xml
      - key: key-logging.properties
        path: logging.properties
  containers:
  - name: cm-test-app
    image: kubeguide/tomcat-app:v1
    ports:
    - containerPort: 8080
    volumeMounts:
    - name: serverxml
      mountPath: /configfiles

 

4. restrictions ConfigMap

(1) ConfigMap must be created before Pod
(2) ConfigMap by Namespace restrictions, can only be in the same Namespaces can refer to it in the Pod
(3) ConfigMap the quota management also failed to achieve
(4) kubelet only supports can be API Server management Pod use ConfigMap.
Static Pod kubelet automatically created by --manifest-url or Node --config on this reference will not ConfigMap
(. 5) at the time of ConfigMap Pod mounts the (volumeMount) operation, the interior of the container can only be mounted "directory" not mounted as "file."
After loading into the container, the directory containing for each Item in the ConfigMap defined, if the directory has other original documents,
the directories in the container will be mounted ConfigMap coverage. If the application needs to retain other original documents, the need for additional processing.
ConfigMap can be mounted to the inside of the container temporary directory, then copy the configuration file or by starting a link to the actual configuration directory script used by the application.

Guess you like

Origin www.cnblogs.com/yangmingxianshen/p/12635699.html