mirror of
https://github.com/jpetazzo/container.training.git
synced 2026-07-25 15:56:29 +00:00
🖨️ Improve Terraform outputs and install metrics-server
Stage2 output should now be easier to copy-paste to a Google Spreadsheet. Add Helm support and use it to deploy metrics-server on each cluster.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
module "clusters" {
|
||||
source = "./modules/scaleway"
|
||||
source = "./modules/linode"
|
||||
for_each = local.clusters
|
||||
cluster_name = each.value.cluster_name
|
||||
min_nodes_per_pool = var.min_nodes_per_pool
|
||||
@@ -13,16 +13,17 @@ locals {
|
||||
clusters = {
|
||||
for i in range(101, 101 + var.how_many_clusters) :
|
||||
i => {
|
||||
cluster_name = format("%s-%03d", local.tag, i)
|
||||
kubeconfig_path = format("./stage2/kubeconfig.%03d", i)
|
||||
dashdash_kubeconfig = format("--kubeconfig=./stage2/kubeconfig.%03d", i)
|
||||
externalips_path = format("./stage2/externalips.%03d", i)
|
||||
cluster_name = format("%s-%03d", local.tag, i)
|
||||
kubeconfig_path = format("./stage2/kubeconfig.%03d", i)
|
||||
#dashdash_kubeconfig = format("--kubeconfig=./stage2/kubeconfig.%03d", i)
|
||||
externalips_path = format("./stage2/externalips.%03d", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "local_file" "stage2" {
|
||||
filename = "./stage2/main.tf"
|
||||
file_permission = "0644"
|
||||
content = templatefile(
|
||||
"./stage2.tmpl",
|
||||
{ clusters = local.clusters }
|
||||
@@ -30,16 +31,28 @@ resource "local_file" "stage2" {
|
||||
}
|
||||
|
||||
resource "local_file" "kubeconfig" {
|
||||
for_each = local.clusters
|
||||
filename = each.value.kubeconfig_path
|
||||
content = module.clusters[each.key].kubeconfig
|
||||
for_each = local.clusters
|
||||
filename = each.value.kubeconfig_path
|
||||
file_permission = "0600"
|
||||
content = module.clusters[each.key].kubeconfig
|
||||
}
|
||||
|
||||
resource "local_file" "externalips" {
|
||||
for_each = local.clusters
|
||||
filename = each.value.externalips_path
|
||||
file_permission = "0600"
|
||||
content = ""
|
||||
|
||||
provisioner "local-exec" {
|
||||
environment = {
|
||||
KUBECONFIG = local_file.kubeconfig[each.key].filename
|
||||
}
|
||||
|
||||
command = <<-EOT
|
||||
kubectl ${each.value.dashdash_kubeconfig} get nodes --watch \
|
||||
kubectl get nodes --watch \
|
||||
| grep --silent --line-buffered . \
|
||||
&& kubectl ${each.value.dashdash_kubeconfig} wait node --for=condition=Ready --all --timeout=10m \
|
||||
&& kubectl ${each.value.dashdash_kubeconfig} get nodes \
|
||||
&& kubectl wait node --for=condition=Ready --all --timeout=10m \
|
||||
&& kubectl get nodes \
|
||||
-o 'jsonpath={.items[*].status.addresses[?(@.type=="ExternalIP")].address}' > ${each.value.externalips_path}
|
||||
EOT
|
||||
}
|
||||
|
||||
@@ -14,39 +14,39 @@ provider "kubernetes" {
|
||||
config_path = "./kubeconfig.${index}"
|
||||
}
|
||||
|
||||
resource "kubernetes_namespace" "sshpod_${index}" {
|
||||
resource "kubernetes_namespace" "shpod_${index}" {
|
||||
provider = kubernetes.cluster_${index}
|
||||
metadata {
|
||||
name = "sshpod"
|
||||
name = "shpod"
|
||||
}
|
||||
}
|
||||
|
||||
resource "kubernetes_deployment" "sshpod_${index}" {
|
||||
resource "kubernetes_deployment" "shpod_${index}" {
|
||||
provider = kubernetes.cluster_${index}
|
||||
metadata {
|
||||
name = "sshpod"
|
||||
namespace = kubernetes_namespace.sshpod_${index}.metadata.0.name
|
||||
name = "shpod"
|
||||
namespace = kubernetes_namespace.shpod_${index}.metadata.0.name
|
||||
}
|
||||
spec {
|
||||
selector {
|
||||
match_labels = {
|
||||
app = "sshpod"
|
||||
app = "shpod"
|
||||
}
|
||||
}
|
||||
template {
|
||||
metadata {
|
||||
labels = {
|
||||
app = "sshpod"
|
||||
app = "shpod"
|
||||
}
|
||||
}
|
||||
spec {
|
||||
service_account_name = "sshpod"
|
||||
service_account_name = "shpod"
|
||||
container {
|
||||
image = "jpetazzo/sshpod"
|
||||
name = "sshpod"
|
||||
image = "jpetazzo/shpod"
|
||||
name = "shpod"
|
||||
env {
|
||||
name = "PASSWORD"
|
||||
value = random_string.sshpod_${index}.result
|
||||
value = random_string.shpod_${index}.result
|
||||
}
|
||||
lifecycle {
|
||||
post_start {
|
||||
@@ -58,11 +58,11 @@ resource "kubernetes_deployment" "sshpod_${index}" {
|
||||
resources {
|
||||
limits = {
|
||||
cpu = "2"
|
||||
memory = "100M"
|
||||
memory = "500M"
|
||||
}
|
||||
requests = {
|
||||
cpu = "100m"
|
||||
memory = "100M"
|
||||
memory = "250M"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,17 +71,18 @@ resource "kubernetes_deployment" "sshpod_${index}" {
|
||||
}
|
||||
}
|
||||
|
||||
resource "kubernetes_service" "sshpod_${index}" {
|
||||
resource "kubernetes_service" "shpod_${index}" {
|
||||
provider = kubernetes.cluster_${index}
|
||||
metadata {
|
||||
name = "sshpod"
|
||||
namespace = kubernetes_namespace.sshpod_${index}.metadata.0.name
|
||||
name = "shpod"
|
||||
namespace = kubernetes_namespace.shpod_${index}.metadata.0.name
|
||||
}
|
||||
spec {
|
||||
selector = {
|
||||
app = "sshpod"
|
||||
app = "shpod"
|
||||
}
|
||||
port {
|
||||
name = "ssh"
|
||||
port = 22
|
||||
target_port = 22
|
||||
node_port = 32222
|
||||
@@ -90,18 +91,18 @@ resource "kubernetes_service" "sshpod_${index}" {
|
||||
}
|
||||
}
|
||||
|
||||
resource "kubernetes_service_account" "sshpod_${index}" {
|
||||
resource "kubernetes_service_account" "shpod_${index}" {
|
||||
provider = kubernetes.cluster_${index}
|
||||
metadata {
|
||||
name = "sshpod"
|
||||
namespace = kubernetes_namespace.sshpod_${index}.metadata.0.name
|
||||
name = "shpod"
|
||||
namespace = kubernetes_namespace.shpod_${index}.metadata.0.name
|
||||
}
|
||||
}
|
||||
|
||||
resource "kubernetes_cluster_role_binding" "sshpod_${index}" {
|
||||
resource "kubernetes_cluster_role_binding" "shpod_${index}" {
|
||||
provider = kubernetes.cluster_${index}
|
||||
metadata {
|
||||
name = "sshpod"
|
||||
name = "shpod"
|
||||
}
|
||||
role_ref {
|
||||
api_group = "rbac.authorization.k8s.io"
|
||||
@@ -110,25 +111,54 @@ resource "kubernetes_cluster_role_binding" "sshpod_${index}" {
|
||||
}
|
||||
subject {
|
||||
kind = "ServiceAccount"
|
||||
name = "sshpod"
|
||||
namespace = "sshpod"
|
||||
name = "shpod"
|
||||
namespace = "shpod"
|
||||
}
|
||||
}
|
||||
|
||||
resource "random_string" "sshpod_${index}" {
|
||||
resource "random_string" "shpod_${index}" {
|
||||
length = 6
|
||||
special = false
|
||||
upper = false
|
||||
}
|
||||
|
||||
output "ssh_${index}" {
|
||||
value = format(
|
||||
"ssh -l %s -p %s %s # password=%s",
|
||||
"k8s",
|
||||
"32222",
|
||||
split(" ", file("./externalips.${index}"))[0],
|
||||
random_string.sshpod_${index}.result
|
||||
)
|
||||
provider "helm" {
|
||||
alias = "cluster_${index}"
|
||||
kubernetes {
|
||||
config_path = "./kubeconfig.${index}"
|
||||
}
|
||||
}
|
||||
|
||||
resource "helm_release" "metrics_server_${index}" {
|
||||
provider = helm.cluster_${index}
|
||||
repository = "https://charts.bitnami.com/bitnami"
|
||||
chart = "metrics-server"
|
||||
name = "metrics-server"
|
||||
namespace = "metrics-server"
|
||||
create_namespace = true
|
||||
set {
|
||||
name = "apiService.create"
|
||||
value = "true"
|
||||
}
|
||||
set {
|
||||
name = "extraArgs.kubelet-insecure-tls"
|
||||
value = "true"
|
||||
}
|
||||
set {
|
||||
name = "extraArgs.kubelet-preferred-address-types"
|
||||
value = "InternalIP"
|
||||
}
|
||||
}
|
||||
|
||||
%{ endfor ~}
|
||||
|
||||
output "ip_addresses_of_nodes" {
|
||||
value = join("\n", [
|
||||
%{ for index, cluster in clusters ~}
|
||||
join("\t", concat(
|
||||
[ random_string.shpod_${index}.result, "ssh -l k8s -p 32222" ],
|
||||
split(" ", file("./externalips.${index}"))
|
||||
)),
|
||||
%{ endfor ~}
|
||||
])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user