根据实践学习K8S--5.使用ConfigMap对象下发配置2--yaml

上一节使用from-file来创建ConfigMap,这节使用yaml创建

1.创建yaml文件

[root@node-1 configmap]# vi tomcat.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: tomcat-config
data:
  log4j: |
    log4j.rootLogger=debug, R
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=${catalina.home}/logs/tomcat.log
    log4j.appender.R.MaxFileSize=10MB
    log4j.appender.R.MaxBackupIndex=10
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
    log4j.logger.org.apache.catalina=DEBUG, R
    log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
    log4j.logger.org.apache.catalina.core=DEBUG, R
    log4j.logger.org.apache.catalina.session=DEBUG, R

2.查看configmap对象

[root@node-1 configmap]# kubectl apply -f tomcat.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
configmap/tomcat-config configured
[root@node-1 configmap]# kubectl get configmaps tomcat-config 
NAME            DATA   AGE
tomcat-config   3      12h
[root@node-1 configmap]# kubectl describe configmaps tomcat-config 
Name:         tomcat-config
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"log4j":"log4j.rootLogger=debug, R \nlog4j.appender.R=org.apache.log4j.RollingFileAppender \nlog4j.appender.R.F...

Data
====
log4j:
----
log4j.rootLogger=debug, R 
log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=${catalina.home}/logs/tomcat.log 
log4j.appender.R.MaxFileSize=10MB 
log4j.appender.R.MaxBackupIndex=10 
log4j.appender.R.layout=org.apache.log4j.PatternLayout 
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 
log4j.logger.org.apache.catalina=DEBUG, R
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
log4j.logger.org.apache.catalina.core=DEBUG, R
log4j.logger.org.apache.catalina.session=DEBUG, R

3.应用到pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: centos
  labels:
    app: centos
spec:
  replicas: 1
  selector:
    matchLabels:
      app: centos
  template:
    metadata:
      labels:
        app: centos
    spec:
      containers:
      - name: centos
        image: centos:7.4-tomcat
        imagePullPolicy: Never
        command: [ "/bin/bash", "-c", "--" ]
        args: [ "while true; do sleep 30; done;" ]
        volumeMounts:
        - name: iso
          mountPath: /media/cdrom
        - name: tomcat-config
          mountPath: /tmp
      volumes:
      - name: iso
        hostPath:
          path: /mnt
      - name: tomcat-config
        configMap:
          name: tomcat-config
          items:
          - key: log4j
            path: log4j2.xml
      nodeSelector:
        path: iso

查看结果

[root@node-1 pod]# kubectl exec -it centos-6f559447bb-9vn9q /bin/bash        
[root@centos-6f559447bb-9vn9q /]# 
[root@centos-6f559447bb-9vn9q /]# cd /tmp/
[root@centos-6f559447bb-9vn9q tmp]# ll
total 0
lrwxrwxrwx 1 root root 17 Jan 28 04:00 log4j2.xml -> ..data/log4j2.xml
[root@centos-6f559447bb-9vn9q tmp]# 

修改configmap,写入#flag2作为区别

[root@node-1 configmap]# vi tomcat.yaml 
#flag2
apiVersion: v1
kind: ConfigMap
metadata:
  name: tomcat-config
data:
  log4j: |
    #flag2
    log4j.rootLogger=debug, R
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=${catalina.home}/logs/tomcat.log
    log4j.appender.R.MaxFileSize=10MB
    log4j.appender.R.MaxBackupIndex=10
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
    log4j.logger.org.apache.catalina=DEBUG, R
    log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
    log4j.logger.org.apache.catalina.core=DEBUG, R
    log4j.logger.org.apache.catalina.session=DEBUG, R
[root@node-1 configmap]# kubectl apply -f tomcat.yaml 
configmap/tomcat-config configured

等待十几秒后,pod的文件会自动更新

[root@centos-6f559447bb-9vn9q tmp]# ll
total 0
lrwxrwxrwx 1 root root 17 Jan 28 04:00 log4j2.xml -> ..data/log4j2.xml
[root@centos-6f559447bb-9vn9q tmp]# 
[root@centos-6f559447bb-9vn9q tmp]# cat log4j2.xml 
#flag2
log4j.rootLogger=debug, R 
log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=${catalina.home}/logs/tomcat.log 
log4j.appender.R.MaxFileSize=10MB 
log4j.appender.R.MaxBackupIndex=10 
log4j.appender.R.layout=org.apache.log4j.PatternLayout 
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 
log4j.logger.org.apache.catalina=DEBUG, R
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
log4j.logger.org.apache.catalina.core=DEBUG, R
log4j.logger.org.apache.catalina.session=DEBUG, R
[root@centos-6f559447bb-9vn9q tmp]# 

你可能感兴趣的:(K8S)