Created
July 16, 2025 18:01
-
-
Save leshikus/8395b4144a6428c6814268824cb2b8b0 to your computer and use it in GitHub Desktop.
Setup grafana in kube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Setup local Kubernetes cluster with Grafana | |
hosts: localhost | |
vars: | |
arch: amd64 | |
cluster_name: kind-grafana | |
kubectl_url: "https://dl.k8s.io/release/{{ lookup('url', 'https://dl.k8s.io/release/stable.txt') }}/bin/linux/{{ arch }}/kubectl" | |
kind_url: "https://kind.sigs.k8s.io/dl/latest/kind-linux-{{ arch }}" | |
helm_script_url: "https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3" | |
do_restart: true | |
tasks: | |
- name: Install packages | |
become: true | |
package: | |
name: | |
- curl | |
- gnupg | |
- software-properties-common | |
state: present | |
- name: Download and install kubectl | |
get_url: | |
url: "{{ kubectl_url }}" | |
dest: /tmp/kubectl | |
mode: '0755' | |
register: kubectl_download | |
- name: Move kubectl to /usr/local/bin | |
become: true | |
copy: | |
src: /tmp/kubectl | |
dest: /usr/local/bin/kubectl | |
remote_src: yes | |
mode: '0755' | |
when: kubectl_download.changed | |
- name: Download and install kind | |
get_url: | |
url: "{{ kind_url }}" | |
dest: /tmp/kind | |
mode: '0755' | |
register: kind_download | |
- name: Move kind to /usr/local/bin | |
become: true | |
copy: | |
src: /tmp/kind | |
dest: /usr/local/bin/kind | |
remote_src: yes | |
mode: '0755' | |
when: kind_download.changed | |
- name: Install Helm if not present | |
shell: | | |
curl -fsSL -o get_helm.sh {{ helm_script_url }} | |
chmod u+x get_helm.sh | |
./get_helm.sh | |
args: | |
creates: /usr/local/bin/helm | |
- name: Write kind cluster config | |
copy: | |
dest: ./kind-config.yaml | |
content: | | |
kind: Cluster | |
apiVersion: kind.x-k8s.io/v1alpha4 | |
nodes: | |
- role: control-plane | |
- role: worker | |
- role: worker | |
- name: Get the cluster list | |
shell: > | |
kind get clusters | |
register: cluster_list | |
failed_when: false | |
changed_when: false | |
- set_fact: | |
cluster_exists: "{{ cluster_name in cluster_list.stdout }}" | |
- name: Delete cluster | |
shell: | | |
kind delete cluster --name {{ cluster_name }} | |
rm -f .kube/config | |
when: cluster_exists and do_restart | |
- name: Create kind cluster | |
shell: kind create cluster --name {{ cluster_name }} --config kind-config.yaml | |
args: | |
creates: ~/.kube/config | |
register: stdout | |
when: not cluster_exists or do_restart | |
- name: Wait for nodes to be ready | |
shell: kubectl get nodes | |
register: nodes_out | |
retries: 10 | |
delay: 5 | |
until: '"Ready" in nodes_out.stdout' | |
- name: Add Grafana Helm repo | |
shell: helm repo add grafana https://grafana.github.io/helm-charts | |
args: | |
creates: ~/.cache/helm/repository/grafana-index.yaml | |
- name: Update Helm repos | |
shell: helm repo update | |
- name: Save default Grafana values.yaml | |
shell: helm show values grafana/grafana > values.yaml | |
args: | |
creates: values.yaml | |
- name: Add HashiCorp GPG key | |
become: true | |
apt_key: | |
url: https://apt.releases.hashicorp.com/gpg | |
state: present | |
- name: Add HashiCorp repository | |
become: true | |
apt_repository: | |
repo: "deb [arch=amd64] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main" | |
state: present | |
- name: Install Terraform | |
become: true | |
apt: | |
name: terraform | |
state: latest | |
update_cache: yes | |
- name: Create Kube config dir | |
file: | |
path: ./kube-config | |
state: directory | |
- name: Create Kube config | |
copy: | |
dest: ./kube-config/main.tf | |
content: | | |
provider "helm" { | |
kubernetes = { | |
config_path = "~/.kube/config" | |
} | |
} | |
resource "helm_release" "grafana" { | |
name = "grafana" | |
repository = "https://grafana.github.io/helm-charts" | |
chart = "grafana" | |
namespace = "monitoring" | |
create_namespace = true | |
wait = true | |
} | |
- name: Init Terraform | |
shell: | | |
terraform init | |
terraform apply -auto-approve | |
args: | |
chdir: kube-config | |
- name: Get password | |
shell: | | |
kubectl get secret --namespace monitoring grafana -o jsonpath="{.data.admin-password}" | base64 -d | |
register: g_password | |
- name: Print Grafana password | |
debug: msg="password={{ g_password.stdout }}" | |
- name: Proxy Grafana port | |
shell: | | |
kill $(cat proxy.pid) || true | |
nohup kubectl port-forward -n monitoring svc/grafana 3000:80 1>port-forward.log 2>&1 & | |
echo $! >proxy.pid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment