K8S - Java微服务配置 - 使用ConfigMap配置redis

参考文档:https://v1-27.docs.kubernetes.io/zh-cn/docs/tutorials/configuration/configure-redis-using-configmap/

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

#由 spec.volumes[1] 创建一个名为 config 的卷
#spec.volumes[1].items[0] 下的 key 和 path 会将来自 example-redis-config ConfigMap 中的 redis-config 密钥公开在 config 卷上一个名为 redis.conf 的文件中。
#然后 config 卷被 spec.containers[0].volumeMounts[1] 挂载在 /redis-master。
#pod内容示例:
apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:5.0.4
    command:
      - redis-server
      - "/redis-master/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
          path: redis.conf


#创建configmap
kubectl apply -f example-redis-config.yaml
#创建pod redis
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml

在这里插入图片描述
又来这个错?搜了下默认是从docker hub拉取,可能是仓库地址的配置问题,这里先不纠结这个
我们更新下直接拉取docker的 reids 镜像

yum update -y
yum makecache
docker pull redis

# 运行后进入pod
kubectl apply -f redis-pod.yaml
kubectl exec -it redis -- redis-cli

在这里插入图片描述
通过ConfigMap修改redis配置

[root@weihengmaster1 configmap]# vim example-redis-config.yaml 
#添加 maxmemory 和 maxmemory-policy 配置项
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru   

#修改后别忘了更新到 configmap 资源中
[root@weihengmaster1 configmap]# kubectl apply -f example-redis-config.yaml
configmap/example-redis-config configured
[root@weihengmaster1 configmap]# 

更新成功:
在这里插入图片描述

测试完毕清理资源
kubectl delete pod/redis configmap/example-redis-config

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值