Skip to main content

Kubernetes: A Factory for Valkey

Kubernetes - This article is part of a series.
Part 8: This Article

Using Valkey for cache
#

As the official Valkey operator for Kubernetes is still in its early development stages at the time of writing, the next best way we can generalise the deployment of Valkey is to create a factory module which deploys the official Valkey Helm chart. This can then be used across multiple service modules.

Creating the factory module
#

Using the dendritic pattern, we can easily create a NixOS factory module to handle the deployment of the Valkey helm chart:

Note

See NixOS Documentation for more information

{
  self,
  inputs,
  ...
}:
{
  config.flake.factory.valkey =
    {
      namespace,
      values,
    }:
    {
      lib,
      pkgs,
      config,
      ...
    }:
    let
      chart = {
        name = "valkey";
        repo = "https://valkey.io/valkey-helm";
        version = "0.9.3";
        hash = "sha256-Ig2kNNiZka/DUSBHQB7fZq/+9sf6hrUeBveNolbxDvw=";
      };
      image = pkgs.dockerTools.pullImage {
        imageName = "valkey/valkey";
        imageDigest = "sha256:546304417feac0874c3dd576e0952c6bb8f06bb4093ea0c9ca303c73cf458f63";
        sha256 = "sha256-Fytwh9dNSRODr0ZsSaqIXGppqVF424C2TW47Uiv0ZWA=";
        finalImageTag = "9.0.1";
        arch = "amd64";
      };
    in
    {
      config = {
        services.k3s = {
          images = [ image ];
          autoDeployCharts = {
            "${namespace}-valkey" = chart // {
              targetNamespace = namespace;
              createNamespace = true;
              values = values // {
                image = {
                  repository = image.imageName;
                  tag = image.imageTag;
                };
              };
            };
          };
        };
      };
    };
}

Deploying valkey caches
#

Using the valkey factory method we can add individual deployments for use in complex service deployments:

{
  self,
  inputs,
  lib,
  ...
}:
{
  flake.modules.nixos.immich-valkey = self.factory.valkey {
    namespace = "immich";
    values = {
      resources = {
        requests.cpu = "20m";
        requests.memory = "64Mi";
        limits.cpu = "100m";
        limits.memory = "128Mi";
      };
      auth = {
        enabled = true;
        usersExistingSecret = "immich-secrets";
        aclUsers.default = {
          permissions = "~* &* +@all";
          passwordKey = "valkey-password";
        };
      };
    };
  };
}

Kubernetes - This article is part of a series.
Part 8: This Article