Deployment

As a more complete example, let’s define some high-level variables and then split our module out into another file as we start to grow.

    default.nix
    
{ kubenix ? import ../../../.. }:
kubenix.evalModules.${builtins.currentSystem} {
  module = { kubenix, ... }: {
    # instead of defining everything inline, let's import it
    imports = [ ./module.nix ];

    # annotate the generated resources with a project name
    kubenix.project = "example";
    # define a target api version to validate output
    kubernetes.version = "1.24";
  };
}

Now we create a module which does a few related things:

  • create a Deployment
  • mount a ConfigMap into its pod
  • define a Service
    module.nix
    
{ kubenix, ... }: {
  imports = [ kubenix.modules.k8s ];

  kubernetes.resources = {
    deployments.nginx.spec = {
      replicas = 10;
      selector.matchLabels.app = "nginx";
      template = {
        metadata.labels.app = "nginx";
        spec = {
          securityContext.fsGroup = 1000;
          containers.nginx = {
            image = "nginx:1.25.1";
            imagePullPolicy = "IfNotPresent";
            volumeMounts = {
              "/etc/nginx".name = "config";
              "/var/lib/html".name = "static";
            };
          };
          volumes = {
            config.configMap.name = "nginx-config";
            static.configMap.name = "nginx-static";
          };
        };
      };
    };

    configMaps = {
      nginx-config.data."nginx.conf" = ''
        user nginx nginx;
        error_log /dev/stdout info;
        pid /dev/null;
        events {}
        http {
          access_log /dev/stdout;
          server {
            listen 80;
            index index.html;
            location / {
              root /var/lib/html;
            }
          }
        }
      '';

      nginx-static.data."index.html" = ''
        <html><body><h1>Hello from NGINX</h1></body></html>
      '';
    };

    services.nginx.spec = {
      selector.app = "nginx";
      ports = [{
        name = "http";
        port = 80;
      }];
    };
  };
}