1、ConfigMapp配置管理
1.1 ConfigMapp简介
ConfigMap是存储通用的配置变量的。ConfigMap有点儿像一个统一的配置文件,使用户可以将分布式系统中用于不同模块的环境变量统一到一个对象中管理;而它与配置文件的区别在于它是存在集群的“环境”中的,并且支持K8s集群中所有通用的操作调用方式。
而资源的使用者可以通过ConfigMap来存储这个资源的配置,这样需要访问这个资源的应用就可以同通过ConfigMap来引用这个资源。相当通过创建Configmap封装资源配置。
configmap以一个或者多个key:value的形式保存在k8s系统中供应用使用,既可以用于表示一个变量的值(reg.westos:info),也可以用于表示一个完整配置文件的内容
实验环境:
[root@server2 calico]# kubectl delete -f deny-nginx.yaml 删除之前的设置,保证实验环境的纯净
networkpolicy.networking.k8s.io "deny-nginx" deleted
networkpolicy.networking.k8s.io "acces-nginx" deleted
networkpolicy.networking.k8s.io "default-deny" deleted
networkpolicy.networking.k8s.io "deny-namespace" deleted
networkpolicy.networking.k8s.io "access-namespace" deleted
networkpolicy.networking.k8s.io "web-allow-external" deleted
[root@server2 ~]# kubectl delete ns test 删除test空间
1.2 、configmap创建方式
1.2.1 使用字面直创建:
[root@server2 configmap]# kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2 以键直形式存储
configmap/my-config created
[root@server2 configmap]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 13d
my-config 2 40s 创建成功
[root@server2 configmap]# kubectl describe cm my-config 查看创建的my-config 详细信息
Name: my-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
key1:
----
config1
key2:
----
config2
BinaryData
====
Events: <none>
[root@server2 configmap]# kubectl get cm my-config -o yaml 也可以将配置文件信息转换成yaml格式
apiVersion: v1
data:
key1: config1
key2: config2
kind: ConfigMap
metadata:
creationTimestamp: "2022-03-29T13:52:47Z"
name: my-config
namespace: default
resourceVersion: "738797"
uid: 6223c5b0-88e0-4627-92a4-0b196718d848
1.2.2 使用文件创建:
kubectl create configmap my-config-2 --from-file=/etc/resolv.conf 其中文件名就是key,文件内容就是直
[root@server2 configmap]# kubectl describe cm my-config-2 查看配置文件详细信息
Name: my-config-2
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
resolv.conf: 这个就代表key
----
# Generated by NetworkManager
nameserver 114.114.114.114 文件里的内容就是键值
BinaryData
====
Events: <none>
如果想加多个,该怎么办??
[root@server2 configmap]# mkdir test 创建一个目录,将多个文件放到目录里
[root@server2 configmap]# cp /etc/passwd test/
[root@server2 configmap]# cp /etc/fstab test/
[root@server2 configmap]# kubectl create configmap my-config-3 --from-file=test 此时test是个目录
1.2.3使用yaml文件创建:
[root@server2 configmap]# vim cm1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm1-config
data:
db_host: "172.25.50.250" 键值
db_port: "3306" 键值
[root@server2 configmap]# kubectl apply -f cm1.yaml 运行
configmap/cm1-config created
1.3、 如何使用configmap
1.3.1 通过环境变量的方式传递给pod
[root@server2 configmap]# vim cm1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm1-config
data:
db_host: "172.25.50.250"
db_port: "3306"
---
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
containers:
- name: pod1
image: busybox
command: ["/bin/sh", "-c", "env"] 表示在shell里执行打印变量
env: 将定义的键值传入容器内
- name: key1 此时key的名称被重新定义
valueFrom: 值从哪来
configMapKeyRef: 从configMapKeyRef做映射来
name: cm1-config 从cm1-config来
key: db_host
- name: key2 此时key的名称被重新定义
valueFrom:
configMapKeyRef:
name: cm1-config
key: db_port
restartPolicy: Never
[root@server2 configmap]# kubectl apply -f cm1.yaml 运行
configmap/cm1-config unchanged
pod/pod1 created
[root@server2 configmap]# kubectl logs pod1 查看日志的输出
[root@server2 configmap]# kubectl delete pod pod1 删除pod1
pod "pod1" deleted
[root@server2 configmap]# vim cm1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm1-config
data:
db_host: "172.25.50.250"
db_port: "3306"
---
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
containers:
- name: pod1
image: busybox
command: ["/bin/sh", "-c", "env"]
envFrom:
- configMapRef:
name: cm1-config 变量来自cm配置文件
restartPolicy: Never
[root@server2 configmap]# kubectl apply -f cm1.yaml
configmap/cm1-config unchanged
pod/pod1 created
[root@server2 configmap]# kubectl logs pod1 查看日志输出
1.3.2 通过在pod命令行里运行的方式
[root@server2 configmap]# kubectl delete pod pod1
pod "pod1" deleted
[root@server2 configmap]# vim cm1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm1-config
data:
db_host: "172.25.50.250"
db_port: "3306"
---
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
containers:
- name: pod1
image: busybox
command: ["/bin/sh", "-c", "echo $(db_host) $(db_port)"] 直接在命令行里输出两个变量直,两个变量直取至于cm1-config
envFrom:
- configMapRef:
name: cm1-config
restartPolicy: Never
[root@server2 configmap]# kubectl apply -f cm1.yaml 运行
configmap/cm1-config unchanged
pod/pod1 created
[root@server2 configmap]# kubectl logs pod1
172.25.50.250 3306
1.3.3 通过数据卷使用congfigmap
[root@server2 configmap]# kubectl delete pod pod1 删除pod1
pod "pod1" deleted
[root@server2 configmap]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod2
spec:
containers:
- name: pod2
image: busybox
command: ["/bin/sh", "-c", "cat /config/db_host"]
volumeMounts: 卷挂载
- name: config-volume 挂载的卷名字
mountPath: /config 挂载到什么地方,路经没有可以自动建立
volumes: 此参数表示卷的声明
- name: config-volume
configMap: 挂载的卷内容是映射configMap里的cm1-config 的内容
name: cm1-config
restartPolicy: Never
[root@server2 configmap]# kubectl apply -f pod.yaml 运行
pod/pod2 created
[root@server2 configmap]# kubectl logs pod2
172.25.50.250
1.3.4 ConfigMap热更新
[root@server2 configmap]# kubectl delete -f pod.yaml 删除
[root@server2 configmap]# vim pod.yaml 创建pod,一直运行
pod "pod2" deleted
apiVersion: v1
kind: Pod
metadata:
name: pod2
spec:
containers:
- name: pod2
image: busybox
stdin: true
tty: true 交互式,一直运行在后端
volumeMounts:
- name: config-volume
mountPath: /config
volumes:
- name: config-volume
configMap:
name: cm1-config
[root@server2 configmap]# kubectl apply -f pod.yaml 运行
pod/pod2 created
[root@server2 configmap]# kubectl get pod 查看,一直运行后端
[root@server2 configmap]# kubectl get cm 查看cm
NAME DATA AGE
cm1-config 2 14h
kube-root-ca.crt 1 13d
my-config 2 14h
my-config-2 1 14h
my-config-3 2 14h
[root@server2 configmap]# kubectl delete cm my-config 将不要的cm删除掉
configmap "my-config" deleted
[root@server2 configmap]# kubectl delete cm my-config-2
configmap "my-config-2" deleted
[root@server2 configmap]# kubectl delete cm my-config-3
configmap "my-config-3" deleted
[root@server2 configmap]# kubectl edit cm cm1-config 编辑cm1-config
/ # cd config/
/config # ls
db_host db_port
/config # cat db_port 已经变更了
8080
/config # cat db_host 已经变更了
172.25.50.100
[root@server2 configmap]# kubectl delete -f pod.yaml 删除
pod "pod2" deleted
[root@server2 configmap]# vim nginx.conf 创建nginx.conf文件,其中文件名为key,里面内容为值
server {
listen 80;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@server2 configmap]# kubectl create configmap nginxconf --from-file=nginx.conf 创建一个configmap的cm
configmap/nginxconf created
[root@server2 configmap]# kubectl describe cm nginxconf 查看cm为nginxconf配置文件详细信息
Name: nginxconf
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
nginx.conf:
----
server {
listen 80;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@server2 configmap]# vim pod.yaml 编辑配置文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/conf.d 路经为nginx 的 include目录,
volumes: 表示将nginx.conf的内容挂接到/etc/nginx/conf.d目录里
- name: config-volume
configMap:
name: nginxconf
[root@server2 configmap]# kubectl apply -f pod.yaml 运行
deployment.apps/my-nginx created
[root@server2 configmap]# kubectl get pod
[root@server2 configmap]# kubectl exec -it my-nginx-7b84dc948c-9bvf7 -- bash 进入容器内部
root@my-nginx-7b84dc948c-9bvf7:/# cd /etc/nginx/
root@my-nginx-7b84dc948c-9bvf7:/etc/nginx# ls
conf.d mime.types nginx.conf uwsgi_params
fastcgi_params modules scgi_params
root@my-nginx-7b84dc948c-9bvf7:/etc/nginx# cd conf.d/
root@my-nginx-7b84dc948c-9bvf7:/etc/nginx/conf.d# ls
nginx.conf
root@my-nginx-7b84dc948c-9bvf7:/etc/nginx/conf.d# cat nginx.conf 下面就是挂接内容
server {
listen 80;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
root@server2 configmap]# kubectl get pod -o wide 查看ip
[root@server2 configmap]# curl 10.244.141.232 访问,成功获取nginx默认页面
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
[root@server2 configmap]# kubectl edit cm nginxconf 编辑cm
[root@server2 configmap]# kubectl exec -it my-nginx-7b84dc948c-9bvf7 -- bash
root@my-nginx-7b84dc948c-9bvf7:/# cd /etc/nginx/conf.d/
root@my-nginx-7b84dc948c-9bvf7:/etc/nginx/conf.d# cat nginx.conf
server {
listen 8080; 已经变更为8080
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
root@my-nginx-7b84dc948c-9bvf7:/etc/nginx/conf.d# curl localhost 访问,可以看出依然访问的是80而不是8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
root@my-nginx-7b84dc948c-9bvf7:/etc/nginx/conf.d# curl localhost:8080 访问8080,访问不了,说明修改后没有生效,需要手动触发
curl: (7) Failed to connect to localhost port 8080: Connection refused
kubectl patch deployments.apps my-ngi