Real World Example: Configuring Redis with ConfigMap

Kubernetes Redis

k8s cluster configuration

IPHostConfiguration
11.0.1.150master1 (keepalived+haproxy)2C 4G 30G
11.0.1.151master2 (keepalived+haproxy)2C 4G 30G
11.0.1.152node12C 4G 30G

VIP address: https://11.0.1.100:16443

Target

  • Create a ConfigMap with the Redis configuration values
  • Create a Redis Pod, mount it and use the created ConfigMap
  • Verify that the configuration has been applied correctly
  • Verify ConfigMap mounting

Create Resources

Create configmap

redis-configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-configmap
data:
  redis-config: ""

create

$ kubectl create -f redis-configmap.yaml

Create a redis pod

redis-pod.yaml:

apiVersion: v1
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   # Redis will automatically read the environment variable
      value: "true"
    ports:
    - containerPort: 6379   # Expose container port 6379, protocol tcp, no port name specified
    resources:
      limits:  # Resource limits for the container
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data     # Temporary directory for writing Redis cache, created and destroyed with the pod lifecycle
      emptyDir: {}
    - name: config
      configMap:
        name: redis-configmap  # ConfigMap name
        items:
        - key: redis-config
          path: redis.conf   # File name after being mounted into the pod

create

$ kubectl create -f redis-pod.yaml

Verify Redis Configuration

Get redis configuration information

Use to kubectl execenter the pod and run redis-clithe tool to check the current configuration:

$ kubectl exec -it redis -- redis-cli

Check maxmemory:

127.0.0.1:6379> CONFIG GET maxmemory

It should show the default value of 0:

1) "maxmemory"
2) "0"

Also, check out maxmemory-policy:

127.0.0.1:6379> CONFIG GET maxmemory-policy

It should also show the default value noeviction:

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

Modify configmap

$ kubectl edit cm redis-configmap

redis-configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-configmap
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru     

Check the redis configuration again

Confirm that the ConfigMap has been updated:

kubectl describe configmap/redis-configmap

You should see the configuration we just added:

Name:         redis-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

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

Check the Redis Pod again to see if the configuration has been applied by kubectl execusing :redis-cli

kubectl exec -it redis -- redis-cli

Check maxmemory:

127.0.0.1:6379> CONFIG GET maxmemory

It keeps the default value of 0:

1) "maxmemory"
2) "0"

Again, maxmemory-policyleave the default settings as noeviction:

127.0.0.1:6379> CONFIG GET maxmemory-policy

return:

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

The configuration values ​​have not changed because the Pod needs to be restarted to pick up the updated values ​​from the associated ConfigMap. Let’s delete and recreate the Pod:

$ kubectl delete pod redis
$ kubectl apply -f redis-pod.yaml

Now, recheck the configuration values ​​one last time:

kubectl exec -it redis -- redis-cli

Check maxmemory:

127.0.0.1:6379> CONFIG GET maxmemory

Now, it should return the updated value 2097152:

1) "maxmemory"
2) "2097152"

Also maxmemory-policyupdated:

127.0.0.1:6379> CONFIG GET maxmemory-policy

Now it reflects the expected value allkeys-lru:

1) "maxmemory-policy"
2) "allkeys-lru"

Delete the created resources and clean up your work:

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

Documentation

[ 30% OFF ] Kubernetes Certification Coupon (CKAD , CKA , CKS)

Save 30% on all the Linux Foundation training and certification programs. This is a limited-time offer for this month. This offer is applicable for CKA, CKAD, CKSKCNALFCS, PCA FINOPSNodeJSCHFA, and all the other certification, training, and BootCamp programs.

Coupon: use code TECK30 at checkout

Hurry Up: Offer Ends Soon.  

Coupon: use code TECK30 at checkout

Hurry Up: Offer Ends Soon.  

Author

0 Shares:
You May Also Like
Make Kubernetes simpler! 8 AI Tools You Must Know
Read More

Make Kubernetes simpler! 8 AI Tools You Must Know

Table of Contents Show OverviewK8sGPTInstallPrerequisiteskubectl-aiInstall via Homebrew:Install via Krew:DemoKoPylotFunctionOperating principleKopilotInstallKubectl-GPTInstallPrerequisitesKube-CopilotInstallSet operationKubernetes ChatGPT botDemoAppilotAuthor Overview Kubernetes users inevitably face…