docs reset

This commit is contained in:
Bryton Hall 2022-08-11 23:07:20 -04:00
parent 43371dba54
commit b874fce232
10 changed files with 225 additions and 28 deletions

View file

@ -0,0 +1,28 @@
# Example: kubernetes nginx deployment
A simple example creating kubernetes nginx deployment and associated docker
image
## Usage
### Building and applying kubernetes configuration
```
nix eval -f ./. --json result | kubectl apply -f -
```
### Building and pushing docker images
```
nix run -f ./. pushDockerImages -c copy-docker-images
```
### Running tests
Test will spawn vm with kubernetes and run test script, which checks if everyting
works as expected.
```
nix build -f ./. test-script
cat result | jq '.'
```

View file

@ -0,0 +1,59 @@
{
evalModules,
registry,
}: let
# evaluated configuration
inherit
((evalModules {
module = {kubenix, ...}: {
imports = [
kubenix.modules.testing
./module.nix
];
# commonalities
kubenix.project = "nginx-deployment-example";
docker.registry.url = registry;
kubernetes.version = "1.21";
testing = {
tests = [./test.nix];
docker.registryUrl = "";
# testing commonalities for tests that exhibit the respective feature
common = [
{
features = ["k8s"];
options = {
kubernetes.version = "1.20";
};
}
];
};
};
}))
config
;
in {
inherit config;
# config checks
checks = config.testing.success;
# TODO: e2e test
# test = config.testing.result;
# nixos test script for running the test
test-script = config.testing.testsByName.nginx-deployment.script;
# genreated kubernetes List object
inherit (config.kubernetes) generated;
# JSON file you can deploy to kubernetes
inherit (config.kubernetes) result;
# Exported docker images
images = config.docker.export;
# script to push docker images to registry
pushDockerImages = config.docker.copyScript;
}

View file

@ -0,0 +1,20 @@
{
dockerTools,
nginx,
}:
dockerTools.buildLayeredImage {
name = "nginx";
contents = [nginx];
extraCommands = ''
mkdir -p etc
chmod u+w etc
echo "nginx:x:1000:1000::/:" > etc/passwd
echo "nginx:x:1000:nginx" > etc/group
'';
config = {
Cmd = ["nginx" "-c" "/etc/nginx/nginx.conf"];
ExposedPorts = {
"80/tcp" = {};
};
};
}

View file

@ -0,0 +1,69 @@
{
config,
lib,
pkgs,
kubenix,
...
}:
with lib; let
nginx = pkgs.callPackage ./image.nix {};
in {
imports = with kubenix.modules; [k8s docker];
docker.images.nginx.image = nginx;
kubernetes.resources.deployments.nginx = {
spec = {
replicas = 10;
selector.matchLabels.app = "nginx";
template = {
metadata.labels.app = "nginx";
spec = {
securityContext.fsGroup = 1000;
containers.nginx = {
image = config.docker.images.nginx.path;
imagePullPolicy = "IfNotPresent";
volumeMounts."/etc/nginx".name = "config";
volumeMounts."/var/lib/html".name = "static";
};
volumes.config.configMap.name = "nginx-config";
volumes.static.configMap.name = "nginx-static";
};
};
};
};
kubernetes.resources.configMaps.nginx-config.data."nginx.conf" = ''
user nginx nginx;
daemon off;
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;
}
}
}
'';
kubernetes.resources.configMaps.nginx-static.data."index.html" = ''
<html><body><h1>Hello from NGINX</h1></body></html>
'';
kubernetes.resources.services.nginx = {
spec = {
ports = [
{
name = "http";
port = 80;
}
];
selector.app = "nginx";
};
};
}

View file

@ -0,0 +1,33 @@
{
lib,
pkgs,
kubenix,
test,
...
}:
with lib; {
imports = [kubenix.modules.test ./module.nix];
test = {
name = "nginx-deployment";
description = "Test testing nginx deployment";
script = ''
@pytest.mark.applymanifest('${test.kubernetes.resultYAML}')
def test_nginx_deployment(kube):
"""Tests whether nginx deployment gets successfully created"""
kube.wait_for_registered(timeout=30)
deployments = kube.get_deployments()
nginx_deploy = deployments.get('nginx')
assert nginx_deploy is not None
status = nginx_deploy.status()
assert status.readyReplicas == 10
# TODO: implement those kind of checks from the host machine into the cluster
# via port forwarding, prepare all runtimes accordingly
# ${pkgs.curl}/bin/curl http://nginx.default.svc.cluster.local | grep -i hello
'';
};
}