Created
March 6, 2019 21:06
-
-
Save santisaez/6731665fcf8b75125a2a392b880469e9 to your computer and use it in GitHub Desktop.
Example Terraform code to deploy a Kubernetes cluster at DigitalOcean
This file contains 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
// Create a Kubernetes cluster in DigitalOcean | |
resource "digitalocean_kubernetes_cluster" "prueba" { | |
name = "prueba" | |
region = "lon1" | |
version = "1.11.7-do.3" | |
node_pool { | |
name = "prueba-pool" | |
size = "s-1vcpu-2gb" | |
node_count = 1 | |
} | |
} | |
resource "local_file" "kubeconfig" { | |
content = "${digitalocean_kubernetes_cluster.prueba.kube_config.0.raw_config}" | |
filename = "kubeconfig.yaml" | |
} | |
// Initiate the Kubernetes provider with DigitalOcean cluster credentials, can be replaced by "export KUBECONFIG=kubeconfig.yaml" | |
provider "kubernetes" { | |
host = "${digitalocean_kubernetes_cluster.prueba.endpoint}" | |
client_certificate = "${base64decode(digitalocean_kubernetes_cluster.prueba.kube_config.0.client_certificate)}" | |
client_key = "${base64decode(digitalocean_kubernetes_cluster.prueba.kube_config.0.client_key)}" | |
cluster_ca_certificate = "${base64decode(digitalocean_kubernetes_cluster.prueba.kube_config.0.cluster_ca_certificate)}" | |
} | |
// Create a Pod running Nginx | |
resource "kubernetes_pod" "nginx" { | |
metadata { | |
name = "nginx-prueba" | |
labels { | |
app = "nginx" | |
} | |
} | |
spec { | |
container { | |
image = "nginx:latest" | |
name = "nginx" | |
port { | |
container_port = 80 | |
} | |
} | |
} | |
} | |
// Expose Nginx with a LoadBalancer service from DigitalOcean | |
resource "kubernetes_service" "nginx" { | |
metadata { | |
name = "nginx-prueba" | |
} | |
spec { | |
selector { | |
app = "${kubernetes_pod.nginx.metadata.0.labels.app}" | |
} | |
port { | |
port = 80 | |
target_port = 80 | |
} | |
// This resource is provided by the Cloud Controller Manager for DigitalOcean (https://github.com/digitalocean/digitalocean-cloud-controller-manager) | |
type = "LoadBalancer" | |
} | |
} | |
// Show the external IP address of the load-balancer | |
output "load_balancer_ip" { | |
value = "${kubernetes_service.nginx.load_balancer_ingress.0.ip}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment