commit d6c361ca0e5e5f1a6d8319117410e22500fffd54 Author: Alexander Svan Date: Sun Feb 25 10:19:47 2024 +0100 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4516c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Terraform +.terraform/ +terraform.* +.terraform.* + +# Nix +flake.lock diff --git a/README.md b/README.md new file mode 100644 index 0000000..7e4527a --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ + +# Kube Playground + +## Getting started + +Start a developer console in NixOS with all need dependencies + +```bash +nix develop +``` + +Run Terraform to deploy needed VMs + +First init + +```bash +terraform init +``` + +Then apply + + +```bash +terraform apply +``` + +## Misc + +Good extension for Openlens + +for the extension: go to File-->Extensions--> use `@alebcay/openlens-node-pod-menu` for url, hit install diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..206ec36 --- /dev/null +++ b/flake.nix @@ -0,0 +1,24 @@ +{ + description = "A flake for Terraform development"; + + inputs.nixpkgs.url = "nixpkgs/nixos-unstable"; + + outputs = { self, nixpkgs }: { + devShells.x86_64-linux.default = + let + system = "x86_64-linux"; + pkgs = import nixpkgs { + inherit system; + config.allowUnfree = true; + }; + in + pkgs.mkShell { + buildInputs = with pkgs; [ + terraform + cdrtools + kubectl + openlens + ]; + }; + }; +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..fcffc1a --- /dev/null +++ b/main.tf @@ -0,0 +1,91 @@ +terraform { + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = "0.7.6" + } + } +} + +provider "libvirt" { + uri = "qemu:///system" +} + +variable "vms" { + description = "Map of VMs to create" + type = map(object({ + name = string + memory = string + vcpu = number + })) + default = { + "vm1" = { + name = "kube-master" + memory = "2048" + vcpu = 1 + }, + "vm2" = { + name = "kube-worker-01" + memory = "2048" + vcpu = 2 + } + } +} + +resource "libvirt_volume" "base-kube-qcow2" { + name = "base-kube" + source = "/home/alex/ISOs/ubuntu-22.04-server-cloudimg-amd64.img" + format = "qcow2" +} + +resource "libvirt_volume" "kube-qcow2" { + for_each = var.vms + name = each.value.name + base_volume_id = libvirt_volume.base-kube-qcow2.id + size = 25 * 1024 * 1024 * 1024 + format = "qcow2" +} + +resource "libvirt_domain" "kube-vm" { + for_each = var.vms + name = each.value.name + memory = each.value.memory + vcpu = each.value.vcpu + + network_interface { + network_name = "default" + } + + disk { + volume_id = libvirt_volume.kube-qcow2[each.key].id + } + + cloudinit = libvirt_cloudinit_disk.commoninit.id +} + +resource "libvirt_cloudinit_disk" "commoninit" { + name = "commoninit.iso" + user_data = <<-EOF + #cloud-config + password: passw0rd + chpasswd: { expire: False } + ssh_pwauth: True + runcmd: + - swapoff -a + - sed -i '/ swap / s/^/#/' /etc/fstab + - apt-get update && apt-get install -y apt-transport-https curl + - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - + - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" + - apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io + - curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - + - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list + - apt-get update + - apt-get install -y kubelet kubeadm + EOF + network_config = <<-EOF + version: 2 + ethernets: + ens3: + dhcp4: true + EOF +} \ No newline at end of file