Skip to content

Commit

Permalink
Added terraform for Vultr (#458)
Browse files Browse the repository at this point in the history
* Added terraform for Vultr

* Fixed terraform format
Fixed typo for container name

* Minor changes

Added ENABLE_PRIMITIVE option
Fixed md linter errors

* Fixed another md linter errors

Co-authored-by: Oleksii Titov <[email protected]>
  • Loading branch information
aot9 and Oleksii Titov authored Mar 30, 2022
1 parent 70f741c commit 7dbb46f
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
33 changes: 33 additions & 0 deletions terraform/vultr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Vultr deployment

## Requirements

* Vultr account
* API token
* `terraform`

## Deploy

```bash
export VULTR_API_KEY="Your Vultr API Key"
terraform init
terraform plan -var "key=<path_to_ssh_key>" -var "num_inst=<number of instances to create>"
terraform apply -var "key=<path_to_ssh_key>" -var "num_inst=<number of instances to create>"
```

## Destroy

To delete all the resources that were created run

```bash
terraform destroy
```

## Tips

Deploy script installs vnstat util that is useful for monitoring server network performance.
Example, get network statistics for the last 5 hours:

```bash
ssh root@ip vnstat -h 5
```
32 changes: 32 additions & 0 deletions terraform/vultr/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
terraform {
required_providers {
vultr = {
source = "vultr/vultr"
version = "2.10.0"
}
}
}

resource "vultr_ssh_key" "ssh_key" {
name = "my-ssh-key"
ssh_key = file("${var.key}.pub")
}

resource "vultr_instance" "my_instance" {
count = var.num_inst
plan = var.plan
region = var.region
app_id = var.app
ssh_key_ids = [vultr_ssh_key.ssh_key.id]

provisioner "remote-exec" {
script = "scripts/deploy.sh"

connection {
host = self.main_ip
private_key = file("${var.key}")
}
}
}


3 changes: 3 additions & 0 deletions terraform/vultr/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "ip" {
value = vultr_instance.my_instance[*].main_ip
}
11 changes: 11 additions & 0 deletions terraform/vultr/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /bin/bash

apt install -y vnstat

container_name="db1000n"
docker_cmd="docker run -d -it --rm --name=${container_name} -e ENABLE_PRIMITIVE=false --pull always ghcr.io/arriven/db1000n"

${docker_cmd}

echo "0 */2 * * * docker kill ${container_name} || true && ${docker_cmd}" >> cronjob
crontab cronjob
22 changes: 22 additions & 0 deletions terraform/vultr/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

variable "region" {
default = "icn" # Seoul, South Korea
}

variable "plan" {
default = "vc2-1c-1gb"
}

variable "app" {
description = "Docker Ubuntu 20.04"
default = "37"
}

variable "key" {
description = "Path to SSH key"
type = string
}

variable "num_inst" {
type = number
}

0 comments on commit 7dbb46f

Please sign in to comment.