Redis Enterprise database (REDB) lifecycle

A Redis Enterprise database (REDB) is created with a custom resource file. The custom resource defines the size, name, and other specifications for the REDB. The database is created when you apply the custom resource file.

The database controller in Redis Enterprise for Kubernetes:

  • Discovers the custom resource
  • Makes sure the REDB is created in the same namespace as the Redis Enterprise cluster (REC)
  • Maintains consistency between the custom resource and the REDB

The database controller recognizes the new custom resource and validates the specification. If valid, the controller combines the values specified in the custom resource with default values to create a full specification. It then uses this full specification to create the database on the specified Redis Enterprise cluster (REC).

Once the database is created, it is exposed with the same service mechanisms by the service rigger for the Redis Enterprise cluster. If the database custom resource is deleted, the database and its services are deleted from the cluster.

Create a database

Your Redis Enterprise database custom resource must be of the kind: RedisEnterpriseDatabase and have values for name and memorySize. All other values are optional and will be defaults if not specified.

  1. Create a file (in this example mydb.yaml) that contains your database custom resource.

    apiVersion: app.redislabs.com/v1alpha1
    kind: RedisEnterpriseDatabase
    metadata:
      name: mydb
    spec:
      memorySize: 1GB
    

    To create a REDB in a different namespace from your REC, you need to specify the cluster with redisEnterpriseCluster in the spec section of your RedisEnterpriseDatabase custom resource.

    redisEnterpriseCluster:
      name: rec
    
  2. Apply the file in the namespace you want your database to be in.

    kubectl apply -f mydb.yaml
    
  3. Check the status of your database.

    kubectl get redb mydb -o jsonpath="{.status.status}"
    

    When the status is active, the database is ready to use.

Modify a database

The custom resource defines the properties of the database. To change the database, you can edit your original specification and apply the change or use kubectl edit.

To modify the database:

  1. Edit the definition:

    kubectl edit redb mydb
    
  2. Change the specification (only properties in spec section) and save the changes.
    For more details, see RedisEnterpriseDatabaseSpec or Options for Redis Enterprise databases.

  3. Monitor the status to see when the changes take effect:

    kubectl get redb mydb -o jsonpath="{.status.status}"
    

    When the status is active, the database is ready for use.

Delete a database

The database exists as long as the custom resource exists. If you delete the custom resource, the database controller deletes the database. The database controller removes the database and its services from the cluster.

To delete a database, run:

kubectl delete redb mydb

Connect to a database

After the database controller creates a database, the services for accessing the database are created in the same namespace. By default there are two services, one ‘ClusterIP’ service and one ‘headless’ service.
Connection information for the database is stored in a Kubernetes secret maintained by the database controller. This secret contains:

  • The database port (port)
  • A comma separated list of service names (service_names)
  • The database password for authenticating (password)

The name of that secret is stored in the database custom resource.

Note:
Use these steps to connect to your database from within your K8s cluster. To access your database from outside the K8s cluster, set up the Ingress controller or use OpenShift routes.
  1. Retrieve the secret name.

    kubectl get redb mydb -o jsonpath="{.spec.databaseSecretName}"
    

    The database secret name usually takes the form of redb-<database_name>, so in our example it will be redb-mydb.

  2. Retrieve and decode the password.

    kubectl get secret redb-mydb -o jsonpath="{.data.password}" | base64 --decode
    
  3. Retrieve and decode the port number.

    kubectl get secret redb-mydb -o jsonpath="{.data.port}" | base64 --decode
    
  4. Retrieve and decode the service_names.

    kubectl get secret redb-mydb -o jsonpath="{.data.service_names}" | base64 --decode
    

    You’ll need to pick just one service listed here to use for connecting.

  5. From a pod within your cluster, use redis-cli to connect to your database.

    redis-cli -h <service_name> -p <port>
    
  6. Enter the password you retrieved from the secret.

    auth <password>
    

    You are now connected to your database!