/devops-terraform-scaleway

Terraform Infrastructure as Code to provision a Kubernetes Cluster to Scaleway cloud

Primary LanguageHCL

Scaleway Kubernetes Cluster deployment with Terraform

Generating an SSH key

An SSH key consists of a pair of files. One is the private key, which should never be shared with anyone. The other is the public key. The other file is a public key which allows you to log into the containers and VMs you provision. When you generate the keys, you will use ssh-keygen to store the keys in a safe location so you can bypass the login prompt when connecting to your instances.

To generate SSH keys in macOS, follow these steps:

Enter the following command in the Terminal window.

ssh-keygen -t rsa

This starts the key generation process. When you execute this command, the ssh-keygen utility prompts you to indicate where to store the key.

Press the ENTER key to accept the default location. The ssh-keygen utility prompts you for a passphrase.

Type in a passphrase. You can also hit the ENTER key to accept the default (no passphrase). However, this is not recommended. Warning! You will need to enter the passphrase a second time to continue.

After you confirm the passphrase, the system generates the key pair.

Your identification has been saved in /Users/myname/.ssh/id_rsa. Your public key has been saved in /Users/myname/.ssh/id_rsa.pub. Your private key is saved to the id_rsa file in the .ssh directory and is used to verify the public key you use belongs to the same Triton Compute Service account.

Setting up your cluster requires that you have terraform and kubectl installed:

Install terraform and kubectl using via homebrew

Install tfswitch to specify Terraform version

brew install tfswitch

Supply Terraform version 0.11.11

tfswitch 0.11.11

Install kubectl

brew install kubectl

Creating a namespace in terraform

terraform workspace new my-namespace

Init Terraform

terraform init

Decrypt private key before passing it to terraform apply

Creating the k8s cluster

The current terraform script needs that some environment variables to be defined to run, you can pass the variables via command line or by editing the variables.tf script.

Here is how to run terraform using command line variables:

Important: The command must be executed under same directory as Terraform scripts.

terraform apply \
 -var scaleway_organization={{SCALEWAY_ORGANIZATION_ID}} \
 -var scaleway_access={{SCALEWAY_ACCESS_ID}} \
 -var scaleway_token={{SCALEWAY_ACCESS_TOKEN}} \
 -var private_key=~/.ssh/rsa_key

If you edited the variables.tf script simply run

terraform apply

Export Kubernetes cluster config generated by Terraform as env variable

 export KUBECONFIG=~/my-namespace.conf

my-namespace = terraform workspace name (default if not defined)

Create a proxy for Kubernetes Dashboard

 kubectl -n kube-system port-forward deployment/kubernetes-dashboard 8888:9090 --kubeconfig=./my-namespace.conf

my-namespace = terraform workspace name (default if not defined)

Access the Kubernetes Dashboard

http://127.0.0.1:8888

Destroying the cluster

terraform destroy \
 -var scaleway_organization={{SCALEWAY_ORGANIZATION_ID}} \
 -var scaleway_access={{SCALEWAY_ACCESS_ID}} \
 -var scaleway_token={{SCALEWAY_ACCESS_TOKEN}} \
 -var private_key=~/.ssh/rsa_key