From ae1b2660dc697cef20382da50a038bd4ee6a9998 Mon Sep 17 00:00:00 2001 From: Dave Demlow Date: Wed, 29 Nov 2023 07:40:45 -0500 Subject: [PATCH] deploy microk8s with portainer agent --- simple_vm_deploy_microk8s.yml | 128 ++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 simple_vm_deploy_microk8s.yml diff --git a/simple_vm_deploy_microk8s.yml b/simple_vm_deploy_microk8s.yml new file mode 100644 index 0000000..5ee75ef --- /dev/null +++ b/simple_vm_deploy_microk8s.yml @@ -0,0 +1,128 @@ +--- +- name: Simple vm deploy microk8s single node #edit vmname variable - use -l filter to specify cluster vs. full inventory + hosts: edge + vars: + - vmname: microk8s- + connection: local + gather_facts: false + strategy: host_pinned # free #allows each cluster to start next task before all clusters have finished current task + environment: #if set here - hypercore modules will automatically use this for each remote cluster - avoiding need to specify cluster_instance for each test + SC_HOST: "https://{{ inventory_hostname }}" + SC_USERNAME: "{{ scale_user }}" + SC_PASSWORD: "{{ scale_pass }}" + + tasks: + - name: generate 5 digit random character password using lower case ans set as variable named ran5 + ansible.builtin.set_fact: + ran5: "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=5') }}" + + + - name: Ubuntu20_04 template - Ubuntu 20.04 - import if not present # used to clone and cloud-init target VM + scale_computing.hypercore.vm_import: + cluster_instance: + host: "https://{{inventory_hostname }}" + username: "{{scale_user}}" + password: "{{scale_pass}}" + vm_name: ubuntu20_04 + http_uri: + path: 'https://github.com/ddemlow/RestAPIExamples/raw/master/ubuntu20_04-cloud-init' + file_name: ubuntu20_04-cloud-init.xml +# until: ubuntu20_04.msg is search("import complete") #doesnt' check if vm already exists + retries: 5 + delay: 1 + ignore_errors: false # import errors are not uncommon depdending on network connection to smb / https source + register: ubuntu20_04 + + + - name: Clone and configure ad hoc "{{ vmname }} {{ran5}}" + scale_computing.hypercore.vm_clone: + cluster_instance: + host: "https://{{inventory_hostname }}" + username: "{{scale_user}}" + password: "{{scale_pass}}" + vm_name: "{{vmname}}{{ran5}}" + source_vm_name: ubuntu20_04 + tags: + - temp + cloud_init: + user_data: | + #cloud-config + password: "password" + chpasswd: { expire: False } + ssh_pwauth: True + ssh_authorized_keys: # Add your ssh public key for publickey authentication + - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDihWWhjoPj8KVLtdLDwNJQ71zi9An0iUFjefRWu2Eju ddemlow@scalecomputing.com + disable_root: false # allow ssh root login + ssh_pwauth: True + ssh_import_id: gh:ddemlow + apt: {sources: {docker.list: {source: 'deb [arch=amd64] https://download.docker.com/linux/ubuntu $RELEASE stable', keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88}}} + packages: [snapd, qemu-guest-agent, ansible, git, unzip] + snap: + commands: + 00: snap install microk8s + bootcmd: + - [ sh, -c, 'sudo echo GRUB_CMDLINE_LINUX="nomodeset" >> /etc/default/grub' ] + - [ sh, -c, 'sudo echo GRUB_GFXPAYLOAD_LINUX="1024x768" >> /etc/default/grub' ] + - [ sh, -c, 'sudo echo GRUB_DISABLE_LINUX_UUID=true >> /etc/default/grub' ] + - [ sh, -c, 'sudo update-grub' ] + runcmd: + - [ systemctl, restart, --no-block, qemu-guest-agent ] + - snap install microk8s --classic + # Add the default user to the microk8s group + - adduser ubuntu microk8s + - chown -f -R ubuntu ~/.kube + + # Enable MicroK8s addons + - microk8s enable dns dashboard storage host-access + + # Wait for MicroK8s to become available + - microk8s status --wait-ready + + # Make kubectl available to the default user + - snap alias microk8s.kubectl kubectl + + #helm repo add + - microk8s helm registry login --username='robot-edgelabs-clients' --password='LWb7bsa9msWphOy8brbXu6XJUlzXhfSV' registry.edgelabs.ai + - microk8s kubectl create secret docker-registry regcred --docker-server=registry.edgelabs.ai --docker-username=robot-edgelabs-clients --docker-password='LWb7bsa9msWphOy8brbXu6XJUlzXhfSV' + - microk8s helm install ai-sensor --set api.key='id1756317079|IWz602oCKaOuwBlhwEo8' oci://registry.edgelabs.ai/charts/ai-sensor + + #install portainer agent and register + - PORTAINER_EDGE_ID=$(hostname) + - 'curl https://downloads.portainer.io/ee2-19/portainer-edge-agent-setup.sh | bash -s -- "$PORTAINER_EDGE_ID" "aHR0cHM6Ly8yMC44OC4yMi4yMjc6OTQ0M3wyMC44OC4yMi4yMjc6ODAwMHwydXByOUtuYTd6ZHBNWExNMm9meDNubHZEOHh2THpLVjN5WnlXM1lsWFdvPXww" "1" "" "EDGE_ASYNC=1,PORTAINER_GROUP=3"' + - echo "cloud-init complete" > /dev/tty + write_files: + - content: "{{ inventory_hostname }}" + path: /clusterip.txt + meta_data: | + dsmode: local + local-hostname: "{{ vmname }}{{ ran5 }}" + + - name: Disk desired configuration for "{{ vmname }}{{ran5}}" + scale_computing.hypercore.vm_disk: + cluster_instance: + host: "https://{{inventory_hostname }}" + username: "{{scale_user}}" + password: "{{scale_pass}}" + vm_name: "{{ vmname }}{{ran5}}" + items: + - disk_slot: 0 + type: virtio_disk + size: "{{ '300 GB' | human_to_bytes }}" # 50GB | human to bytes results in 53.7GB VSD in Hypercore + state: present + + - name: Vm desired configuration and state for "{{ vmname }}{{ran5}}" + scale_computing.hypercore.vm_params: + cluster_instance: + host: "https://{{inventory_hostname }}" + username: "{{scale_user}}" + password: "{{scale_pass}}" + vm_name: "{{vmname}}{{ran5}}" + memory: "{{ '4 GB' | human_to_bytes }}" + description: + tags: + - ansible + - "{{ site_name }}" + - ansible_group__microk8s # this will create tag used by hypercore inventory plugin when executing towards VM hosts + - SERIAL + vcpu: 4 + power_state: start