Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Deploy a Redis cluster on OpenShift Virtualization

September 18, 2024
Ahmed Bashir
Related topics:
Automation and managementDatabasesKubernetesVirtualization
Related products:
Red Hat OpenShiftRed Hat OpenShift Virtualization

Share:

    Redis is an open source, memory based key value store that is commonly used as a database, cache, and message broker. This article covers how to deploy a Redis cluster based on virtual machines (VMs) powered by Red Hat OpenShift Virtualization.

    Export the environment variables

    I created an Ansible playbook to automate the deployment of Redis virtual machines, OpenShift services, a Redis command-line interface (CLI) pod for interaction, and a Redis Insights deployment/service/route for cluster management and visualization. This playbook utilized the kubernetes.core and the kubevirt.core Ansible collections. 

    Before executing the playbook, we need to make sure the necessary authentication environment variables are exported. Below are the required environment variables for using kuberentes.core and kubevirt.core:

    export K8S_AUTH_HOST=<https://api.ocp.example.com:6443>
    export K8S_AUTH_API_KEY=<your-api-key>
    export K8S_AUTH_VERIFY_SSL=<true/false>

    Our Redis cluster will consist of 6 nodes: 3 primary nodes and 3 secondary nodes, all based on Fedora.

    Create the OpenShift services

    Before we create the virtual machines , we will create a dedicated OpenShift service for each VM as well as a service load balancing between all the Redis VMs. Individual VM services are targeted using the vm.kubevirt.io/name selector, while the load balancing service is using the app: redis selector to target all the VMs.

    In the Service definition, we've specified two ports:

    • Port 6379: The standard Redis data port for client connections.
    • Port 16379: The Redis bus port used for inter-node communication and cluster coordination.

    Below is a snippet of playbook detailing service creation:

    - name: Create redis cluster service
      kubernetes.core.k8s:
        state: present
        definition:
          apiVersion: v1
          kind: Service
          metadata:
            name: "redis"
            namespace: "{{ project }}"
          spec:
            selector:
              app: redis
            ports:
              - name: redis
                protocol: TCP
                port: 6379
                targetPort: 6379
              - name: bus
                protocol: TCP
                port: 16379
                targetPort: 16379
    - name: Create redis VM services
      kubernetes.core.k8s:
        state: present
        definition:
          apiVersion: v1
          kind: Service
          metadata:
            name: "{{ item }}"
            namespace: "{{ project }}"
          spec:
            selector:
              vm.kubevirt.io/name: "{{ item }}"
            ports:
              - name: redis
                protocol: TCP
                port: 6379
                targetPort: 6379
              - name: bus
                protocol: TCP
                port: 16379
                targetPort: 16379
      with_items:
      - "{{ redis_vms }}"

    Create the virtual machines

    Once the necessary services are created, we'll proceed to create the six virtual machines. The cloud-init configuration for these VMs will include installing the Redis package, configuring Redis with our desired parameters, and enabling the Redis service. Below is a snippet of playbook detailing VM cloud-init:

    #cloud-config
    user: fedora
    password: fedora
    chpasswd:
      expire: false
    ssh_pwauth: true
    packages:
    - redis
    runcmd:
    - sed -i "s/bind 127.0.0.1/bind 0.0.0.0/" /etc/redis/redis.conf
    - sed -i "s/protected-mode yes/protected-mode no/" /etc/redis/redis.conf
    - sed -i "s/# cluster-enabled yes/cluster-enabled yes/" /etc/redis/redis.conf
    - sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-6379.conf/" /etc/redis/redis.conf
    - sed -i "s/# cluster-node-timeout 15000/cluster-node-timeout 15000/" /etc/redis/redis.conf
    - sed -i "s/# replica-announce-ip 5.5.5.5/replica-announce-ip {{(query('kubernetes.core.k8s', kind='Service', resource_name=item,namespace=project) | first ).spec.clusterIP}}/" /etc/redis/redis.conf
    - sed -i "s/# cluster-announce-ip 10.1.1.5/cluster-announce-ip {{(query('kubernetes.core.k8s', kind='Service', resource_name=item,namespace=project) | first ).spec.clusterIP}}/" /etc/redis/redis.conf
    - systemctl enable --now redis

    Configure networking on OpenShift Virtualization

    When configuring networking for VMs on OpenShift Virtualization, there are primarily two approaches:

    • OpenShift CNI: Leveraging the built-in Container Network Interface (CNI) solution provided by OpenShift.
    • External networks: Utilizing external networks via bridges to connect VMs to the outside world.

    For this article, we will be deploying our Redis cluster VMs using the default OpenShift network.

    When connecting VMs to the default Pod network using OpenShift CNI, they access the Pod network via NAT, assigning a default IP of 10.0.2.2/24 at the guest OS level of all VMs. This presents a challenge for clustered components like Redis, which often require unique hostnames or IPs for bootstrapping and cluster connectivity.

    By utilizing Redis' cluster announce IPs and replica announce IPs configuration parameters, these issues can be effectively mitigated. When the Redis “cluster announce IP”/”replica announce IP”  is set as a Kubernetes service, it acts as stable virtual IP addresses that abstracts the underlying VM. 

    By configuring the cluster announce IP and replica announce IP to point to the Service's cluster IP, which is associated with the VM, we can establish a reliable communication mechanism within the Redis cluster. This will avoid using the default 10.0.2.2 IP that is present at the guest OS level. Here is a snippet of playbook detailing replica-announce-ip and cluster-announce-ip values:

    - sed -i "s/# replica-announce-ip 5.5.5.5/replica-announce-ip {{(query('kubernetes.core.k8s', kind='Service', resource_name=item,namespace=project) | first ).spec.clusterIP}}/" /etc/redis/redis.conf
    - sed -i "s/# cluster-announce-ip 10.1.1.5/cluster-announce-ip {{(query('kubernetes.core.k8s', kind='Service', resource_name=item,namespace=project) | first ).spec.clusterIP}}/" /etc/redis/redis.conf"

    These 2 lines in our cloud-init script  are responsible for setting the replica-announce-ip and the cluster-announce-ip to the value of the OpenShift service cluster IP that has been assigned to it. This is done by using the Kubernetes Ansible query to retrieve the service cluster IP that has been pre-created for each VM.

    Initialize the Redis cluster

    Once the VMs are created and bootstrapped using cloud-init, we can initialize the Redis cluster. Connecting to the Redis CLI pod, execute the following command:

    redis-cli --cluster create \
    redis-master-1.redis-cluster.svc.cluster.local:6379 \
    redis-master-2.redis-cluster.svc.cluster.local:6379 \
    redis-master-3.redis-cluster.svc.cluster.local:6379 \
    redis-slave-1.redis-cluster.svc.cluster.local:6379 \
    redis-slave-2.redis-cluster.svc.cluster.local:6379 \
    redis-slave-3.redis-cluster.svc.cluster.local:6379 --cluster-replicas 1

    Figure 1 depicts this command being run inside the Project.

    alt text
    Figure 1: Executing the redis-cli --cluster create command from the redis-cli pod.

    With the command executed, our Redis cluster is now ready to accept connections and clients. We can verify by running the following command and validating the PONG output:

    redis-cli -h redis.redis-cluster.svc.cluster.local ping
    +PONG

    Add the cluster to Redis Insights

    Now let's navigate our browser to the Redis Insights route that we created.

    Once loaded, we can add our Redis cluster. Fill out the information below, as shown in Figure 2.

    • Name: redis-cluster
    • Host: redis.redis-cluster.svc.cluster.local
    • Port: 6379
    alt text
    Figure 2: Adding the Redis cluster to Redis Insights.

    After adding the Redis cluster to Redis Insights, it will automatically detect the individual nodes. The displayed IPs are the OpenShift Service cluster IPs configured in the Redis configuration file. This indicates that the nodes are advertising themselves using the cluster IP as per our configuration. We can select all and proceed to Add Cluster Database, as shown in Figure 3.

    alt text
    Figure 3: Selecting the Redis cluster nodes to import into Redis Insights.

    After adding the Redis cluster, you can explore its overview in Redis Insights. Navigating to the CLI tab, you can execute commands like CLUSTER NODES. See Figure 4. This command will display a detailed breakdown of all cluster nodes, including their roles (primary/secondary) and the connected node, as shown in Figure 5.

    alt text
    Figure 4: Redis Insights overview page.
    alt text
    Figure 5: Executing CLUSTER NODES on the Redis Insights CLI.

    Test the Redis cluster

    To test the Redis cluster, create a new key-value pair using the following CLI command:

    SET GREETING "Hello World"

    Figure 6 shows this command being executed inside the CLI.

    alt text
    Figure 6: Executing SET GREETING "Hello World" on the Redis Insights CLI.

    Returning to the browser tab, you can explore your cluster data by clicking the GREETING key. This will reveal the value you set, Hello World. See Figure 7.

    alt text
    Figure 7: Viewing the newly created key value pair on the browser tab in Redis Insights.

    Conclusion

    This article has demonstrated how to create a Redis cluster running on VMs powered by OpenShift Virtualization. We've showcased the effective use of OpenShift Services for managing node-to-node communication within the Redis cluster. Additionally, we've explored basic operations like cluster health checks and data manipulation using redis-cli and Redis Insights.

    Related Posts

    • Develop a basic rate limiter with Quarkus and Redis

    • How To Setup A Redis Server Cluster on Red Hat

    • Enable OpenShift Virtualization on Red Hat OpenShift

    • Easily upgrade hosted OpenShift Virtualization clusters on hosted control planes

    • Monitor OpenShift Virtualization using user-defined projects and Grafana

    • OpenShift Virtualization for vSphere admins: A change in the traditional storage paradigm

    Recent Posts

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    • How to update OpenStack Services on OpenShift

    • How to integrate vLLM inference into your macOS and iOS apps

    What’s up next?

    Learn how to create and manage your virtual machines (VMs) using Red Hat OpenShift and the Developer Sandbox in this Learning Path.

     

    Start the activity
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue