1.在生成环境中,相同集群的应用配置是一样的,如果要修改其中一个配置,就需要修改所有节点的配置,.
K8S中通过ConfigMap可以统一下发变量和配置文件.
以tomcat为例,先在pod中安装tomcat:
[root@centos-7c556897c8-pxj9b /]# yum install tomcat
安装完成之后配置文件在:
[root@centos-7c556897c8-pxj9b tomcat]# pwd
/etc/tomcat
[root@centos-7c556897c8-pxj9b tomcat]# ll
total 216
drwxrwxr-x 3 root tomcat 23 Jan 27 09:15 Catalina
-rw-r--r-- 1 root tomcat 13443 Aug 7 2017 catalina.policy
-rw-r--r-- 1 root tomcat 6496 Aug 7 2017 catalina.properties
drwxr-xr-x 2 root tomcat 20 Jan 27 09:15 conf.d
-rw-r--r-- 1 root tomcat 1394 Aug 7 2017 context.xml
-rw-r--r-- 1 root tomcat 547 Aug 7 2017 log4j.properties
-rw-r--r-- 1 root tomcat 3288 Aug 7 2017 logging.properties
-rw-r--r-- 1 root tomcat 6613 Aug 7 2017 server.xml
-rw-r----- 1 root tomcat 2418 Aug 7 2017 tomcat-users.xml
-rw-r--r-- 1 root tomcat 1651 Aug 7 2017 tomcat.conf
-rw-r--r-- 1 root tomcat 167655 Aug 7 2017 web.xml
下面我们对log4j.properties和server.xml进行统一配置.
先将pod保存成镜像.
1.找到pod所在的节点:
[root@node-1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-7c556897c8-pxj9b 1/1 Running 0 4d23h 192.168.1.43 node-2 <none> <none>
2.进入节点进行镜像保存
[root@node-2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cfe0858884de 22872cc86f9c "/bin/bash -c -- 'wh鈥 5 days ago Up 5 days k8s_centos_centos-7c556897c8-pxj9b_default_1db8f14a-3900-4826-beab-10b1f170c240_0
a7eaf04cdd29 registry.aliyuncs.com/google_containers/pause:3.1 "/pause" 5 days ago Up 5 days k8s_POD_centos-7c556897c8-pxj9b_default_1db8f14a-3900-4826-beab-10b1f170c240_0
1f21ee9bada9 ff281650a721 "/opt/bin/flanneld -鈥 5 days ago Up 5 days k8s_kube-flannel_kube-flannel-ds-amd64-ngv5r_kube-system_0af8bd3f-2ede-4d7e-973f-babfb621d32d_7
896928f1e17d c21b0c7400f9 "/usr/local/bin/kube鈥 5 days ago Up 5 days k8s_kube-proxy_kube-proxy-mt2xz_kube-system_7298b76b-ecfb-459f-adc0-8d8e0647d0ec_6
0ecb8fdd8d05 registry.aliyuncs.com/google_containers/pause:3.1 "/pause" 5 days ago Up 5 days k8s_POD_kube-flannel-ds-amd64-ngv5r_kube-system_0af8bd3f-2ede-4d7e-973f-babfb621d32d_7
665a883cbf77 registry.aliyuncs.com/google_containers/pause:3.1 "/pause" 5 days ago Up 5 days k8s_POD_kube-proxy-mt2xz_kube-system_7298b76b-ecfb-459f-adc0-8d8e0647d0ec_7
[root@node-2 ~]# docker commit --author "dingyz" --
--author --change --help --message --pause=false
[root@node-2 ~]# docker commit --author "dingyz" --message "centos7.4-tomcat" cfe0858884de centos:7.4-tomcat
sha256:4a8deb9c9d407ae7f8589a14d468a8f856e098ba80f1487d49b5daa645cb5280
[root@node-2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.4-tomcat 4a8deb9c9d40 11 seconds ago 434MB
centos centos7.4.1708.net-tools 22872cc86f9c 5 days ago 236MB
registry.cn-hangzhou.aliyuncs.com/dingyz/centos 7.4.net-tools 22872cc86f9c 5 days ago 236MB
centos latest 470671670cac 9 days ago 237MB
nginx latest c7460dfcab50 2 weeks ago 126MB
mysql 5.7 db39680b63ac 4 weeks ago 437MB
mysql latest ed1ffcb5eff3 4 weeks ago 456MB
registry.aliyuncs.com/google_containers/kube-proxy v1.16.0 c21b0c7400f9 4 months ago 86.1MB
registry.aliyuncs.com/google_containers/coredns 1.6.2 bf261d157914 5 months ago 44.1MB
centos centos7.4.1708 9f266d35e02c 10 months ago 197MB
registry.cn-hangzhou.aliyuncs.com/dingyz/centos 7.4.base 9f266d35e02c 10 months ago 197MB
registry.cn-hangzhou.aliyuncs.com/userdefine/centos 7.4.base 9f266d35e02c 10 months ago 197MB
quay-mirror.qiniu.com/coreos/flannel v0.11.0-amd64 ff281650a721 12 months ago 52.6MB
registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 2 years ago 742kB
kubeguide/tomcat-app v1 a29e200a18e9 3 years ago 358MB
nginx 1.7.9 84581e99d807 5 years ago 91.7MB
[root@node-2 ~]#
可以将镜像导出到其他节点,获取上传到云仓库,让其他节点也可以进行部署.
[root@node-2 ~]# docker save -o ./centos7.4-tomcat.tar centos:7.4-tomcat
[root@node-2 ~]#
[root@node-2 ~]# ls
anaconda-ks.cfg centos7.4-tomcat.tar Desktop Documents Downloads initial-setup-ks.cfg logstash-7.4.2.tar.gz metricbeat-7.4.2-linux-x86_64.tar.gz Music Pictures Public Templates Videos
[root@node-2 ~]# scp centos7.4-tomcat.tar [email protected]:/root
[root@node-3 ~]# ls
anaconda-ks.cfg centos7.4-tomcat.tar Desktop Dockerfile Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates test.tar Videos
[root@node-3 ~]# docker load < centos7.4-tomcat.tar
c0b8b77dce7c: Loading layer [==================================================>] 200MB/200MB
Loaded image: centos:7.4-tomcat
[root@node-3 ~]#
返回管理节点,创建配置目录,并添加配置文件:
[root@node-1 tomcat]# pwd
/opt/k8s/configmap/tomcat
[root@node-1 tomcat]# ls
log4j.properties server.xml
[root@node-1 tomcat]#
创建ConfigMap对象:直接从目录读取所有配置文件内容.
[root@node-1 configmap]# kubectl create configmap tomcat-config --from-file=tomcat
configmap/tomcat-config created
[root@node-1 configmap]#
[root@node-1 configmap]#
[root@node-1 configmap]#
[root@node-1 configmap]#
[root@node-1 configmap]# kubectl describe configmaps tomcat-config tomcat
Name: tomcat-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
log4j.properties:
----
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
server.xml:
----
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
Events: <none>
创建pod将configMap中的配置挂载到pod中,
[root@node-1 pod]# vi centos_tomcat.yaml
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: serverxml
mountPath: /etc/tomcat/log4j1.properties
subPath: log4j.properties
- name: serverxml
mountPath: /etc/tomcat/server.xml
subPath: server.xml
volumes:
- name: iso
hostPath:
path: /mnt
- name: serverxml
configMap:
name: tomcat-config
items:
- key: log4j.properties
path: log4j.properties
- key: server.xml
path: server.xml
nodeSelector:
path: iso
查看结果:
[root@centos-6d8f95c859-v9lh2 /]# ll /etc/tomcat/
total 220
drwxrwxr-x 3 root tomcat 23 Jan 27 09:15 Catalina
-rw-r--r-- 1 root tomcat 13443 Aug 7 2017 catalina.policy
-rw-r--r-- 1 root tomcat 6496 Aug 7 2017 catalina.properties
drwxr-xr-x 2 root tomcat 20 Jan 27 09:15 conf.d
-rw-r--r-- 1 root tomcat 1394 Aug 7 2017 context.xml
-rw-r--r-- 1 root tomcat 547 Aug 7 2017 log4j.properties
-rw-r--r-- 1 root root 547 Jan 27 14:42 log4j1.properties
-rw-r--r-- 1 root tomcat 3288 Aug 7 2017 logging.properties
-rw-r--r-- 1 root root 6613 Jan 27 14:42 server.xml
-rw-r----- 1 root tomcat 2418 Aug 7 2017 tomcat-users.xml
-rw-r--r-- 1 root tomcat 1651 Aug 7 2017 tomcat.conf
-rw-r--r-- 1 root tomcat 167655 Aug 7 2017 web.xml
我们将configmap的logging的配置文件添加一个标记:
[root@node-1 tomcat]# cat log4j.properties
#flag
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
当前方式不支持热更新,所以需要删除deployment,重新创建.
[root@node-1 pod]# kubectl delete deployments.apps centos
deployment.apps "centos" deleted
[root@node-1 pod]# kubectl create -f centos_tomcat.yaml
deployment.apps/centos created
[root@node-1 pod]#
[root@node-1 pod]#
[root@node-1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
centos-6d8f95c859-r4xw2 1/1 Running 0 10s
centos-6d8f95c859-v9lh2 1/1 Terminating 1 17m
[root@node-1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
centos-6d8f95c859-r4xw2 1/1 Running 0 12s
centos-6d8f95c859-v9lh2 1/1 Terminating 1 17m
[root@node-1 pod]# kubectl exec -it centos-6d8f95c859-r4xw2 /bin/bash
[root@centos-6d8f95c859-r4xw2 /]# cd /etc/tomcat/
[root@centos-6d8f95c859-r4xw2 tomcat]# ll
total 220
drwxrwxr-x 3 root tomcat 23 Jan 27 09:15 Catalina
-rw-r--r-- 1 root tomcat 13443 Aug 7 2017 catalina.policy
-rw-r--r-- 1 root tomcat 6496 Aug 7 2017 catalina.properties
drwxr-xr-x 2 root tomcat 20 Jan 27 09:15 conf.d
-rw-r--r-- 1 root tomcat 1394 Aug 7 2017 context.xml
-rw-r--r-- 1 root tomcat 547 Aug 7 2017 log4j.properties
-rw-r--r-- 1 root root 553 Jan 27 14:59 log4j1.properties
-rw-r--r-- 1 root tomcat 3288 Aug 7 2017 logging.properties
-rw-r--r-- 1 root root 6613 Jan 27 14:59 server.xml
-rw-r----- 1 root tomcat 2418 Aug 7 2017 tomcat-users.xml
-rw-r--r-- 1 root tomcat 1651 Aug 7 2017 tomcat.conf
-rw-r--r-- 1 root tomcat 167655 Aug 7 2017 web.xml
[root@centos-6d8f95c859-r4xw2 tomcat]# cat log4j1.properties
#flag
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
很晚,睡觉…