Kubernetes ConfigMap - Secret - 使用ConfigMap来配置 Redis

目录

ConfigMap :

参考文档:k8s -- ConfigMap - 简书 (jianshu.com)    K8S ConfigMap使用 - 知乎 (zhihu.com)

ConfigMap的作用类型:

可以作为卷的数据来源:使用 ConfigMap 来配置 Redis | Kubernetes

可以基于文件创建 ConfigMap:配置 Pod 使用 ConfigMap | Kubernetes

可以基于目录创建 ConfigMap:配置 Pod 使用 ConfigMap | Kubernetes

为什么需要使用ConfigMap呢?

使用 ConfigMap 来配置 Redis

参考网址:使用 ConfigMap 来配置 Redis | Kubernetes

配置拓扑图:

步骤:

nginx配置文件投射:(ConfigMap 基于文件创造)

Secret

参考文档:Secret | Kubernetes

例子:使用 Secret 安全地分发凭证:使用 Secret 安全地分发凭证 | Kubernetes


ConfigMap :

ConfigMap理解为一个容器,存放数据的地方,里面存放键值对的数据或者是文件类型,里面存放的数据是明文的,不是加密

ConfigMap顾名思义,是用于保存配置数据的键值对,可以用来保存单个属性,也可以保存配置文件。Secret可以为Pod提供密码、Token、私钥等敏感数据;对于一些非敏感数据,比如应用的配置信息,则可以使用ConfigMap。

Secret理解为一个容器,存放数据的地方,在内存里,里面存放敏感的数据,例如密码,密钥,token等,需要进行加密,数据可以是键值对或者文件

参考文档k8s -- ConfigMap - 简书 (jianshu.com)    K8S ConfigMap使用 - 知乎 (zhihu.com)

ConfigMap的作用类型:

可以作为卷的数据来源:使用 ConfigMap 来配置 Redis | Kubernetes

可以基于文件创建 ConfigMap:配置 Pod 使用 ConfigMap | Kubernetes

可以基于目录创建 ConfigMap:配置 Pod 使用 ConfigMap | Kubernetes

kubectl create configmap game-config --from-file=configure-pod-container/configmap/

为什么需要使用ConfigMap呢?

使用k8s部署应用,当你将应用配置写进代码中,就会存在一个问题,更新配置时也需要打包镜像,configmap可以将配置信息和docker镜像解耦。
使用微服务架构的话,存在多个服务共用配置的情况,如果每个服务中单独一份配置的话,那么更新配置就很麻烦,使用configmap可以友好的进行配置共享。
其次,configmap可以用来保存单个属性,也可以用来保存配置文件。

基于文件创造ConfigMap拓扑图

 

使用 ConfigMap 来配置 Redis

参考网址:使用 ConfigMap 来配置 Redis | Kubernetes

配置拓扑图:

步骤:

首先创建一个配置模块为空的 ConfigMap:

cat <<EOF >./example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: ""
EOF

应用上面创建的 ConfigMap 以及 Redis pod 清单:

[root@master configmap]# kubectl apply -f example-redis-config.yaml
configmap/example-redis-config created
[root@master configmap]# wget https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml

配置文件 redis-pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:5.0.4
    command:
      - redis-server
      - "/redis-master/redis.conf"   #redis启动的时候加载配置文件redis.conf
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379
    resources:
      limits:
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data
      emptyDir: {}
    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config   #读取key redis-config的内容
          path: redis.conf    #写到容器里的redis.conf配置文件里去

启动redis-pod.yaml文件

[root@master configmap]# kubectl apply -f redis-pod.yaml 
pod/redis created
[root@master configmap]# 

检查创建的对象:

[root@master configmap]# kubectl get pod/redis configmap/example-redis-config 
NAME        READY   STATUS    RESTARTS   AGE
pod/redis   1/1     Running   0          3m20s

NAME                             DATA   AGE
configmap/example-redis-config   1      5m2s
[root@master configmap]# 

使用 kubectl exec 进入 pod,运行 redis-cli 工具检查当前配置:

[root@master configmap]# kubectl exec -it redis -- redis-cli
127.0.0.1:6379> 

查看 maxmemory

127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
127.0.0.1:6379> 

同样,查看 maxmemory-policy

127.0.0.1:6379> CONFIG GET maxmemory-policy

它也应该显示默认值 noeviction

1) "maxmemory-policy"
2) "noeviction"

应用更新的 ConfigMap:

向 example-redis-config ConfigMap 添加一些配置:

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru    
[root@master configmap]# kubectl  apply -f example-redis-config-2.yaml 
configmap/example-redis-config configured
[root@master configmap]# kubectl  get cm
NAME                   DATA   AGE
example-redis-config   1      15m
kube-root-ca.crt       1      31h
[root@master configmap]# 

确认 ConfigMap 已更新:

[root@master configmap]# kubectl describe configmap/example-redis-config
Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lru 


BinaryData
====

Events:  <none>
[root@master configmap]# 

重新启动 Pod 才能从关联的 ConfigMap 中获取更新的值。 

[root@master configmap]# kubectl delete pod redis
pod "redis" deleted
[root@master configmap]# ls
example-redis-config-2.yaml  example-redis-config.yaml  redis-pod.yaml
[root@master configmap]# kubectl apply -f redis-pod.yaml
pod/redis created
[root@master configmap]# kubectl get pod -o wide
NAME                                READY   STATUS              RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
redis                               1/1     Running             0          15s   10.244.1.18   node1   <none>           <none>
test                                1/1     Running             0          28h   10.244.2.7    node2   <none>           <none>
[root@master configmap]# 

现在,最后一次重新检查配置值:

[root@master configmap]# kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory   #查看 maxmemory:
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> 
#现在,它应该返回更新后的值 2097152 大概2mb

127.0.0.1:6379> CONFIG GET maxmemory-policy # 同样,maxmemory-policy 也已更新:
1) "maxmemory-policy"
2) "allkeys-lru"
127.0.0.1:6379> 
#现在它反映了期望值 allkeys-lru:

删除创建的资源,清理你的工作:

kubectl delete pod/redis configmap/example-redis-config

nginx配置文件投射:(ConfigMap 基于文件创造)


===
[root@scmaster nginx]# cat nginx/nginx.conf 
worker_processes  4;
events {
    worker_connections  2048;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
	listen  80;
	server_name localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@scmaster nginx]# kubectl create configmap wyt-nginx-1 --from-file=nginx.conf
[root@scmaster nginx]# cat nginx.yaml 
kind: Deployment
metadata:
  name: wyt-nginx
spec:
  replicas: 2
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wyt-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wyt-nginx
  template:
    metadata:
      labels:
        app: wyt-nginx
    spec:
      containers:
        - name: nginx
          image: "nginx:latest"
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 80
          volumeMounts:
          - name: wyt-nginx-config
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
      volumes:
        - name: wyt-nginx-config
          configMap:
            name: wyt-nginx-1
            items:
            - key: nginx.conf
              path: nginx.conf              

[root@scmaster nginx]# 

Secret

Secret 是存储诸如密码或密钥之类的敏感数据的对象 --》相当于一个密码箱

Secret 对象类型用来保存敏感信息,例如密码、OAuth 令牌和 SSH 密钥。 

Kubernetes Secret 默认情况下存储为 base64-编码的、非加密的字符串。

参考文档:Secret | Kubernetes

例子:使用 Secret 安全地分发凭证:使用 Secret 安全地分发凭证 | Kubernetes

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值