ConfigMap顾名思义,是用于保存配置数据的键值对,可以用来保存单个属性,也可以保存配置文件。
ConfigMaps允许你将配置构件与映像内容解耦,以保持容器化应用程序的可移植性。configmap 可以从文件、目录或者 key-value 字符串创建等创建 ConfigMap。也可以通过 kubectl create -f从描述文件创建。可以使用 kubectl create创建命令。创建ConfigMap的方式有4种:
- 直接在命令行中指定configmap参数创建,即--from-literal。
- 指定文件创建,即将一个配置文件创建为一个ConfigMap--from-file=<文件>
- 指定目录创建,即将一个目录下的所有配置文件创建为一个ConfigMap,--from-file=<目录>
- 先写好标准的configmap的yaml文件,然后kubectl create -f 创建
命令行方式
从key-value字符串创建,官方翻译是从字面值中创建ConfigMap。
语法规则
kubectl create configmap map的名字 --from-literal=key=value
如果有多个key,value。可以继续在后边写
kubectl create configmap map的名字
--from-literal=key1=value1
--from-literal=key2=value2
--from-literal=key3=value4
案例
创建configmap
kubectl create configmap helloconfigmap --from-literal=lagou.hello=world
查看configmap
kubectl get configmap helloconfigmap -o go-template='{{.data}}'
在使用kubectl get获取资源信息的时候,可以通过-o(--output简写形式)指定信息输出的格式,如果 指定的是yaml或者json输出的是资源的完整信息,实际工作中,输出内容过少则得不到我们想要的信息,输 出内容过于详细又不利于快速定位的我们想要找到的内容,其实-o输出格式可以指定为go-template然后 指定一个template,这样我们就可以通过go-template获取我们想要的内容.go-template与 kubernetes无关,它是go语言内置的一种模板引擎.这里不对go-template做过多解释,仅介绍在 kubernetes中获取资源常用的语法,想要获取更多内容,大家可以参考相关资料获取帮助。大家记住是固 定语法即可。
配置文件方式
语法规则
语法规则如下:当 --from-file指向一文件,key的名称是文件名称,value的值是这个文件的内容。
kubectl create configmap cumulx-test --from-file=xxxx
案例
创建一个配置文件:jdbc.properties
vi jdbc.properties
jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=admin
创建configmap换用其他方式查看信息
kubectl create configmap myjdbcmap --from-file=jdbc.properties
查看configmap详细信息
kubectl describe configmaps myjdbcmap
目录方式
语法规则
语法规则如下:当 --from-file指向一个目录,每个目录中的文件直接用于填充ConfigMap中的 key,key的名称是文件名称,value的值是这个文件的内容。下面的命令读取/data目录下的所有文件
kubectl create configmap cumulx-test --from-file=/data/
案例
kubectl create configmap myjdbcconfigmap --from-file=/data/jdbc.properties
查看configmap详细信息
kubectl describe configmaps myjdbcmap
资源文件方式
yml文件
apiVersion: v1
kind: ConfigMap
metadata:
name: mariadbconfigmap
data:
mysql-driver: com.mysql.jdbc.Driver
mysql-url: jdbc:mysql://localhost:3306/test
mysql-user: root
mysql-password: admin
运行yml文件
kubectl apply -f mariadb-configmap.yml
查看configmap详细信息
kubectl describe configmaps mariadbconfigmap
configmap升级mariadb
获得my.cnf文件
运行测试镜像:
docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d
192.168.198.155:5000/lagouedu/mariadb:10.5.2
将my.cnf文件从容器中复制到/data目录
docker cp some-mariadb:/etc/mysql/my.cnf /data
停止容器
docker stop some-mariadb
删除容器
docker rm some-mariadb
删除镜像
docker rmi -f 192.168.198.155:5000/lagouedu/mariadb:10.5.2
更改my.cnf文件中的端口号为:3307
生成configmap文件
data/mariadbconfigmap.yml
将my.cnf文件中mariadb数据库启动端口号修改为3307,将my.cnf文件上传master节点,首先通过命令行方式生成configmap资源。
再通过命令行方式将configmap反向生成yaml文件。将生成文件copy回 idea开发工具。修改yaml文件备用。
kubectl create configmap mysqlini --from-file=my.cnf
kubectl get configmap mysqlini -o yaml > mariadbconfigmap.yml
修改mariadb.yml
volumeMounts:
- mountPath: /etc/mysql/mariadb.conf.d/
name: mariadbconfigmap
restartPolicy: Always
volumes:
- name: mariadbconfigmap
configMap:
name: mariadbconfigmap
全部资源文件清单
configmap/mariadbsecret.yml
apiVersion: v1
kind: Secret
metadata:
name: mariadbsecret
type: Opaque
data:
password: YWRtaW4=
#mariadb的用户名root加密,用于演示,无实际效果
username: cm9vdA==
configmap/mariadb.yml--需要再次整理,加入私服镜像信息
注意修改pod的端口号为3307,service的targetPort端口号为3307
apiVersion: apps/v1
kind: Deployment
metadata:
name: mariadb-deploy
labels:
app: mariadb-deploy
spec:
replicas: 1
template:
metadata:
name: mariadb-deploy
labels:
app: mariadb-deploy
spec:
imagePullSecrets:
- name: lagouharbor
containers:
- name: mariadb-deploy
image: 192.168.198.155:5000/lagouedu/mariadb:10.5.2
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3307
env:
- name: MYSQL_ROOT_PASSWORD
#这是mysqlroot用户的密码
valueFrom:
secretKeyRef:
key: password
name: mariadbsecret
- name: TZ
value: Asia/Shanghai
args:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
volumeMounts:
- mountPath: /etc/mysql/mariadb.conf.d/ #容器内的挂载目录
name: lagoumariadb #随便给一个名字,这个名字必须与volumes.name一致
restartPolicy: Always
volumes:
- name: lagoumariadb
configMap:
name: mariadbconfigmap
selector:
matchLabels:
app: mariadb-deploy
---
apiVersion: v1
kind: Service
metadata:
name: mariadb-svc
spec:
selector:
app: mariadb-deploy
ports:
- port: 3307
targetPort: 3307
nodePort: 30036
type: NodePort
configmap/mariadbconfigmap.yml
apiVersion: v1
data:
my.cnf: "# MariaDB database server configuration file.\n#\n# You can copy this file
to one of:\n# - \"/etc/mysql/my.cnf\" to set global options,\n# - \"~/.my.cnf\"
to set user-specific options.\n# \n# One can use all long options that the program
supports.\n# Run program with --help to get a list of available options and with\n#
--print-defaults to see which it would actually understand and use.\n#\n# For
explanations see\n# http://dev.mysql.com/doc/mysql/en/server-system- variables.html\n\n#
This will be passed to all mysql clients\n# It has been reported that passwords
should be enclosed with ticks/quotes\n# escpecially if they contain \"#\" chars...\n#
Remember to edit /etc/mysql/debian.cnf when changing the socket location.\n[client]\nport\t\t=
3307\nsocket\t\t= /var/run/mysqld/mysqld.sock\n\n# Here is entries for some specific
programs\n# The following values assume you have at least 32M ram\n\n# This was
formally known as [safe_mysqld]. Both versions are currently parsed.\n[mysqld_safe]\nsocket\t\t=
/var/run/mysqld/mysqld.sock\nnice\t\t= 0\n\n[mysqld]\n#\n# * Basic Settings\n#\n#user\t\t=
mysql\npid-file\t= /var/run/mysqld/mysqld.pid\nsocket\t\t= /var/run/mysqld/mysqld.sock\nport\t\t=
3307\nbasedir\t\t= /usr\ndatadir\t\t= /var/lib/mysql\ntmpdir\t\t= /tmp\nlc_messages_dir\t=
/usr/share/mysql\nlc_messages\t= en_US\nskip-external-locking\n#\n# Instead of
skip-networking the default is now to listen only on\n# localhost which is more
compatible and is not less secure.\n#bind-address\t\t= 127.0.0.1\n#\n# * Fine
Tuning\n#\nmax_connections\t\t= 100\nconnect_timeout\t\t= 5\nwait_timeout\t\t=
600\nmax_allowed_packet\t= 16M\nthread_cache_size = 128\nsort_buffer_size\t=
4M\nbulk_insert_buffer_size\t= 16M\ntmp_table_size\t\t= 32M\nmax_heap_table_size\t=
32M\n#\n# * MyISAM\n#\n# This replaces the startup script and checks MyISAM tables
if needed\n# the first time they are touched. On error, make copy and try a repair.\nmyisam_recover_options
= BACKUP\nkey_buffer_size\t\t= 128M\n#open-files-limit\t= 2000\ntable_open_cache\t=
400\nmyisam_sort_buffer_size\t= 512M\nconcurrent_insert\t= 2\nread_buffer_size\t=
2M\nread_rnd_buffer_size\t= 1M\n#\n# * Query Cache Configuration\n#\n# Cache only
tiny result sets, so we can fit more in the query cache.\nquery_cache_limit\t\t=
128K\nquery_cache_size\t\t= 64M\n# for more write intensive setups, set to DEMAND
or OFF\n#query_cache_type\t\t= DEMAND\n#\n# * Logging and Replication\n#\n# Both
location gets rotated by the cronjob.\n# Be aware that this log type is a performance killer.\n#
As of 5.1 you can enable the log at runtime!\n#general_log_file = /var/log/mysql/mysql.log\n#
general_log = 1\n#\n# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.\n#\n#
we do want to know about network errors and such\n#log_warnings\t\t= 2\n#\n# Enable the slow query log to
see queries with especially long duration\n#slow_query_log[={0|1}]\nslow_query_log_file\t=
/var/log/mysql/mariadb-slow.log\nlong_query_time = 10\n#log_slow_rate_limit\t= 1000\n#log_slow_verbosity\t=
query_plan\n\n#log-queries-not-using- indexes\n#log_slow_admin_statements\n#\n#
The following can be used as easy to replay backup logs or for replication.\n# note: if you are setting up a
replication slave, see README.Debian about\n# other settings you may need to change.\n#server-id\t\t= 1\n#
report_host\t\t= master1\n#auto_increment_increment = 2\n#auto_increment_offset\t= 1\n#log_bin\t\t\t=
/var/log/mysql/mariadb-bin\n#log_bin_index\t\t= /var/log/mysql/mariadb-bin.index\n#
not fab for performance, but safer\n#sync_binlog\t\t= 1\nexpire_logs_days\t= 10\nmax_binlog_size = 100M\n#
slaves\n#relay_log\t\t= /var/log/mysql/relay-bin\n#relay_log_index\t= /var/log/mysql/relay- bin.index\n#
relay_log_info_file\t= /var/log/mysql/relay-bin.info\n#log_slave_updates\n#read_only\n#\n#
If applications support it, this stricter sql_mode prevents some\n# mistakes like inserting invalid dates etc.\n#
sql_mode\t\t= NO_ENGINE_SUBSTITUTION,TRADITIONAL\n#\n# * InnoDB\n#\n# InnoDB is enabled by default with a
10MB datafile in /var/lib/mysql/.\n# Readthe manual for more InnoDB related options. There are many!\n
default_storage_engine\t= InnoDB\ninnodb_buffer_pool_size\t= 256M\ninnodb_log_buffer_size\t= 8M\ninnodb_file_per_table\t=
1\ninnodb_open_files\t= 400\ninnodb_io_capacity\t= 400\ninnodb_flush_method\t= O_DIRECT\n#\n# * Security Features\n#\n#
Read the manual, too, if you want chroot!\n# chroot = /var/lib/mysql/\n#\n#
For generating SSL certificates I recommend the OpenSSL GUI \"tinyca\".\n#\n# ssl-ca=/etc/mysql/cacert.pem\n#
ssl- cert=/etc/mysql/server-cert.pem\n# ssl-key=/etc/mysql/server-key.pem\n\n#\n# * Galera-related settings\n#\n[galera]\n#
Mandatory settings\n#wsrep_on=ON\n#wsrep_provider=\n#wsrep_cluster_address=\n#
binlog_f ormat=row\n#default_storage_engine=InnoDB\n#innodb_autoinc_lock_mode=2\n#\n#
Allow server to accept connections on all interfaces.\n#\n#bind- address=0.0.0.0\n#\n#
Optional setting\n#wsrep_slave_threads=1\n#innodb_flush_log_at_trx_commit=0\n\n
[mysql dump]\nquick\nquote-names\nmax_allowed_packet\t= 16M\n\n[mysql]\n#no-auto-rehash\t#
faster start of mysql but no tab completion\n\n[isamchk]\nkey_buffer\t\t= 16M\n\n#\n#
* IMPORTANT: Additional settings that can override those from this file!\n#
The files must end with '.cnf', otherwise they'll be ignored.\n#\n!include
/etc/mysql/mariadb.cnf\n!includedir /etc/mysql/conf.d/\n"
kind: ConfigMap
metadata:
name: mariadbconfigmap
namespace: default
运行服务
部署服务
kubectl apply -f .
查看相关信息
kubectl get configmaps
kubectl get pods
查看mariadb容器日志
查看mariadb容器日志:观察mariadb启动port是否为3307
kubectl logs -f mariadb-5d99587d4b-xwthc
客户端测试
IP:192.168.198.157
username:root
password:admin
prot: 30036
网友评论