Skip to content

Commit 0a2b07c

Browse files
committed
initial commit
0 parents  commit 0a2b07c

File tree

13 files changed

+658
-0
lines changed

13 files changed

+658
-0
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# Created by https://www.gitignore.io/api/terraform
3+
4+
### Terraform ###
5+
# Local .terraform directories
6+
**/.terraform/*
7+
8+
# .tfstate files
9+
*.tfstate
10+
*.tfstate.*
11+
12+
# Crash log files
13+
crash.log
14+
15+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
16+
# .tfvars files are managed as part of configuration and so should be included in
17+
# version control.
18+
#
19+
# example.tfvars
20+
21+
# Ignore override files as they are usually used to override resources locally and so
22+
# are not checked in
23+
override.tf
24+
override.tf.json
25+
*_override.tf
26+
*_override.tf.json
27+
28+
# Include override files you do wish to add to version control using negated pattern
29+
#
30+
# !example_override.tf
31+
32+
33+
# End of https://www.gitignore.io/api/terraform

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Kubernetes on Scaleway with Rancher and Terraform
2+
3+
Deploy Rancher and Kubernetes on Scaleway using Terraform (with DNS on Cloudflare).
4+
5+
This terraform is based on the [DO quickstart deployment of Rancher](https://github.com/rancher/quickstart/tree/master/do)
6+
7+
## Overview
8+
9+
![overview](assets/overview.png)
10+
11+
## Prerequisites
12+
13+
* Prepare a [Scaleway](https://www.scaleway.com/) account
14+
* Prepare a [Cloudflare](https://www.cloudflare.com/) account
15+
* Setup at least 1 zone in [Cloudflare](https://www.cloudflare.com/)
16+
* You can get a free domain at services like [Freenom}(https://www.freenom.com/)
17+
* Install [terraform](https://www.terraform.io/intro/getting-started/install.html)
18+
* Install [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
19+
20+
## Jump Start
21+
22+
**!!WARNING!!** This will deploy resources to Scaleway and be sure that they are going to cost you money.
23+
24+
### Set up the tfvars
25+
26+
`rancherserver/terraform.tfvars`
27+
28+
Be sure to set the following variables:
29+
30+
* scw_token
31+
* Your Scaleway Secret Token
32+
* scw_org
33+
* Your Scaleway Organization ID
34+
* admin_password
35+
* The password you're going to use for the Rancher Server
36+
* rancher_server_url
37+
* The URL you will use for your Rancher Server. This terraform is going to setup a rancher subdomain to your cloudflare zone. So if your zone is example.com , this terraform will create an A record to resolve rancher.example.com .
38+
* cloudflare_email
39+
* Your Email for Cloudflare
40+
* cloudflare_token
41+
* Your Cloudflare API token
42+
* cloudflare_zone
43+
* Your Cloudflare zone
44+
45+
`rancheragent/terraform.tfvars`
46+
47+
Be sure to set the following variables:
48+
49+
* rancher_server_address
50+
* The address for your Rancher Server. It can either be an IP or domain name but I would recommend the domain name you set previously.
51+
* count_agent_all_nodes
52+
* This is how many nodes to create that have all the roles(etcd, controlplae, worker). If you're just experimenting with the Kubernetes cluster, having one of this will be enough.
53+
* count_agent_etcd_nodes
54+
* This is how many nodes to create that have the etcd role
55+
* count_agent_controlplane_nodes
56+
* This is how many nodes to create that have the controlplane role
57+
* count_agent_worker_nodes
58+
* This is how many nodes to create that have the worker role
59+
60+
### Deploy the Rancher Server resources
61+
62+
```bash
63+
cd rancherserver
64+
terraform apply
65+
```
66+
67+
### Deploy the Rancher Agent resources
68+
69+
```bash
70+
cd rancheragent
71+
terraform apply
72+
```
73+
74+
## Check it out
75+
76+
![rancher](assets/rancher.png)
77+
78+
![k8scluster](assets/k8scluster.png)
79+
80+
## Clean up
81+
82+
**Destroy the Rancher Agent and related resources**
83+
```bash
84+
cd rancheragent
85+
terraform apply
86+
```
87+
88+
**Destroy the Rancher Server and related resources**
89+
```bash
90+
cd rancherserver
91+
terraform destroy
92+
```

assets/k8scluster.png

161 KB
Loading

assets/overview.png

143 KB
Loading

assets/rancher.png

344 KB
Loading

rancheragent/dns.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
provider "cloudflare" {
2+
email = "${var.cloudflare_email}"
3+
token = "${var.cloudflare_token}"
4+
}
5+
6+
variable "cloudflare_email" {}
7+
8+
variable "cloudflare_token" {}
9+
10+
variable "cloudflare_zone" {}
11+
12+
# resource "cloudflare_record" "rancher_worker_nodes" {
13+
# count = "${var.count_agent_worker_nodes}"
14+
# domain = "${var.cloudflare_zone}"
15+
# name = "default"
16+
# value = "${scaleway_server.rancheragent_worker.*.public_ip[count.index]}"
17+
# type = "A"
18+
# ttl = 3600
19+
# }
20+
21+
# resource "cloudflare_record" "rancher_all_nodes" {
22+
# count = "${var.count_agent_all_nodes}"
23+
# domain = "${var.cloudflare_zone}"
24+
# name = "default"
25+
# value = "${scaleway_server.rancheragent_all.*.public_ip[count.index]}"
26+
# type = "A"
27+
# ttl = 3600
28+
# }

rancheragent/files/userdata_agent

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/bin/bash -x
2+
export curlimage=appropriate/curl
3+
export jqimage=stedolan/jq
4+
export rancher_server_host='${server_address}'
5+
6+
if [ `command -v curl` ]; then
7+
curl -sL https://releases.rancher.com/install-docker/${docker_version_agent}.sh | sh
8+
elif [ `command -v wget` ]; then
9+
wget -qO- https://releases.rancher.com/install-docker/${docker_version_agent}.sh | sh
10+
fi
11+
12+
for image in $curlimage $jqimage; do
13+
until docker inspect $image > /dev/null 2>&1; do
14+
docker pull $image
15+
sleep 2
16+
done
17+
done
18+
19+
while true; do
20+
docker run --rm $curlimage -sLk https://$rancher_server_host/ping && break
21+
sleep 5
22+
done
23+
24+
# Login
25+
while true; do
26+
27+
LOGINRESPONSE=$(docker run \
28+
--rm \
29+
$curlimage \
30+
-s "https://$rancher_server_host/v3-public/localProviders/local?action=login" -H 'content-type: application/json' --data-binary '{"username":"admin","password":"${admin_password}"}' --insecure)
31+
LOGINTOKEN=$(echo $LOGINRESPONSE | docker run --rm -i $jqimage -r .token)
32+
33+
if [ "$LOGINTOKEN" != "null" ]; then
34+
break
35+
else
36+
sleep 5
37+
fi
38+
done
39+
40+
# Get the Agent Image from the rancher server
41+
while true; do
42+
AGENTIMAGE=$(docker run \
43+
--rm \
44+
$curlimage \
45+
-sLk \
46+
-H "Authorization: Bearer $LOGINTOKEN" \
47+
"https://$rancher_server_host/v3/settings/agent-image" | docker run --rm -i $jqimage -r '.value')
48+
49+
if [ -n "$AGENTIMAGE" ]; then
50+
break
51+
else
52+
sleep 5
53+
fi
54+
done
55+
56+
until docker inspect $AGENTIMAGE > /dev/null 2>&1; do
57+
docker pull $AGENTIMAGE
58+
sleep 2
59+
done
60+
61+
# Create cluster
62+
CLUSTERRESPONSE=$(docker run --rm --net=host $curlimage -s "https://$rancher_server_host/v3/cluster" -H 'content-type: application/json' -H "Authorization: Bearer $LOGINTOKEN" --data-binary '{"type":"cluster","rancherKubernetesEngineConfig":{"addonJobTimeout":30,"ignoreDockerVersion":true,"sshAgentAuth":false,"type":"rancherKubernetesEngineConfig","authentication":{"type":"authnConfig","strategy":"x509"},"network":{"type":"networkConfig","plugin":"canal"},"ingress":{"type":"ingressConfig","provider":"nginx"},"services":{"type":"rkeConfigServices","kubeApi":{"podSecurityPolicy":false,"type":"kubeAPIService"},"etcd":{"snapshot":false,"type":"etcdService","extraArgs":{"heartbeat-interval":500,"election-timeout":5000}}}},"name":"${cluster_name}"}' --insecure)
63+
64+
# Extract clusterid to use for generating the docker run command
65+
CLUSTERID=`echo $CLUSTERRESPONSE | docker run --rm -i $jqimage -r .id`
66+
67+
# Generate registrationtoken
68+
docker run --rm --net=host $curlimage -s "https://$rancher_server_host/v3/clusterregistrationtoken" -H 'content-type: application/json' -H "Authorization: Bearer $LOGINTOKEN" --data-binary '{"type":"clusterRegistrationToken","clusterId":"'$CLUSTERID'"}' --insecure
69+
70+
# Test if cluster is created
71+
while true; do
72+
CLUSTERID=$(docker run \
73+
--rm \
74+
$curlimage \
75+
-sLk \
76+
-H "Authorization: Bearer $LOGINTOKEN" \
77+
"https://$rancher_server_host/v3/clusters?name=${cluster_name}" | docker run --rm -i $jqimage -r '.data[].id')
78+
79+
if [ -n "$CLUSTERID" ]; then
80+
break
81+
else
82+
sleep 5
83+
fi
84+
done
85+
86+
# Get role flags from hostname
87+
ROLEFLAG=`hostname | awk -F'-' '{ print $NF }'`
88+
if [[ "$ROLEFLAG" == "all" ]]; then
89+
ROLEFLAG="all-roles"
90+
fi
91+
92+
# Get token
93+
# Test if cluster is created
94+
while true; do
95+
AGENTCMD=$(docker run \
96+
--rm \
97+
$curlimage \
98+
-sLk \
99+
-H "Authorization: Bearer $LOGINTOKEN" \
100+
"https://$rancher_server_host/v3/clusterregistrationtoken?clusterId=$CLUSTERID" | docker run --rm -i $jqimage -r '.data[].nodeCommand' | head -1)
101+
102+
if [ -n "$AGENTCMD" ]; then
103+
break
104+
else
105+
sleep 5
106+
fi
107+
done
108+
109+
# Combine command and flags
110+
COMPLETECMD="$AGENTCMD --$ROLEFLAG"
111+
112+
# Run command
113+
$COMPLETECMD

0 commit comments

Comments
 (0)