Skip to content

Instantly share code, notes, and snippets.

@andrewsomething
Last active December 12, 2018 23:19
Show Gist options
  • Save andrewsomething/6e4add8cbb4309b93aac419b3de65d61 to your computer and use it in GitHub Desktop.
Save andrewsomething/6e4add8cbb4309b93aac419b3de65d61 to your computer and use it in GitHub Desktop.
Terraform + DigitalOcean Kubernetes = <3
provider "digitalocean" {}
// Create a cluster on DigitalOcean
resource "digitalocean_kubernetes_cluster" "example" {
name = "example"
region = "nyc1"
version = "1.12.3-do.1"
node_pool {
name = "wokers"
size = "s-2vcpu-2gb"
node_count = 3
}
}
// Initiate the Kubernetes provider w/ DigitalOcean cluster creds
provider "kubernetes" {
host = "${digitalocean_kubernetes_cluster.example.endpoint}"
client_certificate = "${base64decode(digitalocean_kubernetes_cluster.example.kube_config.0.client_certificate)}"
client_key = "${base64decode(digitalocean_kubernetes_cluster.example.kube_config.0.client_key)}"
cluster_ca_certificate = "${base64decode(digitalocean_kubernetes_cluster.example.kube_config.0.cluster_ca_certificate)}"
}
// Create an Nginx deployment
resource "kubernetes_pod" "nginx" {
metadata {
name = "nginx-example"
labels {
App = "nginx"
}
}
spec {
container {
image = "nginx:1.15.7-alpine"
name = "example"
port {
container_port = 80
}
}
}
}
// Expose Nginx w/ a LoadBalancer service
resource "kubernetes_service" "nginx" {
metadata {
name = "nginx-example"
}
spec {
selector {
App = "${kubernetes_pod.nginx.metadata.0.labels.App}"
}
port {
port = 80
target_port = 80
}
type = "LoadBalancer"
}
}
output "lb_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