Skip to content

Commit f7e2b62

Browse files
committed
feat: k8s cluster deployment on digital ocean
0 parents  commit f7e2b62

File tree

13 files changed

+258
-0
lines changed

13 files changed

+258
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.terraform
2+
terraform.tfstate
3+
terraform.tfstate.backup
4+
terraform.tfstate.d

Makefile

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
deploy:
2+
$(eval workspace=$(shell terraform workspace new deploy-cluster))
3+
terraform workspace select deploy-cluster
4+
5+
terraform init ./deployments/staging/cluster
6+
terraform apply -auto-approve ./deployments/staging/cluster
7+
8+
terraform output kube_config > ~/.kube/config
9+
10+
deploy_helm_and_state:
11+
$(eval tf_host=$(shell terraform output host))
12+
$(eval tf_port=$(shell terraform output port))
13+
$(eval tf_database=$(shell terraform output database))
14+
$(eval tf_user=$(shell terraform output user))
15+
$(eval tf_password=$(shell terraform output password))
16+
17+
$(eval workspace=$(shell terraform workspace new helm_and_state))
18+
terraform workspace select helm_and_state
19+
20+
terraform init ./deployments/staging/helm_and_state
21+
terraform apply \
22+
-auto-approve \
23+
-var 'host=$(tf_host)' \
24+
-var 'port=$(tf_port)' \
25+
-var 'database=$(tf_database)' \
26+
-var 'user=$(tf_user)' \
27+
-var 'password=$(tf_password)' \
28+
./deployments/staging/helm_and_state
29+
30+
destroy:
31+
terraform workspace select deploy-cluster
32+
terraform init ./deployments/staging/cluster
33+
terraform destroy -force ./deployments/staging/cluster
34+
35+
destroy_helm_and_state:
36+
terraform workspace select helm
37+
terraform init ./helm
38+
terraform destroy -force ./helm
39+
40+
get_kube_config:
41+
terraform workspace select deploy-cluster
42+
terraform output kube_config > ~/.kube/config
43+
44+
helm_install_external_dns:
45+
helm install --name external-dns \
46+
--namespace kube-system \
47+
--set provider=cloudflare \
48+
--set source=ingress \
49+
--set source=service \
50+
--set cloudflare.apiKey= \
51+
--set cloudflare.email= \
52+
stable/external-dns
53+
54+
.PHONY: \
55+
deploy \
56+
deploy_helm_and_state \
57+
destroy \
58+
destroy_helm_and_state \
59+
get_kube_config \

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Terraform cluster deployment
2+
3+
## Overview
4+
Terraform script for deploying a cluster to Digital Ocean
5+
6+
Add your digital ocean token to `variables.tf`
7+
8+
## Prerequisites
9+
Install [teraform](https://www.terraform.io/)
10+
11+
### Step 1
12+
- Deploy k8s cluster
13+
```shell
14+
make deploy
15+
```
16+
This will deploy a k8s cluster on download the `kube_config`
17+
18+
### Step 2
19+
- Deploy helm and state
20+
```shell
21+
make deploy_helm_and_state
22+
```
23+
Running this step must be ran directly after Step 1 as it has dependancies on `terraform output`
24+
25+
### Step N
26+
- Destroy k8s cluster
27+
```shell
28+
make destroy
29+
```
30+
31+
### Beware of Dragons
32+
- Please run the steps in order!
33+
- Helm install doesn't seem to be working

deployments/staging/cluster/main.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
provider "digitalocean" {
2+
token = "${var.do_token}"
3+
}
4+
module "cluster" {
5+
source = "../../../modules/do-cluster"
6+
cluster_name = "staging"
7+
}
8+
output "kube_config" {
9+
value = "${module.cluster.kube_config}"
10+
}
11+
output "cluster_id" {
12+
value = "${module.cluster.cluster_id}"
13+
}
14+
output "host" {
15+
value = "${module.cluster.host}"
16+
}
17+
output "port" {
18+
value = "${module.cluster.port}"
19+
}
20+
output "database" {
21+
value = "${module.cluster.database}"
22+
}
23+
output "user" {
24+
value = "${module.cluster.user}"
25+
}
26+
output "password" {
27+
value = "${module.cluster.password}"
28+
}
29+
output "uri" {
30+
value = "${module.cluster.uri}"
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
variable "do_token" { default = "" }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
provider "postgresql" {
2+
host = "${var.host}"
3+
port = "${var.port}"
4+
database = "${var.database}"
5+
username = "${var.user}"
6+
password = "${var.password}"
7+
sslmode = "require"
8+
connect_timeout = 15
9+
}
10+
11+
module "database_tables" {
12+
source = "../../../modules/database-tables"
13+
}
14+
15+
provider "helm" {
16+
install_tiller = true
17+
service_account = "helm"
18+
}
19+
20+
provider "kubernetes" {
21+
}
22+
23+
module "cluster_helm" {
24+
source = "../../../modules/helm"
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "host" {}
2+
variable "port" {}
3+
variable "database" {}
4+
variable "user" {}
5+
variable "password" {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "postgresql_database" "terraform_backend" {
2+
name = "terraform_backend"
3+
lc_collate = "C"
4+
connection_limit = -1
5+
allow_connections = true
6+
}

modules/do-cluster/cluster.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "digitalocean_kubernetes_cluster" "main" {
2+
name = "${var.cluster_name}"
3+
region = "fra1"
4+
version = "1.15.3-do.2"
5+
6+
7+
node_pool {
8+
name = "api-go"
9+
size = "s-1vcpu-2gb"
10+
node_count = 3
11+
}
12+
13+
# management {
14+
# auto_repair = true
15+
# auto_upgrade = true
16+
# }
17+
}

modules/do-cluster/database.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "digitalocean_database_cluster" "pg" {
2+
name = "postgres-db"
3+
engine = "pg"
4+
version = "11"
5+
size = "db-s-1vcpu-1gb"
6+
region = "fra1"
7+
node_count = 1
8+
}

modules/do-cluster/outputs.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Outputs kubeconfig to connect to k8s cluster
2+
output "kube_config" {
3+
value = "${digitalocean_kubernetes_cluster.main.kube_config.0.raw_config}"
4+
}
5+
6+
output "cluster_id" {
7+
value = "${digitalocean_kubernetes_cluster.main.id}"
8+
}
9+
10+
# Output the details to connect to the DB
11+
output "host" {
12+
value = "${digitalocean_database_cluster.pg.host}"
13+
}
14+
output "port" {
15+
value = "${digitalocean_database_cluster.pg.port}"
16+
}
17+
output "database" {
18+
value = "${digitalocean_database_cluster.pg.database}"
19+
}
20+
output "user" {
21+
value = "${digitalocean_database_cluster.pg.user}"
22+
}
23+
output "password" {
24+
value = "${digitalocean_database_cluster.pg.password}"
25+
}
26+
output "uri" {
27+
value = "${digitalocean_database_cluster.pg.uri}"
28+
}

modules/do-cluster/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
variable "cluster_name" { default = "review" }

modules/helm/helm.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
resource "kubernetes_service_account" "helm" {
2+
metadata {
3+
name = "helm"
4+
namespace = "kube-system"
5+
}
6+
}
7+
8+
resource "kubernetes_cluster_role_binding" "helm" {
9+
metadata {
10+
name = "helm"
11+
}
12+
role_ref {
13+
api_group = "rbac.authorization.k8s.io"
14+
kind = "ClusterRole"
15+
name = "cluster-admin"
16+
}
17+
subject {
18+
kind = "User"
19+
name = "admin"
20+
api_group = "rbac.authorization.k8s.io"
21+
}
22+
subject {
23+
kind = "ServiceAccount"
24+
name = "helm"
25+
namespace = "kube-system"
26+
}
27+
subject {
28+
kind = "Group"
29+
name = "system:masters"
30+
api_group = "rbac.authorization.k8s.io"
31+
}
32+
}
33+
34+
# provider "helm" {
35+
# kubernetes {
36+
# config_path = "~/.kube/config"
37+
# }
38+
# install_tiller = true
39+
# service_account = "helm"
40+
# }

0 commit comments

Comments
 (0)