Skip to content

Commit

Permalink
Microk8s support
Browse files Browse the repository at this point in the history
  • Loading branch information
solamarpreet committed Oct 11, 2022
1 parent 408c437 commit 1afb486
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 59 deletions.
19 changes: 19 additions & 0 deletions ansible/kubeconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUREekNDQWZlZ0F3SUJBZ0lVRUlPNGQ2T1BkMUdHNFBQOWRmU3lHM0JwNkZVd0RRWUpLb1pJaHZjTkFRRUwKQlFBd0Z6RVZNQk1HQTFVRUF3d01NVEF1TVRVeUxqRTRNeTR4TUI0WERUSXlNVEF4TURFM05USXpNMW9YRFRNeQpNVEF3TnpFM05USXpNMW93RnpFVk1CTUdBMVVFQXd3TU1UQXVNVFV5TGpFNE15NHhNSUlCSWpBTkJna3Foa2lHCjl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF2V3NLVDk0Y09OZ0RmS0hpbytsSUNQY1A2UlFTM0VoUHFQOGkKOFhkV0g0OWt1MjloRGNVMlEyUWdLVU1KTGY1RjBWOEZpY2F5RitwVFpVOVVzR0VpSU1VS1lrb1JNSWxBM0VpSApKWlpKa1k2Y0ZJYjZxY3NiR1k1WGtxcWVyTmorM1UvWnQvV0ZDTXNnY1pwTFRNeE9ieGs3OHpmYkM3YmliL3ZQCnI5VDhTRDU0Vm5YdE9kUzB3RmFXZUU2bXQxTyt6Qk0zUllNd3BqRVhzaUxjYk1EZXdoVW9PZ1gvRklqVjFGQS8KdGhIeFRvaWV5L1E4R1N6RjRVTUtyQW02VlZTdmYvV2J0VU83M0VXOVI1NGNtRDNHT3VOaHA0c1VPYThYK2MvdQprdktIbEdXVVc0cit0RUZZaTA0RlFmdHEycGtzN3BmQllFY2ZNTlZvWHIyeGk2blRpd0lEQVFBQm8xTXdVVEFkCkJnTlZIUTRFRmdRVUFaWFpQLzlVeFZmZ2x0UjRvQitSVC92M1NGZ3dId1lEVlIwakJCZ3dGb0FVQVpYWlAvOVUKeFZmZ2x0UjRvQitSVC92M1NGZ3dEd1lEVlIwVEFRSC9CQVV3QXdFQi96QU5CZ2txaGtpRzl3MEJBUXNGQUFPQwpBUUVBSjRFck1ZUXhjalp4Q0lVWUNYcks3ODVZaVBlb0t3M3IvQi94QjBnWHFRK0xrTE84SnFWZXMyd0lVTWxoCm9vMXhsRWowWUtVUmdVL0pkWWZuRjJ6ejg1QXBuOHVrTUh2MmYxTHE5c0lYclJ3WGR1b2hnaEVwc01ld3QxRHQKdFZaTGQ4WTFNaFpEMmplS2JJT2FEeEdQK1ZaL0dHeUhFOURvS0FydWdMTHVXTnhHTzlXdVc1bkVtSVBIWVRjUwpHdWJqUUNjWDlKNE5udVk3NDBoZ0U2ZGw1R2tZcWVadUtvZlNzVEhWYzNtS2N6TmFaQmV0ZXJJSXNUZ2VuVUdnCmZaUXkzeXc0NVdQMU5lQWVIQkNaNXdIQ2FzYWJpQ1g3TTZiSG44OHp6dXR5MUFRVlI4K1dLaUNqbHVqTGwvYm4KN0gzN3ZBWHdwMkVId1dla3NodjNlT2U4ZkE9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://10.0.0.11:16443
name: microk8s-cluster
contexts:
- context:
cluster: microk8s-cluster
user: admin
name: microk8s
current-context: microk8s
kind: Config
preferences: {}
users:
- name: admin
user:
token: Zjl2NXBpUVlvRVA3SUV5UFdXa1hqSGZ0cDl0RDJjN3B4cWRwSVp3a2FmOD0K

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---

- name: Initialising cluster
hosts: homelab
hosts: nodes
become: yes
tasks:
- name: Updating instances
Expand Down Expand Up @@ -67,13 +67,12 @@

- name: Linking Control Plane IP to DuckDNS
uri:
url: "{{ duckdns_url }}"
url: "{{ kube_duckdns_url }}"


- name: Rebooting
hosts: homelab
hosts: nodes
become: yes
tasks:
- name: Rebooting instances
reboot:

90 changes: 90 additions & 0 deletions ansible/playbooks/microk8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---

- name: Initialising cluster
hosts: nodes
become: yes
tasks:
- name: Updating instances
apt:
update_cache: yes
upgrade: dist

- name: Rebooting instances
reboot:

- name: Updating snaps
shell: snap refresh

- name: Rebooting instances
reboot:

- name: Installing microk8s
command: snap install microk8s --classic --channel=latest/stable

- name: Waiting for microk8s to start
command: microk8s status --wait-ready


- name: Configuring Master node
hosts: ocarm1
become: yes
tasks:
- name: Enable ingress addon
command: microk8s enable ingress

- name: Add DNS record
shell: "echo {{ hostvars['ocarm2']['private_ip'] }} ocarm2 | tee -a /etc/hosts"

- name: Copying worker node token
shell: microk8s add-node --format=short | grep -m 1 "microk8s.*"
register: nodetoken

- name: Store worker node token
set_fact:
token: "{{ nodetoken.stdout }}"


- name: Configuring Worker node
hosts: ocarm2
become: yes
tasks:
- name: Add DNS record
shell: "echo {{ hostvars['ocarm1']['private_ip'] }} ocarm1 | tee -a /etc/hosts"

- name: Joining cluster
command: "{{ hostvars['ocarm1']['token'] }} --worker"


- name: Finalising
hosts: ocarm1
become: yes
tasks:
- name: Sleep for 10 seconds
ansible.builtin.wait_for:
timeout: 10

- name: Adding role to master node
command: microk8s kubectl label node ocarm1 kubernetes.io/role=master

- name: Adding role to worker node
command: microk8s kubectl label node ocarm2 kubernetes.io/role=worker

- name: Generating kube config file
shell: microk8s config > /tmp/kubeconfig

- name: Fetching kube config file for local kubectl access
fetch:
src: /tmp/kubeconfig
dest: ../kubeconfig
flat: yes

- name: Linking Control Plane IP to DuckDNS
uri:
url: "{{ kube_duckdns_url }}"

- name: Rebooting
hosts: nodes
become: yes
tasks:
- name: Rebooting instances
reboot:
27 changes: 0 additions & 27 deletions ansible/playbooks/uninstall-k3s.yml

This file was deleted.

32 changes: 16 additions & 16 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions terraform/ansible.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
resource "local_file" "ansible_inventory" {
content = templatefile("${path.module}/templates/inventory.tftpl",
{ ocarm1_ip = oci_core_instance.ocarm1.public_ip,
ocarm1_name = oci_core_instance.ocarm1.display_name,
ocarm1_private_ip = oci_core_instance.ocarm1.private_ip,
ocarm1_name = oci_core_instance.ocarm1.display_name,
ocarm2_ip = oci_core_instance.ocarm2.public_ip,
ocarm2_private_ip = oci_core_instance.ocarm2.private_ip,
ocarm2_name = oci_core_instance.ocarm2.display_name,
ocamd1_ip = oci_core_instance.ocamd1.public_ip,
ocamd1_private_ip = oci_core_instance.ocamd1.private_ip,
ocamd1_name = oci_core_instance.ocamd1.display_name,

ansible_ssh_private_key_file = var.ansible_ssh_private_key_file
duckdns_url = var.duckdns_url
kube_duckdns_url = var.kube_duckdns_url
registry_duckdns_url = var.registry_duckdns_url
}
)

Expand Down
35 changes: 35 additions & 0 deletions terraform/compute-ampere.tf → terraform/compute.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ resource "oci_core_instance" "ocarm1" {
#Required
source_id = var.ampere_source_image_id
source_type = "image"
boot_volume_size_in_gbs = var.ampere_boot_volume_size
}
}

Expand Down Expand Up @@ -59,5 +60,39 @@ resource "oci_core_instance" "ocarm2" {
#Required
source_id = var.ampere_source_image_id
source_type = "image"
boot_volume_size_in_gbs = var.ampere_boot_volume_size
}
}

resource "oci_core_instance" "ocamd1" {

availability_domain = data.oci_identity_availability_domains.homelab_availability_domains.availability_domains[0].name
compartment_id = var.compartment_id
shape = "VM.Standard.E2.1.Micro"
display_name = "ocamd1"

shape_config {
memory_in_gbs = 1
ocpus = 1
}

metadata = {
ssh_authorized_keys = var.ssh_authorized_keys,
user_data = filebase64("${path.module}/scripts/init.sh")
}

create_vnic_details {
assign_public_ip = true
subnet_id = oci_core_subnet.homelab_public_subnet.id
assign_private_dns_record = true
private_ip = "10.0.0.13"
hostname_label = "ocamd1"
}

source_details {
#Required
source_id = var.amd_source_image_id
source_type = "image"
boot_volume_size_in_gbs = var.amd_boot_volume_size
}
}
2 changes: 1 addition & 1 deletion terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
provider "oci" {

config_file_profile= var.config_file_profile
}
5 changes: 5 additions & 0 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ output "public-ip-ocarm1" {
output "public-ip-ocarm2" {
value = oci_core_instance.ocarm2.public_ip
description = "Public IP for ocarm2"
}

output "public-ip-ocamd1" {
value = oci_core_instance.ocamd1.public_ip
description = "Public IP for ocamd1"
}
18 changes: 13 additions & 5 deletions terraform/templates/inventory.tftpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
[homelab]
${ocarm1_name} ansible_host=${ocarm1_ip}
${ocarm2_name} ansible_host=${ocarm2_ip}
[nodes]
${ocarm1_name} ansible_host=${ocarm1_ip} private_ip=${ocarm1_private_ip}
${ocarm2_name} ansible_host=${ocarm2_ip} private_ip=${ocarm2_private_ip}

[homelab:vars]
[registry]
${ocamd1_name} ansible_host=${ocamd1_ip} private_ip=${ocamd1_private_ip}

[kubernetes:children]
nodes
registry

[kubernetes:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=${ansible_ssh_private_key_file}
duckdns_url=${duckdns_url}
kube_duckdns_url=${kube_duckdns_url}
registry_duckdns_url=${registry_duckdns_url}
8 changes: 6 additions & 2 deletions terraform/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
ssh_authorized_keys = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINdAjFIVuEXPP+mUqyKcLV/eA7A9jjGvkKA8ZFp2xNXz sol@sol-PC"
region = "us-phoenix-1"
ampere_source_image_id = "ocid1.image.oc1.phx.aaaaaaaamtniveyy275y6d3hm37exftu76e2l7pdqhmpcuw6ean2xy44zywa"
region = "ap-hyderabad-1"
ampere_source_image_id = "ocid1.image.oc1.ap-hyderabad-1.aaaaaaaatrcrz26pphlblpduppycd2mkazocye7epuu5vxnuvf7wch43r6fa"
amd_source_image_id = "ocid1.image.oc1.ap-hyderabad-1.aaaaaaaaammbtmhmaozuu7gqqlyz3zftzfnvamc5n3paxv4qpyynf5obwzsa"
ampere_boot_volume_size = 70
amd_boot_volume_size = 50
ansible_ssh_private_key_file = "/home/sol/.ssh/root@oracle"
config_file_profile = "INDIA"
30 changes: 29 additions & 1 deletion terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ variable "region" {
default = "us-phoenix-1"
}

variable "config_file_profile" {
description = "The config profile to use"
type = string
default = "DEFAULT"
}

variable "ampere_source_image_id" {
description = "OCID of the ampere image"
type = string
}

variable "amd_source_image_id" {
description = "OCID of the amd image"
type = string
}

variable "ampere_boot_volume_size" {
description = "Size of the boot volume in GBs"
type = number
}

variable "amd_boot_volume_size" {
description = "Size of the boot volume in GBs"
type = number
}

variable "ssh_authorized_keys" {
description = "Public SSH key added to authorized_keys file of new instances"
type = string
Expand All @@ -25,7 +46,14 @@ variable "compartment_id" {
sensitive = true
}

variable "duckdns_url" {
variable "kube_duckdns_url" {
description = "DuckDNS url to configure DDNS hostname for k8s control plane"
default = "http://example.com"
type = string
sensitive = true
}

variable "registry_duckdns_url" {
description = "DuckDNS url to configure DDNS hostname for k8s control plane"
default = "http://example.com"
type = string
Expand Down
Loading

0 comments on commit 1afb486

Please sign in to comment.