From 0b4e5f1e27cf63989295954385a310de4a514da2 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 13 Jul 2024 18:14:41 +0200 Subject: [PATCH] more stuff --- README.md | 17 ++++ ansible/debian.yml | 2 + ansible/roles/base-config/tasks/main.yml | 9 +++ ansible/roles/k8s-base/defaults/main.yml | 1 + .../k8s-base/files/containerd-config.toml | 3 + ansible/roles/k8s-base/handlers/main.yml | 4 + ansible/roles/k8s-base/tasks/main.yml | 62 +++++++++++++++ create-vm.sh | 26 +++++-- debian-image/create_image.sh | 78 ------------------- debian-image/reset.sh | 16 ---- debian-image/setup.sh | 48 ------------ init-vm.sh | 3 - init.sh | 4 +- install-vms-ansible.sh | 0 shell.nix | 2 +- 15 files changed, 122 insertions(+), 153 deletions(-) create mode 100644 README.md create mode 100644 ansible/roles/base-config/tasks/main.yml create mode 100644 ansible/roles/k8s-base/defaults/main.yml create mode 100644 ansible/roles/k8s-base/files/containerd-config.toml create mode 100644 ansible/roles/k8s-base/handlers/main.yml create mode 100644 ansible/roles/k8s-base/tasks/main.yml delete mode 100755 debian-image/create_image.sh delete mode 100755 debian-image/reset.sh delete mode 100755 debian-image/setup.sh delete mode 100644 init-vm.sh mode change 100644 => 100755 init.sh mode change 100644 => 100755 install-vms-ansible.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..61b8d47 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# vmlab + +## create debian vms + +``` +./init.sh + +sudo ./create-vm.sh debian-k8s-00 +sudo ./create-vm.sh debian-k8s-01 +sudo ./create-vm.sh debian-k8s-02 +sudo ./create-vm.sh debian-k8s-03 +sudo ./create-vm.sh debian-k8s-04 +``` + +``` +./install-vms-ansible.sh +``` diff --git a/ansible/debian.yml b/ansible/debian.yml index 2504044..792eea6 100644 --- a/ansible/debian.yml +++ b/ansible/debian.yml @@ -4,3 +4,5 @@ become: true roles: - update + - base-config + - k8s-base diff --git a/ansible/roles/base-config/tasks/main.yml b/ansible/roles/base-config/tasks/main.yml new file mode 100644 index 0000000..647e39a --- /dev/null +++ b/ansible/roles/base-config/tasks/main.yml @@ -0,0 +1,9 @@ +- name: Change hostname + ansible.builtin.hostname: + name: "{{ hostname }}" +- name: Install core packages + ansible.builtin.apt: + state: present + name: "{{ item }}" + with_items: + - gpg diff --git a/ansible/roles/k8s-base/defaults/main.yml b/ansible/roles/k8s-base/defaults/main.yml new file mode 100644 index 0000000..16f7976 --- /dev/null +++ b/ansible/roles/k8s-base/defaults/main.yml @@ -0,0 +1 @@ +cni_plugins_version: "v1.4.0" diff --git a/ansible/roles/k8s-base/files/containerd-config.toml b/ansible/roles/k8s-base/files/containerd-config.toml new file mode 100644 index 0000000..5936f9a --- /dev/null +++ b/ansible/roles/k8s-base/files/containerd-config.toml @@ -0,0 +1,3 @@ +[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc] + [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] + SystemdCgroup = true diff --git a/ansible/roles/k8s-base/handlers/main.yml b/ansible/roles/k8s-base/handlers/main.yml new file mode 100644 index 0000000..5735b5e --- /dev/null +++ b/ansible/roles/k8s-base/handlers/main.yml @@ -0,0 +1,4 @@ +- name: "Containerd config changed" + ansible.builtin.service: + name: containerd + state: reloaded diff --git a/ansible/roles/k8s-base/tasks/main.yml b/ansible/roles/k8s-base/tasks/main.yml new file mode 100644 index 0000000..db2f779 --- /dev/null +++ b/ansible/roles/k8s-base/tasks/main.yml @@ -0,0 +1,62 @@ +# ==================== +# https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/ +- name: Add K8s key + ansible.builtin.apt_key: + url: "https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key" + keyring: /etc/apt/keyrings/kubernetes-apt-keyring.gpg +- name: Add K8s apt repo + ansible.builtin.apt_repository: + repo: "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /" + filename: kubernetes +- name: Install K8s tools + ansible.builtin.apt: + name: + - kubelet + - kubeadm + - kubectl +- name: Hold kubeadm + ansible.builtin.dpkg_selections: + name: "{{ item }}" + selection: hold + loop: + - kubelet + - kubeadm + - kubectl +# ==================== +# https://github.com/containerd/containerd/blob/main/docs/getting-started.md +# https://docs.docker.com/engine/install/ubuntu/ +- name: Add docker key + ansible.builtin.command: + cmd: "curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc" + creates: /etc/apt/keyrings/docker.asc +- name: Add Docker apt repo + ansible.builtin.apt_repository: + repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu jammy stable" + filename: docker +- name: Install containerd/runc + ansible.builtin.apt: + name: containerd.io +- name: Configure containerd to use systemd cgroups + ansible.builtin.copy: + src: "containerd-config.toml" + dest: "/etc/containerd/config.toml" + mode: "u=r,g=r,o=r" + notify: + - "Containerd config changed" +- name: Create /opt/cni/bin + ansible.builtin.file: + path: /opt/cni/bin + state: directory + mode: "u=rwx,g=rx,o=rx" +- name: Download CNI plugins + ansible.builtin.get_url: + url: "https://github.com/containernetworking/plugins/releases/download/{{ cni_plugins_version }}/cni-plugins-linux-amd64-{{ cni_plugins_version }}.tgz" + dest: "/tmp/cni-plugins-linux-amd64-{{ cni_plugins_version }}.tgz" +- name: Install CNI plugins + ansible.builtin.command: + cmd: "tar Cxzvf /opt/cni/bin /tmp/cni-plugins-linux-amd64-{{ cni_plugins_version }}.tgz" + creates: /opt/cni/bin/tap +- name: Start contained + ansible.builtin.service: + state: started + name: containerd \ No newline at end of file diff --git a/create-vm.sh b/create-vm.sh index deec6f1..6402006 100755 --- a/create-vm.sh +++ b/create-vm.sh @@ -1,18 +1,34 @@ -#!/usr/bin/bash +#!/usr/bin/env bash set -eu -NAME="$1" +NAME="${1:?Must pass the name}" + +if [ "$(whoami)" != "root" ]; then + echo "script must be run as root!" +fi mkdir -p tmp mkdir -p vm-state # https://mop.koeln/blog/creating-a-local-debian-vm-using-cloud-init-and-libvirt/ # > DO NOT DOWNLOAD THE GENERICCLOUD IMAGE -IMG=debian-12-generic-amd64.qcow2 +IMG_DOWNLOADED=debian-12-generic-amd64.qcow2 +IMG=debian-12-generic-amd64-bigger.qcow2 + +if ! [ -f "tmp/$IMG_DOWNLOADED" ]; then + curl -L -o "tmp/$IMG_DOWNLOADED" https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2 +fi if ! [ -f "tmp/$IMG" ]; then - curl -L -o "tmp/$IMG" https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2 + cp "tmp/$IMG_DOWNLOADED" "tmp/$IMG" + echo "INCREASE THE SIZE OF THE IMAGE!!!" + echo "sudo qemu-img resize tmp/$IMG 30G" + echo "sudo modprobe nbd max_part=10" + echo "sudo qemu-nbd -c /dev/nbd0 tmp/$IMG_DOWNLOADED" + echo "sudo gparted /dev/nbd0" + echo "sudo qemu-nbd -d /dev/nbd0" + exit 1 fi DISK="vm-state/$NAME.qcow2" @@ -47,4 +63,4 @@ ip=$(virsh domifaddr "$NAME" | grep ipv4 | awk '{print $4}' | cut -d/ -f1) echo "IP: $ip" -echo "$ip" >> vm-state/inventory.ini +echo "$ip hostname=$NAME" >> vm-state/inventory.ini diff --git a/debian-image/create_image.sh b/debian-image/create_image.sh deleted file mode 100755 index 731a736..0000000 --- a/debian-image/create_image.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env bash - -set -eu - -# https://mvallim.github.io/kubernetes-under-the-hood/documentation/create-linux-image.html - -SCRIPT=$(dirname "$0") -BUILD="$SCRIPT/build" -IMAGE="$BUILD/debian-image.raw" - -mkdir -p "$BUILD" - -if ! [ -f "$IMAGE" ]; then - # Create a 30GB disk - dd \ - if=/dev/zero \ - of="$IMAGE" \ - bs=1 \ - count=0 \ - seek=32212254720 \ - status=progress - - sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | sudo fdisk "$IMAGE" -o # clear the in memory partition table -n # new partition -p # primary partition -1 # partition number 1 - # default - start at beginning of disk -+512M # 512 MB boot parttion -n # new partition -p # primary partition -2 # partion number 2 - # default, start immediately after preceding partition - # default, extend partition to end of disk -a # make a partition bootable -1 # bootable partition is partition 1 -- /dev/loop0p1 -p # print the in-memory partition table -w # write the partition table -q # and we're done -EOF -else - echo "INFO: Skipping disk creation" -fi - -if ! [ -e "/dev/loop0" ]; then - sudo losetup -fP "$IMAGE" - sudo losetup -a - - sudo fdisk -l /dev/loop0 - sudo mkfs.ext4 /dev/loop0p1 # /boot - sudo mkfs.ext4 /dev/loop0p2 # / -else - echo "INFO: Skipping loop device setup" -fi - - -mkdir -p "$BUILD/chroot" -sudo mount /dev/loop0p2 "$BUILD/chroot/" - -if ! [ -d "$BUILD/chroot/bin" ]; then - sudo debootstrap \ - --arch=amd64 \ - --variant=minbase \ - --components "main" \ - --include "ca-certificates,cron,iptables,isc-dhcp-client,libnss-myhostname,ntp,ntpdate,rsyslog,ssh,sudo,dialog,whiptail,man-db,curl,dosfstools,e2fsck-static" \ - bullseye \ - "$BUILD/chroot" \ - http://deb.debian.org/debian/ -else - echo "INFO: Skipping debian bootstrap" -fi - -sudo mount --bind /dev "$BUILD/chroot/dev" -sudo mount --bind /run "$BUILD/chroot/run" - -sudo cp "$SCRIPT/setup.sh" "$BUILD/chroot/usr/local/bin" - -sudo chroot "$BUILD/chroot" /usr/local/bin/setup.sh diff --git a/debian-image/reset.sh b/debian-image/reset.sh deleted file mode 100755 index 7a11631..0000000 --- a/debian-image/reset.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env bash - -set -eu - -SCRIPT=$(dirname "$0") -BUILD="$SCRIPT/build" - -sudo umount "$BUILD/chroot" || true - -sudo umount "$BUILD/chroot/dev/pts" || true -sudo umount "$BUILD/chroot/dev" || true -sudo umount "$BUILD/chroot/sys" || true -sudo umount "$BUILD/chroot/proc" || true -sudo umount "$BUILD/chroot/run" || true - -sudo rm -rf "$BUILD" diff --git a/debian-image/setup.sh b/debian-image/setup.sh deleted file mode 100755 index 46352b9..0000000 --- a/debian-image/setup.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash -# ^^ not compatible on purpose, this only runs inside debian - -function info { - echo "INFO DEB:" "$@" -} - -export PATH="/usr/local/bin:/usr/bin:/bin" -export HOME=/root -export LC_ALL=C - -info "Hello from debian!" - -info "Setting up mounts" - -mount none -t proc /proc -mount none -t sysfs /sys -mount none -t devpts /dev/pts - -info "Configuring the system" - -echo "debian-image" > /etc/hostname -cat < /etc/apt/sources.list -deb http://deb.debian.org/debian/ bullseye main contrib non-free -deb-src http://deb.debian.org/debian/ bullseye main contrib non-free - -deb http://deb.debian.org/debian/ bullseye-updates main contrib non-free -deb-src http://deb.debian.org/debian/ bullseye-updates main contrib non-free - -deb http://deb.debian.org/debian-security bullseye-security main -deb-src http://deb.debian.org/debian-security bullseye-security main -EOF - -cat < /etc/fstab -# /etc/fstab: static file system information. -# -# Use 'blkid' to print the universally unique identifier for a -# device; this may be used with UUID= as a more robust way to name devices -# that works even if disks are added and removed. See fstab(5). -# -# -/dev/sda2 / ext4 errors=remount-ro 0 1 -/dev/sda1 /boot ext4 defaults 0 2 -EOF - -apt-get update -apt-get install -y apt-utils -apt-get install -y systemd-sysv diff --git a/init-vm.sh b/init-vm.sh deleted file mode 100644 index 1b134ee..0000000 --- a/init-vm.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -ssh-keygen -A \ No newline at end of file diff --git a/init.sh b/init.sh old mode 100644 new mode 100755 index bc88fc2..1d039b6 --- a/init.sh +++ b/init.sh @@ -2,6 +2,6 @@ mkdir -p vm-state -cat >vm-state/inventory-ini <vm-state/inventory.ini < { } }: pkgs.mkShell { - packages = with pkgs; [ debootstrap ansible ansible-lint ]; + packages = with pkgs; [ debootstrap ansible ansible-lint gparted ]; }