k8s of configMap

configMap is a quick way to modify the variables of the container, the composition kv, when modifying configmap container variable is also changed accordingly.
Our help documentation

[root@node-1 ~]# kubectl explain pod.spec.containers.env.valueFrom.configMapKeyRef

[root@node-1 ~]# kubectl explain configmap

configmap can be ordered directly create value can also be saved to a file, the file named key, the contents of the file to value.
directly with the command:

[root@node-1 ~]# kubectl create configmap --help
 kubectl create configmap nginx-nc --from-literal=nginx_port=80 --from-literal=nginx_server=erick.com
 查看创建的cm
[root@node-1 ~]# kubectl get cm
NAME       DATA   AGE
nginx-nc   2      60s
[root@node-1 cm]# kubectl get cm nginx-nc -o yaml
apiVersion: v1
data:
  nginx_port: "80"
  nginx_server: erick.com
kind: ConfigMap
metadata:
  creationTimestamp: "2019-06-20T22:34:44Z"
  name: nginx-nc
  namespace: default
  resourceVersion: "432545"
  selfLink: /api/v1/namespaces/default/configmaps/nginx-nc
  uid: 9a180b6e-93ab-11e9-b0ae-080027edb92f
[root@node-1 cm]# 

The value stored in files

[root@node-1 cm]# cat www.conf 
server {
        server_name myapp.com;
        port 80;
        root /data/web/html;

}
[root@node-1 cm]# kubectl create configmap nginx-cm-from-file --from-file=./www.conf 
configmap/nginx-cm-from-file created
[root@node-1 cm]# kubectl get cm
NAME                 DATA   AGE
nginx-cm-from-file   1      7s
nginx-nc             2      9m7s
[root@node-1 cm]# kubectl get cm nginx-cm-from-file -o yaml
apiVersion: v1
data:
  www.conf: "server {\n\tserver_name myapp.com;\n\tport 80;\n\troot /data/web/html;\n\n}\n"
kind: ConfigMap
metadata:
  creationTimestamp: "2019-06-20T22:43:44Z"
  name: nginx-cm-from-file
  namespace: default
  resourceVersion: "433432"
  selfLink: /api/v1/namespaces/default/configmaps/nginx-cm-from-file
  uid: dbd2aa33-93ac-11e9-b0ae-080027edb92f
[root@node-1 cm]# 
也可以用describe 看
[root@node-1 cm]# kubectl describe cm nginx-nc
Name:         nginx-nc
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx_port:
----
80
nginx_server:
----
erick.com
Events:  <none>
[root@node-1 cm]# kubectl describe cm nginx-cm-from-file
Name:         nginx-cm-from-file
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
www.conf:
----
server {
  server_name myapp.com;
  port 80;
  root /data/web/html;

}

Events:  <none>

Created in this way can be seen, key for the file name, key to the contents of the file.

Creating a pod association just created cm
[root @ the Node-1 cm] # CAT cm-1.yml
apiVersion: v1
kind: Pod
the Metadata:
name: myapp-cm-1
namespace: default
Annotations:
Erick: "Erick by"
spec :
Containers:

  • name: myapp-cm-1
    image: ikubernetes/myapp:v1
    ports:
    • name: http
      containerPort: 80
      env:
    • name: nginx_port
      valueFrom:
      configMapKeyRef:
      name: nginx-nc
      key: nginx_port
    • name: nginx_server
      valueFrom:
      configMapKeyRef:
      name: nginx-nc
      key: nginx_server

[root@node-1 cm]#

See into the container and the environment variables
[the root-Node. 1 cm & lt @] GET # kubectl POD
NAME RESTARTS of AGE the STATUS READY
MyApp-Running 1/1. 1 0-cm & lt 2m16s
[the root-Node. 1 cm & lt @] # kubectl Exec Expediting IT-cm & lt MyApp -1 - / bin / SH
/ # the env | grep nginx_port
nginx_port = 80
/ # the env | grep nginx_server
nginx_server = erick.com
/ #

We cm of environment variables to modify

[root@node-1 cm]# kubectl edit  configmap  nginx-nc
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  nginx_port: "8080"
  nginx_server: erick.com
kind: ConfigMap
metadata:
  creationTimestamp: "2019-06-20T22:34:44Z"
  name: nginx-nc
  namespace: default
  resourceVersion: "436267"
  selfLink: /api/v1/namespaces/default/configmaps/nginx-nc
  uid: 9a180b6e-93ab-11e9-b0ae-080027edb92f
~                                          
把port修改成8080

再次查看环境变量是否更改
[root@node-1 cm]# kubectl exec -it myapp-cm-1 -- /bin/sh
/ # env|grep nginx_port
nginx_port=80
/ # 

Conclusion: pod environment variables will only take effect when first created, even if it will not take effect restart pod, post-modification will not take effect.

2. Reference environment variables based on pod storage volume.

apiVersion: v1
kind: Pod
metadata:
name: myappcmwww
namespace: default
annotations:
erick: "by erick"
spec:
containers:

  • name: myappcmwww
    image: ikubernetes/myapp:v1
    ports:
    • name: http
      containerPort: 80
      volumeMounts:
    • name: nginx-conf
      mountPath: /etc/nginx/conf.d/
      volumes:
  • name: nginx-conf
    configMap:
    name: nginx-cm-from-file
    [root@node-1 cm]#

See container into the environment variable
[the root-Node. 1 cm & lt @] # kubectl Exec Expediting IT myappcmwww - / bin / SH
/ CAT # /etc/nginx/conf.d/
..2019_06_22_09_11_04.278015527 / ..data / www.conf
/ CAT # /etc/nginx/conf.d/www.conf
Server {
server_name myapp.com;
the listen 80;
the root / Data / Web / HTML;

}
/ #

Modify configmap port 8080

[root@node-1 ~]# kubectl edit cm nginx-cm-from-file
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  www.conf: "server {\n\tserver_name myapp.com;\n\tlisten 8080;\n\troot /data/web/html;\n\n}\n"
kind: ConfigMap
metadata:
  creationTimestamp: "2019-06-20T22:43:44Z"
  name: nginx-cm-from-file
  namespace: default
  resourceVersion: "494403"
  selfLink: /api/v1/namespaces/default/configmaps/nginx-cm-from-file
  uid: dbd2aa33-93ac-11e9-b0ae-080027edb92f

在容器内查看环境变量是否更改。
/ # cat /etc/nginx/conf.d/www.conf 
server {
        server_name myapp.com;
        listen 8080;
        root /data/web/html;

}
/ # 

The dynamic has changed.

Secret
Secret format is encoded using base64

[root@node-1 cm]# kubectl create secret --help
[root@node-1 cm]# kubectl create secret --help
Create a secret using specified subcommand.

Available Commands:
  docker-registry Create a secret for use with a Docker registry ## 链接私有镜像时
  generic         Create a secret from a local file, directory or literal value  ## 储存密码时
  tls             Create a TLS secret ## 放入证书时

Usage:
  kubectl create secret [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
[root@node-1 cm]# 

secrete bash64 is encrypted, the decryption may be reversed.

Guess you like

Origin blog.51cto.com/shyln/2415948