Manage Redis Enterprise databases for Kubernetes
This section describes how the database controller provides the ability to create, manage, and use databases via a database custom resource.
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.
Flexible deployment options
Databases in multiple namespaces can be managed by the same operator. To learn more, see Manage databases in multiple namespaces.
To learn more about designing a multi-namespace Redis Enterprise cluster, see flexible deployment options.
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.
-
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 thespec
section of your RedisEnterpriseDatabase custom resource.redisEnterpriseCluster: name: rec
-
Apply the file in the namespace you want your database to be in.
kubectl apply -f mydb.yaml
-
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:
-
Edit the definition:
kubectl edit redb mydb
-
Change the specification (only properties in
spec
section) and save the changes.
For more details, see RedisEnterpriseDatabaseSpec or Options for Redis Enterprise databases. -
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.
-
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 beredb-mydb
. -
Retrieve and decode the password.
kubectl get secret redb-mydb -o jsonpath="{.data.password}" | base64 --decode
-
Retrieve and decode the port number.
kubectl get secret redb-mydb -o jsonpath="{.data.port}" | base64 --decode
-
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.
-
From a pod within your cluster, use
redis-cli
to connect to your database.redis-cli -h <service_name> -p <port>
-
Enter the password you retrieved from the secret.
auth <password>
You are now connected to your database!