├── .gitignore ├── README.md ├── kubernetes.tf ├── make-files.sh ├── output.tf ├── sample.terraform.tfvars └── vars.tf /.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | !kubernetes.tf 4 | !make-files.sh 5 | !output.tf 6 | !sample.terraform.tfvars 7 | !vars.tf 8 | !README.md 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kubernetes-terraform 2 | 3 | [Terraform](https://terraform.io) formula for creating a [Kubernetes](http://kubernetes.io) cluster running on [Scaleway](https://scaleway.com) 4 | 5 | The default configuration includes Kubernetes 6 | [add-ons](https://github.com/kubernetes/kubernetes/tree/master/cluster/addons): 7 | DNS, Dashboard and UI. 8 | 9 | ## Getting started: 10 | Clone or download repo. 11 | 12 | Copy `sample.terraform.tfvars` to `terraform.tfvars` and insert your variables. 13 | 14 | To generate your kubernetes cluster token, run `python -c 'import random; print "%0x.%0x" % (random.SystemRandom().getrandbits(3*8), random.SystemRandom().getrandbits(8*8))'` and set the kubernetes_token variable. 15 | 16 | 17 | ```bash 18 | $ brew update && brew install kubectl terraform 19 | 20 | $ terraform plan 21 | 22 | $ terraform apply 23 | 24 | $ scp root@:/etc/kubernetes/admin.conf . 25 | 26 | $ kubectl --kubeconfig ./admin.conf proxy 27 | ``` 28 | Access the dashboard and api via the following address: 29 | 30 | - API: `http://localhost:8001/api/v1` 31 | - Dashboard: `http://localhost:8001/ui` 32 | -------------------------------------------------------------------------------- /kubernetes.tf: -------------------------------------------------------------------------------- 1 | provider "scaleway" { 2 | organization = "${var.organization_key}" 3 | token = "${var.secret_key}" 4 | region = "${var.region}" 5 | } 6 | 7 | resource "scaleway_server" "kubernetes_master" { 8 | name = "${format("${var.kubernetes_cluster_name}-master-%02d", count.index)}" 9 | image = "${var.base_image_id}" 10 | dynamic_ip_required = "${var.dynamic_ip}" 11 | type = "${var.scaleway_master_type}" 12 | connection { 13 | user = "${var.user}" 14 | private_key = "${file(var.kubernetes_ssh_key_path)}" 15 | } 16 | 17 | provisioner "local-exec" { 18 | command = "rm -rf ./scw-install.sh ./scw-install-master.sh" 19 | } 20 | provisioner "local-exec" { 21 | command = "echo ${format("MASTER_%02d", count.index)}=\"${self.public_ip}\" >> ips.txt" 22 | } 23 | 24 | provisioner "local-exec" { 25 | command = "echo CLUSTER_NAME=\"${var.kubernetes_cluster_name}\" >> ips.txt" 26 | } 27 | provisioner "local-exec" { 28 | command = "./make-files.sh" 29 | } 30 | 31 | provisioner "local-exec" { 32 | command = "while [ ! -f ./scw-install.sh ]; do sleep 1; done" 33 | } 34 | 35 | provisioner "file" { 36 | source = "./scw-install.sh" 37 | destination = "/tmp/scw-install.sh" 38 | } 39 | 40 | provisioner "remote-exec" { 41 | inline = "KUBERNETES_TOKEN=\"${var.kubernetes_token}\" bash /tmp/scw-install.sh master" 42 | } 43 | 44 | } 45 | 46 | resource "scaleway_server" "kubernetes_slave" { 47 | name = "${format("${var.kubernetes_cluster_name}-slave-%02d", count.index)}" 48 | depends_on = ["scaleway_server.kubernetes_master"] 49 | image = "${var.base_image_id}" 50 | dynamic_ip_required = "${var.dynamic_ip}" 51 | type = "${var.scaleway_slave_type}" 52 | count = "${var.kubernetes_slave_count}" 53 | connection { 54 | user = "${var.user}" 55 | private_key = "${file(var.kubernetes_ssh_key_path)}" 56 | } 57 | provisioner "local-exec" { 58 | command = "while [ ! -f ./scw-install.sh ]; do sleep 1; done" 59 | } 60 | provisioner "file" { 61 | source = "scw-install.sh" 62 | destination = "/tmp/scw-install.sh" 63 | } 64 | provisioner "remote-exec" { 65 | inline = "KUBERNETES_TOKEN=\"${var.kubernetes_token}\" bash /tmp/scw-install.sh slave" 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /make-files.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | . ./ips.txt 4 | cat > scw-install.sh << FIN 5 | curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - 6 | 7 | cat < /etc/apt/sources.list.d/kubernetes.list 8 | deb http://apt.kubernetes.io/ kubernetes-xenial main 9 | EOF 10 | 11 | echo "DOCKER_OPTS='-H unix:///var/run/docker.sock --storage-driver aufs --label provider=scaleway --mtu=1500 --insecure-registry=10.0.0.0/8'" > /etc/default/docker 12 | systemctl restart docker 13 | 14 | apt-get update -qq \ 15 | && apt-get install -y -q --no-install-recommends kubelet kubeadm kubectl kubernetes-cni \ 16 | && apt-get clean 17 | 18 | for arg in "\$@" 19 | do 20 | case \$arg in 21 | 'master') 22 | SUID=\$(scw-metadata --cached ID) 23 | PUBLIC_IP=\$(scw-metadata --cached PUBLIC_IP_ADDRESS) 24 | PRIVATE_IP=\$(scw-metadata --cached PRIVATE_IP) 25 | 26 | kubeadm --token=\$KUBERNETES_TOKEN --apiserver-advertise-address=\$PUBLIC_IP --service-dns-domain=\$SUID.pub.cloud.scaleway.com init 27 | 28 | KUBECONFIG=/etc/kubernetes/admin.conf kubectl create -f http://docs.projectcalico.org/v2.3/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml 29 | KUBECONFIG=/etc/kubernetes/admin.conf kubectl create -f https://git.io/kube-dashboard 30 | break 31 | ;; 32 | 'slave') 33 | kubeadm join --token \$KUBERNETES_TOKEN $MASTER_00:6443 34 | break 35 | ;; 36 | esac 37 | done 38 | FIN 39 | rm -rf ./ips.txt 40 | -------------------------------------------------------------------------------- /output.tf: -------------------------------------------------------------------------------- 1 | output "Use this link to access Kubernetes dashboard" { 2 | value = "http://localhost:8001/ui/" 3 | } 4 | output "Then to access the dashboard locally run" { 5 | value = "kubectl --kubeconfig ./admin.conf proxy" 6 | } 7 | output "To connect to the API Server and viewing the dashboard copy the configuration locally" { 8 | value = "scp root@${scaleway_server.kubernetes_master.public_ip}:/etc/kubernetes/admin.conf ." 9 | } 10 | output "slave-ip" { 11 | value = "${join(",", scaleway_server.kubernetes_slave.*.public_ip)}" 12 | } 13 | output "master-ip" { 14 | value = "${join(",", scaleway_server.kubernetes_master.public_ip)}" 15 | } 16 | 17 | -------------------------------------------------------------------------------- /sample.terraform.tfvars: -------------------------------------------------------------------------------- 1 | organization_key = "00000000-0000-0000-0000-000000000000" 2 | 3 | secret_key = "00000000-0000-0000-0000-000000000000" 4 | 5 | region = "ams1" 6 | 7 | user = "root" 8 | 9 | dynamic_ip = "true" 10 | 11 | base_image_id = "b29a1c4e-43ed-4457-95f5-044ab7806e02" 12 | 13 | bootscript_id = "61648cb3-193d-41cf-bc20-9530fc3a659f" 14 | 15 | scaleway_slave_type = "VC1S" 16 | 17 | scaleway_master_type = "VC1S" 18 | 19 | kubernetes_cluster_name = "scaleway-kubernetes" 20 | 21 | kubernetes_slave_count = "5" 22 | 23 | kubernetes_ssh_key_path = "./scw" 24 | 25 | kubernetes_ssh_public_key_path = "./scw.pub" 26 | 27 | kubernetes_token = "xxxxxx.ccccccccccccccc" 28 | 29 | -------------------------------------------------------------------------------- /vars.tf: -------------------------------------------------------------------------------- 1 | variable "organization_key" { 2 | description = "Scaleway access_key" 3 | default = "00000000-0000-0000-0000-000000000000" 4 | } 5 | 6 | variable "secret_key" { 7 | description = "Scaleway secret_key" 8 | default = "00000000-0000-0000-0000-000000000000" 9 | } 10 | 11 | variable "region" { 12 | description = "Scaleway region: Paris (PAR1) or Amsterdam (AMS1)" 13 | default = "ams1" 14 | } 15 | 16 | variable "user" { 17 | description = "Username to connect the server" 18 | default = "root" 19 | } 20 | 21 | variable "dynamic_ip" { 22 | description = "Enable or disable server dynamic public ip" 23 | default = "true" 24 | } 25 | 26 | variable "bootscript_id" { 27 | description = "Scaleway bootscript ID" 28 | default = "00000000-0000-0000-0000-000000000000" 29 | } 30 | 31 | variable "base_image_id" { 32 | description = "Scaleway image ID" 33 | default = "00000000-0000-0000-0000-000000000000" 34 | } 35 | 36 | variable "scaleway_slave_type" { 37 | description = "Instance type of Slave" 38 | default = "VC1S" 39 | } 40 | 41 | variable "scaleway_master_type" { 42 | description = "Instance type of Master" 43 | default = "VC1S" 44 | } 45 | 46 | variable "kubernetes_cluster_name" { 47 | description = "Name of your cluster. Alpha-numeric and hyphens only, please." 48 | default = "scaleway-kubernetes" 49 | } 50 | 51 | variable "kubernetes_slave_count" { 52 | description = "Number of agents to deploy" 53 | default = "4" 54 | } 55 | 56 | variable "kubernetes_token" { 57 | description = "Token used to secure cluster boostrap" 58 | default = "cef4cf.a9e2d6e46c2d4d49" 59 | } 60 | 61 | variable "kubernetes_ssh_public_key_path" { 62 | description = "Path to your public SSH key path" 63 | default = "./scw.pub" 64 | } 65 | 66 | variable "kubernetes_ssh_key_path" { 67 | description = "Path to your private SSH key for the project" 68 | default = "./scw" 69 | } 70 | --------------------------------------------------------------------------------