Skip to content

Commit

Permalink
readme updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
s4l1h committed Sep 24, 2019
1 parent bc91695 commit 96ba645
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 56 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform.tfvars
.terraform
.terraform*
*.tfstate.backup
*.tfstate
.vscode/
data/
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
## Create SSH key (or use your own key)
## How to use?

Copy **terraform.tfvars** from **sample** and fill all variables in **terraform.tfvars**

```
cp terraform.tfvars.sample terraform.tfvars
```

### Initalize and apply

These commands will create [k3s](https://k3s.io/) cluster.

```
sh create_ssh_key.sh
terraform init
terraform plan
terraform apply
```

I will update here.
### DNS module

if you want to use **cloudflare dns** module.
Please uncomment all lines related to Cloudflare in **dns.tf** and **terraform.tfvars** files.

### Create SSH key (or you can use your own key)

This command will create **id_rsa** and **id_rsa.pub** file in **./data** directory.

```
sh files/create_ssh_key.sh
```
16 changes: 12 additions & 4 deletions agents.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,37 @@ resource "scaleway_server" "agent" {


resource "null_resource" "join_node" {
depends_on = ["null_resource.copy_configs"]
depends_on = ["data.external.join_token"]

count = "${var.instance_agents_count}"
connection {
type = "ssh"
host = "${element(scaleway_server.agent.*.public_ip, count.index)}"
user = "${var.ssh_user}"
port = "${var.ssh_port}"
private_key = local.ssh_private
private_key = "${local.ssh_private}"
}

provisioner "remote-exec" {
inline = [
# "apt-get update",
# "DEBIAN_FRONTEND=noninteractive apt-get upgrade --yes",
# "DEBIAN_FRONTEND=noninteractive apt-get install open-iscsi --yes",
"${data.template_file.agent.rendered}"
]
}
}


data "template_file" "agent" {
template = "${file("templates/agent_install.sh")}"
template = "${file("files/agent_install.sh")}"
vars = {
server_ip = "${scaleway_server.master.public_ip}"
token = local.token
token = "${data.external.join_token.result.token}"
}
}

data "external" "join_token" {
depends_on = ["null_resource.copy_configs"]
program = ["./files/fetch-token.sh"]
}
9 changes: 0 additions & 9 deletions agents_dns.tf

This file was deleted.

18 changes: 18 additions & 0 deletions dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# module "dns" {
# source = "./dns"
# cloudflare_email = "${var.cloudflare_email}"
# cloudflare_token = "${var.cloudflare_token}"
# cloudflare_zone = "${var.cloudflare_zone}"

# master = "${scaleway_server.master}"
# agents = "${scaleway_server.agent}"
# prefix = "${var.prefix}"
# instance_agents_count = "${var.instance_agents_count}"
# }

# # Cloudflare
# variable "cloudflare_email" {}

# variable "cloudflare_token" {}

# variable "cloudflare_zone" {}
22 changes: 22 additions & 0 deletions dns/agents_dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "cloudflare_record" "agent" {
count = "${var.instance_agents_count}"
domain = "${var.cloudflare_zone}"
name = "${var.prefix}-agent-${count.index}"
value = "${element(var.agents.*.public_ip, count.index)}"
type = "A"
ttl = 1
}
resource "cloudflare_record" "agent_private" {
count = "${var.instance_agents_count}"
domain = "${var.cloudflare_zone}"
name = "${var.prefix}-agent-private-${count.index}"
value = "${element(var.agents.*.private_ip, count.index)}"
type = "A"
ttl = 1
}
output "agents_zone" {
value = ["${cloudflare_record.agent.*.hostname}"]
}
output "agents_zone_private" {
value = ["${cloudflare_record.agent_private.*.hostname}"]
}
5 changes: 5 additions & 0 deletions dns/dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Configure Cloudflare
provider "cloudflare" {
email = "${var.cloudflare_email}"
token = "${var.cloudflare_token}"
}
22 changes: 22 additions & 0 deletions dns/master_dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "cloudflare_record" "master" {
domain = "${var.cloudflare_zone}"
name = "${var.prefix}"
value = "${var.master.public_ip}"
type = "A"
ttl = 1
}

resource "cloudflare_record" "master_private" {
domain = "${var.cloudflare_zone}"
name = "${var.prefix}"
value = "${var.master.private_ip}"
type = "A"
ttl = 1
}

output "master_zone" {
value = "${cloudflare_record.master.hostname}"
}
output "master_zone_private" {
value = "${cloudflare_record.master_private.hostname}"
}
19 changes: 19 additions & 0 deletions dns/variables_dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Cloudflare
variable "cloudflare_email" {}

variable "cloudflare_token" {}

variable "cloudflare_zone" {}


variable "master" {}

variable "agents" {}
variable "instance_agents_count" {

}
variable "prefix" {

}


File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions files/fetch-token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -e
token=$(cat ./data/node-token)
# Trim
token=`echo $token`
echo "{\"token\":\"$token\"}"
File renamed without changes.
13 changes: 11 additions & 2 deletions master.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ resource "null_resource" "copy_configs" {
host = "${scaleway_server.master.public_ip}"
user = "${var.ssh_user}"
port = "${var.ssh_port}"
private_key = local.ssh_private
private_key = "${local.ssh_private}"
}

provisioner "remote-exec" {
inline = [
# "apt-get update",
# "DEBIAN_FRONTEND=noninteractive apt-get upgrade --yes",
# "DEBIAN_FRONTEND=noninteractive apt-get install open-iscsi --yes",
"${data.template_file.master.rendered}"
]
}
Expand All @@ -34,10 +37,16 @@ resource "null_resource" "copy_configs" {
provisioner "local-exec" {
command = "scp -i ${var.ssh_private_key_file} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${var.ssh_user}@${scaleway_server.master.public_ip}:/var/lib/rancher/k3s/server/node-token ./data/node-token"
}

provisioner "local-exec" {
when = "destroy"
command = "rm ./data/k3s.yaml;rm ./data/node-token"
on_failure = "continue"
}
}

data "template_file" "master" {
template = "${file("templates/server_install.sh")}"
template = "${file("files/server_install.sh")}"
vars = {
server_ip = "${scaleway_server.master.public_ip}"
}
Expand Down
7 changes: 0 additions & 7 deletions master_dns.tf

This file was deleted.

15 changes: 11 additions & 4 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ output "master_private_ip" {
value = "${scaleway_server.master.private_ip}"
}
#/etc/rancher/k3s/k3s.yaml
output "master_kubeconfig" {
value = fileexists("./data/k3s.yaml") ? file("./data/k3s.yaml") : ""
}
# output "master_kubeconfig" {
# value = fileexists("./data/k3s.yaml") ? file("./data/k3s.yaml") : ""
# }
#/var/lib/rancher/k3s/server/node-token
output "master_node_token" {
value = fileexists("./data/node-token") ? file("./data/node-token") : ""
value = "${data.external.join_token.result.token}"
}

output "agents_public_ip" {
value = ["${scaleway_server.agent.*.public_ip}"]
}

output "agents_private_ip" {
value = ["${scaleway_server.agent.*.private_ip}"]
}
6 changes: 0 additions & 6 deletions providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@ provider "scaleway" {
token = "${var.scw_token}"
region = "${var.region}"
}

# Configure Cloudflare
provider "cloudflare" {
email = "${var.cloudflare_email}"
token = "${var.cloudflare_token}"
}
3 changes: 2 additions & 1 deletion ssh.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
resource "scaleway_ssh_key" "k3s" {
key = local.ssh_public
depends_on = ["scaleway_server.master"]
key = "${local.ssh_public}"
}
22 changes: 13 additions & 9 deletions terraform.tfvars.sample
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ instance_agents_count = "2"



# Email for your Cloudflare account
cloudflare_email = "@@@@gmail.com"

# Cloudflare token
cloudflare_token = "TOKEN_HERE"

# Cloudflare zone
cloudflare_zone = "akmsoftware.co.uk"

#ssh user
ssh_user = "root"
ssh_public_key_file = "./data/id_rsa.pub"
ssh_private_key_file = "./data/id_rsa"



# # If you are going to use dns module you need to fill these variables up.

# # Email for your Cloudflare account
# cloudflare_email = "[email protected]"

# # Cloudflare token
# cloudflare_token = "token_here"

# # Cloudflare zone
# cloudflare_zone = "akmsoftware.co.uk"
13 changes: 2 additions & 11 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ variable "instance_image" {
}
}

# Cloudflare
variable "cloudflare_email" {}

variable "cloudflare_token" {}

variable "cloudflare_zone" {}

# SSH

variable "ssh_user" {
description = "SSH user name to use for remote exec connections,"
default = "root"
Expand All @@ -54,7 +46,6 @@ variable "ssh_private_key_file" {
}

locals {
ssh_public = fileexists(var.ssh_public_key_file) ? file(var.ssh_public_key_file) : ""
ssh_private = fileexists(var.ssh_private_key_file) ? file(var.ssh_private_key_file) : ""
token = fileexists("./data/node-token") ? trimspace(file("./data/node-token")) : ""
ssh_public = "${fileexists(var.ssh_public_key_file) ? trimspace(file(var.ssh_public_key_file)) : ""}"
ssh_private = "${fileexists(var.ssh_private_key_file) ? file(var.ssh_private_key_file) : ""}"
}

0 comments on commit 96ba645

Please sign in to comment.