Use Terraform to deploy juju charm on microk8s

Docker https://developer.hashicorp.com/terraform/tutorials/docker-get-started/docker-build
Miscrok8s https://mikelynchgames.com/software-development/getting-started-with-local-kubernetes-developing-microservices-with-microk8s/

Controller:micro

Model:dev

App:zinc

Unit:zinc/0

App:prometheus

Unit:prometheus/0

App:parca

Unit:parca/0

App:hello-kubecon

Unit:hello-kubecon/0

App:grafana

Unit:grafana/0

Charm:grafana-k8s

Charm:hello-kubecon

Charm:parca-k8s

Charm:prometheus-k8s

Charm:zinc-k8s

Juju on microk8s

Juju https://discourse.charmhub.io/t/introducing-the-juju-terraform-provider/6939

  • juju/microk8s install
$ sudo snap install microk8s --channel=1.32-strict/stable ... $ sudo snap install juju --channel=3.6/stable ...
  • juju/microk8s setup
$ sudo microk8s enable hostpath-storage ... $ sudo juju controllers Use --refresh option with this command to see the latest information. Controller Model User Access Cloud/Region Models Nodes HA Version overlord* - admin superuser localhost/localhost 1 - - (unknown) $ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system calico-kube-controllers-5947598c79-brrfz 1/1 Running 0 27s kube-system calico-node-m8vnf 1/1 Running 0 16s kube-system coredns-79b94494c7-r4lq4 1/1 Running 0 11s kube-system hostpath-provisioner-c778b7559-fr9kx 1/1 Running 0 4s $ sudo juju bootstrap microk8s micro Creating Juju controller "micro" on microk8s/localhost Bootstrap to Kubernetes cluster identified as microk8s/localhost Creating k8s resources for controller "controller-micro" Starting controller pod Bootstrap agent now started Contacting Juju controller at 10.152.183.100 to verify accessibility... Bootstrap complete, controller "micro" is now available in namespace "controller-micro" Now you can run juju add-model <model-name> to create a new model to deploy k8s workloads. $ sudo juju controllers Use --refresh option with this command to see the latest information. Controller Model User Access Cloud/Region Models Nodes HA Version micro* - admin superuser microk8s/localhost 1 1 - 3.6.4 overlord - admin superuser localhost/localhost 1 - - (unknown) $ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE controller-micro controller-0 3/3 Running 0 2m21s controller-micro modeloperator-58798bccc-hk9hj 1/1 Running 0 59s kube-system calico-kube-controllers-5947598c79-brrfz 1/1 Running 0 4m2s kube-system calico-node-m8vnf 1/1 Running 0 3m51s kube-system coredns-79b94494c7-r4lq4 1/1 Running 0 3m46s kube-system hostpath-provisioner-c778b7559-fr9kx 1/1 Running 0 3m39s $ sudo juju add-model test Added 'test' model on microk8s/localhost with credential 'microk8s' for user 'admin' $ sudo juju switch test $ sudo juju models Controller: micro Model Cloud/Region Type Status Units Access Last connection controller microk8s/localhost kubernetes available 1 admin just now test* microk8s/localhost kubernetes available 5 admin 4 seconds ago $ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE controller-micro controller-0 3/3 Running 0 17m controller-micro modeloperator-78645f9fb5-8s9sq 1/1 Running 0 17m test modeloperator-7c47595567-nwl8q 1/1 Running 0 19s kube-system calico-kube-controllers-5947598c79-qs68c 1/1 Running 0 31m kube-system calico-node-fwbxl 1/1 Running 0 31m kube-system coredns-79b94494c7-qjwjd 1/1 Running 0 31m kube-system hostpath-provisioner-c778b7559-6sngm 1/1 Running 0 31m
  • microk8s setup
$ sudo usermod -a -G snap_microk8s $USER $ sudo chown -R $USER ~/.kube $ newgrp microk8s $ alias kubectl='KUBE_EDITOR=vi microk8s kubectl' $ source <(kubectl completion bash)
  • terraform install
$ sudo snap install terraform --classic ...
  • terraform apply (Remember to update the juju provider version in the YAML file to the latest one.)
### Grab the Terraform file $ wget -qO k8s-example.tf https://gist.githubusercontent.com/fourdollars/fdab97c7da29a2dd70f46a726daa9dfd/raw/0db6a002569ee5dd90977a52d2145c47702a8462/k8s-example.tf ### Initialise the provider $ terraform init ### Run Terraform Plan, check the output $ sudo terraform plan ### Deploy the resources, skip the approval $ sudo terraform apply -auto-approve $ sudo juju switch dev $ sudo juju status --relations Model Controller Cloud/Region Version SLA Timestamp dev micro microk8s/localhost 3.6.4 unsupported 13:57:30Z App Version Status Scale Charm Channel Rev Address Exposed Message grafana 9.5.3 active 1 grafana-k8s latest/stable 138 10.152.183.34 no hello-kubecon active 1 hello-kubecon latest/stable 19 10.152.183.232 no parca 0.18.0-800a6bb7 active 1 parca-k8s latest/stable 248 10.152.183.112 no prometheus 2.52.0 active 1 prometheus-k8s latest/stable 232 10.152.183.162 no zinc ... active 1 zinc-k8s latest/stable 176 10.152.183.199 no Unit Workload Agent Address Ports Message grafana/0* active idle 10.1.98.237 hello-kubecon/0* active idle 10.1.98.236 parca/0* active idle 10.1.98.230 prometheus/0* active executing 10.1.98.238 zinc/0* active idle 10.1.98.229 Integration provider Requirer Interface Type Message grafana:grafana grafana:grafana grafana_peers peer grafana:replicas grafana:replicas grafana_replicas peer parca:metrics-endpoint prometheus:metrics-endpoint prometheus_scrape regular prometheus:grafana-source grafana:grafana-source grafana_datasource regular prometheus:prometheus-peers prometheus:prometheus-peers prometheus_peers peer zinc:grafana-dashboard grafana:grafana-dashboard grafana_dashboard regular zinc:metrics-endpoint prometheus:metrics-endpoint prometheus_scrape regular zinc:profiling-endpoint parca:profiling-endpoint parca_scrape regular zinc:zinc-peers zinc:zinc-peers zinc_peers peer $ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE controller-micro controller-0 3/3 Running 0 48m controller-micro modeloperator-78645f9fb5-8s9sq 1/1 Running 0 48m dev grafana-0 3/3 Running 0 25m dev hello-kubecon-0 2/2 Running 0 26m dev modeloperator-df478b697-4q9fz 1/1 Running 0 26m dev parca-0 2/2 Running 0 26m dev prometheus-0 2/2 Running 0 24m dev zinc-0 2/2 Running 0 26m kube-system calico-kube-controllers-5947598c79-qs68c 1/1 Running 0 62m kube-system calico-node-fwbxl 1/1 Running 0 62m kube-system coredns-79b94494c7-qjwjd 1/1 Running 0 62m kube-system hostpath-provisioner-c778b7559-6sngm 1/1 Running 0 62m ### Destroy the resources, skip the approval $ sudo terraform destroy -auto-approve