kubenix/README.md

103 lines
3.3 KiB
Markdown
Raw Normal View History

2022-08-11 23:07:20 -04:00
# kubenix
Kubernetes management with Nix
2017-11-11 11:52:17 +01:00
2022-08-11 23:47:39 -04:00
<p align="center" style="margin: 2em auto;">
<img src="./docs/static/logo.svg" alt="nixos logo in kubernetes blue" width="350"/>
2022-08-11 23:47:39 -04:00
</p>
2017-11-11 11:52:17 +01:00
> **WARN**: this is a work in progress, expect breaking [changes](./CHANGELOG.md)
2022-08-11 23:07:20 -04:00
## Usage
2017-11-11 11:52:17 +01:00
2023-06-12 00:01:53 -04:00
A minimal example `flake.nix` (build with `nix build`):
2022-08-14 22:31:44 -04:00
```nix
{
inputs.kubenix.url = "github:hall/kubenix";
outputs = {self, kubenix, ... }@inputs: let
system = "x86_64-linux";
in {
packages.${system}.default = (kubenix.evalModules.${system} {
module = { kubenix, ... }: {
imports = [ kubenix.modules.k8s ];
2022-08-14 22:31:44 -04:00
kubernetes.resources.pods.example.spec.containers.nginx.image = "nginx";
};
}).config.kubernetes.result;
};
}
```
Or, if you're not using flakes, a `default.nix` file (build with `nix-build`):
2022-08-14 21:48:45 -04:00
```nix
{ kubenix ? import (builtins.fetchGit {
url = "https://github.com/hall/kubenix.git";
rev = "main";
2022-08-14 21:48:45 -04:00
}) }:
(kubenix.evalModules.x86_64-linux {
2022-08-14 22:31:44 -04:00
module = {kubenix, ... }: {
imports = [ kubenix.modules.k8s ];
2022-08-14 22:31:44 -04:00
kubernetes.resources.pods.example.spec.containers.nginx.image = "nginx";
2022-08-14 21:48:45 -04:00
};
}).config.kubernetes.result
```
2022-08-14 22:31:44 -04:00
Either way the JSON manifests will be written to `./result`.
See the [examples](https://kubenix.org/examples/pod) for more.
2022-08-11 23:07:20 -04:00
2022-08-15 20:34:03 -04:00
## CLI
While kubenix is compatible with just about any deployment system, there's a simple builtin CLI which can:
2022-08-15 20:34:03 -04:00
- show a diff, prompt for confirmation, then apply
- prune removed resources
- pipe manifests through [vals](https://github.com/helmfile/vals) for the ability to inject secrets without writing them to the nix store
2022-08-15 20:34:03 -04:00
To configure this, override the default package, passing the arguments of [evalModules](https://nixos.org/manual/nixpkgs/stable/#module-system-lib-evalModules).
2022-08-15 20:34:03 -04:00
```nix
{
kubenix = inputs.kubenix.packages.${pkgs.system}.default.override {
module = import ./cluster;
# optional; pass custom values to the kubenix module
specialArgs = { flake = self; };
};
}
```
2022-08-15 20:34:03 -04:00
Then apply the resources with
nix run '.#kubenix'
which will print a diff and prompt for confirmation:
```diff
diff -N -u -I ' kubenix/hash: ' -I ' generation: ' /tmp/LIVE-2503962153/apps.v1.Deployment.default.home-assistant /tmp/MERGED-231044561/apps.v1.Deployment.default.home-assistant
--- /tmp/LIVE-2503962153/apps.v1.Deployment.default.home-assistant 2023-07-06 23:33:29.841771295 -0400
+++ /tmp/MERGED-231044561/apps.v1.Deployment.default.home-assistant 2023-07-06 23:33:29.842771296 -0400
@@ -43,7 +43,7 @@
spec:
automountServiceAccountToken: true
containers:
- - image: homeassistant/home-assistant:2023.5
+ - image: homeassistant/home-assistant:2023.6
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
apply? [y/N]:
```
2022-08-15 20:34:03 -04:00
> **HINT**: use ` --help` for more commands
2022-08-16 00:58:23 -04:00
Optionally, write the resources to `./result/manifests.json`:
2022-08-15 20:34:03 -04:00
nix build '.#kubenix'
2022-08-15 20:34:03 -04:00
2022-08-11 23:07:20 -04:00
## Attribution
2017-11-11 11:52:17 +01:00
This project was forked from [GTrunSec](https://github.com/GTrunSec/kubenix), which was forked from [xtruder](https://github.com/xtruder/kubenix), with commits incorporated from [blaggacao](https://github.com/blaggacao/kubenix).
2017-11-11 11:52:17 +01:00
2022-08-11 23:07:20 -04:00
Logo is a mishmash of the [Kubernetes wheel](https://github.com/kubernetes/kubernetes/blob/master/logo/logo.svg) and the [NixOs snowflake](https://github.com/NixOS/nixos-artwork/blob/master/logo/white.svg).