k8s--Kubernetes存储--Configmap配置管理


一、环境的清理


##删除namespace
[root@server2 ingress]# kubectl get ns   ##查看所有namespace,并删除
[root@server2 ingress]# kubectl delete pod --all -n demo --force ##先删除里面pod在删除ns会快一点
[root@server2 ingress]# kubectl delete ns demo 
##删除pod
[root@server2 ingress]# kubectl delete pod --all -n test --force 
[root@server2 ingress]# kubectl delete ns test 
[root@server2 ingress]# kubectl delete pod nginx --force 
[root@server2 ingress]# kubectl delete deployments.apps deployment
[root@server2 ingress]# kubectl delete pod demo --force 
##删除服务
[root@server2 ingress]# kubectl get svc
[root@server2 ingress]# kubectl delete svc nginx-svc 
##删除ingress服务
[root@server2 ingress]# kubectl delete ingress ingress-demo 
 
## 删除网络策略
[root@server2 ingress]# kubectl delete networkpolicies. --all

二、Configmap配置管理

ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中
官方网址:https://v1-25.docs.kubernetes.io/zh-cn/docs/concepts/configuration/configmap/
在这里插入图片描述

Configmap用于保存配置数据,以键值对形式存储。
	configMap 资源提供了向 Pod 注入配置数据的方法。
	旨在让镜像和配置文件解耦,以便实现镜像的可移植性和可复用性。	
	典型的使用场景:	
		1.填充环境变量的值
		2.设置容器内的命令行参数
		3.填充卷的配置文件     ##使用较多
 
 
- 创建ConfigMap的方式有4种:
	1.使用字面值创建
	2.使用文件创建
	3.使用目录创建
	4.编写configmap的yaml文件创建
	
- 1.使用字面值创建
	$ kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
 
- 2.使用文件创建	 
	$ kubectl create configmap my-config-2 --from-file=/etc/resolv.conf
	key的名称是文件名称,value的值是这个文件的内容
 
- 3.使用目录创建
	$ kubectl create configmap my-config-3 --from-file=test
	目录中的文件名为key,文件内容是value
 
- 4.编写configmap的yaml文件
	$ vim cm1.yaml
	apiVersion: v1
	kind: ConfigMap
	metadata:
	  name: cm1-config
	data:
	  db_host: "172.25.0.250"
	  db_port: "3306"
	
	$ kubectl create -f cm1.yaml

1. 使用字面值创建

[root@k8s2 configmap]# kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
[root@k8s2 configmap]# kubectl get cm
NAME               DATA   AGE
kube-root-ca.crt   1      5d23h
my-config          2      4s
[root@k8s2 configmap]# kubectl describe cm my-config
Name:         my-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
key2:
----
config2
key1:
----
config1

2.通过文件创建

[root@k8s2 configmap]# kubectl create configmap my-config-2 --from-file=/etc/resolv.conf
在这里插入图片描述

3.通过目录创建

[root@k8s2 configmap]# mkdir test
[root@k8s2 configmap]# cp /etc/passwd test/
[root@k8s2 configmap]# cp /etc/fstab  test/
[root@k8s2 configmap]# ls test/
fstab  passwd
[root@k8s2 configmap]# kubectl create configmap my-config-3 --from-file=test

在这里插入图片描述

4.通过yaml文件创建

[root@k8s2 configmap]# vim cm1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm1-config
data:
  db_host: "172.25.0.250"
  db_port: "3306"

[root@k8s2 configmap]# kubectl apply -f cm1.yaml

在这里插入图片描述

三、如何使用configmap

如何使用configmap:
	1.通过环境变量的方式直接传递给pod		
	2.通过在pod的命令行下运行的方式		
	3.作为volume的方式挂载到pod内                ##此方式最常用

1.使用configmap设置环境变量

[root@k8s2 configmap]# vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
    - name: pod1
      image: busybox
      command: ["/bin/sh", "-c", "env"]    ##env命令,显示当前系统环境变量
      env:                                 ##env此处用来定义相应的变量
        - name: key1
          valueFrom:
            configMapKeyRef:               ##映射的,相当于把cm( configMapKeyRef)中的值拿出来给key1
              name: cm1-config
              key: db_host
        - name: key2
          valueFrom:
            configMapKeyRef:
              name: cm1-config
              key: db_port
  restartPolicy: Never                    ##运行完退出

[root@k8s2 configmap]# kubectl apply -f pod1.yaml

在这里插入图片描述

[root@k8s2 configmap]# kubectl delete  pod pod1

[root@k8s2 configmap]# vim pod2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod2
spec:
  containers:
    - name: pod2
      image: busybox
      command: ["/bin/sh", "-c", "env"]
      envFrom:
        - configMapRef:
            name: cm1-config
  restartPolicy: Never

[root@k8s2 configmap]# kubectl apply -f pod2.yaml

在这里插入图片描述
[root@k8s2 configmap]# kubectl delete pod pod2

2.使用conigmap设置命令行参数

[root@k8s2 configmap]# vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod3
spec:
  containers:
    - name: pod3
      image: busybox
      command: ["/bin/sh", "-c", "echo $(db_host) $(db_port)"] ##调用shell解析命令【"/bin/sh", "-c"】
      envFrom:
        - configMapRef:
            name: cm1-config
  restartPolicy: Never

[root@k8s2 configmap]# kubectl apply -f pod3.yaml

[root@k8s2 configmap]# kubectl logs  pod3
172.25.0.250 3306

[root@k8s2 configmap]# kubectl delete  pod pod3

3.通过数据卷使用configmap

[root@k8s2 configmap]# vim pod4.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod4
spec:
  containers:
    - name: pod4
      image: busybox
      command: ["/bin/sh", "-c", "cat /config/db_host"]##挂载时:cm的key变成了文件名db_host,创建时:文件名变成key
      volumeMounts:                                    ##创建挂载点  
      - name: config-volume                            ##名字
        mountPath: /config                             ##实际容器内的挂载点
  volumes:
    - name: config-volume                              ##当容器内要挂载卷,卷所映射的实际是什么,此处是cm1-config
      configMap:
        name: cm1-config
  restartPolicy: Never
[root@k8s2 configmap]# kubectl apply -f pod4.yaml
[root@k8s2 configmap]# kubectl logs  pod4
172.25.0.250

[root@k8s2 configmap]# kubectl delete pod pod4

4.configmap热更新

[root@k8s2 configmap]# vim nginx.conf
server {
    listen       8000;
    server_name  _;

    location / {
        root /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

[root@k8s2 configmap]# kubectl create configmap nginxconf --from-file=nginx.conf

文件名变成key,文件内容变成值:
在这里插入图片描述

[root@k8s2 configmap]# vim my-nginx.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              ##挂接点
      volumes:
        - name: config-volume                         ##映射的
          configMap:
            name: nginxconf

[root@k8s2 configmap]# kubectl apply -f my-nginx.yaml
[root@k8s2 configmap]# kubectl get pod -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP              NODE   NOMINATED NODE   READINESS GATES
my-nginx-665dbc64f8-gps5w   1/1     Running   0          8s    10.244.219.13   k8s3   <none>           <none>

在这里插入图片描述

[root@k8s2 configmap]# kubectl exec my-nginx-665dbc64f8-gps5w -- cat /etc/nginx/conf.d/nginx.conf
server {
    listen       8000;
    server_name  _;

    location / {
        root /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

[root@k8s2 configmap]# curl 10.244.219.13:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
...

编辑cm,修改端口
[root@k8s2 configmap]# kubectl edit cm nginxconf
在这里插入图片描述
查看pod内的文件:端口也变为80在这里插入图片描述

修改cm后,过上几秒配置信息会同步到容器,但是容器内运行的服务并不会加载生效,需要手动刷新
注意:服务的配置文件需要重载;不是服务的的文件不需要重载:如yaml文件、系统文件等等
方式一:(推荐)
[root@k8s2 configmap]# kubectl delete  pod my-nginx-665dbc64f8-gps5w

方式二:(手动触发版本更新,会新建一个replicaset)
[root@k8s2 configmap]# kubectl patch deployments.apps my-nginx --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20230312"}}}}}'

在这里插入图片描述


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值