Skip to content

santi1s/k8s-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

K8s Lab sandbox

Get a k8s cluster lab running in a few minutes.

You will need:

SSH keys set on your environment (cloud shell or WSL terminal) Ubuntu 18.04 machine with at least 4 cores and 16 G of RAM +100 GB of additional storage Ubuntu 18.04 cloud image

diagram

Prerequisites

For this setup, you will need:

Another option is to use the Cloud Shell on Azure at https://shell.azure.com

The examples here will use Microsoft Azure. For more information please refer to the Before You Begin section.

  • Azure CLI 2.0 (more instructions below)

Microsoft Azure CLI 2.0

Install the Microsoft Azure CLI 2.0

Follow the Azure CLI 2.0 documentation to install the az command line utility. You can install utility in various platforms such as macOS, Windows, Linux (various distros) and as a Docker container.

The examples here are based on the version 2.25.0 of the utility. You can verify the version of the tool by running:

az --version

Note: If this is your first time using the Azure CLI tool, you can familiarize yourself with it's syntax and command options by running az interactive

First Things First

Before we can use Azure, your first step, aside from having an account, is to login. Once Azure CLI is installed, open up a terminal and run the following:

az login

This will prompt you to sign in using a web browser to https://aka.ms/devicelogin and to enter the displayed code. This single step is needed in order to allow az to talk back to Azure.

Basic setup - Do this before proceeding

The first thing you should do is to clone this repo as most of the examples here will do relative reference to files.

Before we proceed, please do this now:

  1. Clone this repo:
git clone https://github.com/santi1s/k8s-lab.git
  1. If you are using VS Code, install the Terraform extension:

vs-terraform|20%

- Installing Terraform

Terraform is distributed as a binary package for various OSes, including: Linux, MacOS, OpenBSD, FreeBSD, Windows and Solaris.

Download the binary at: https://www.terraform.io/downloads.html

Another option is to use the Cloud Shell on Azure at https://shell.azure.com

- Verifying the Installation

Run the following command on a terminal: terraform. You should see an output similar to this:

$ terraform
Usage: terraform [--version] [--help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    push               Upload this Terraform module to Atlas to run
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan

More on the Terraform website.

- Setting up access to Azure

Terraform uses an Azure AD service principal to provision resources on Azure. Here we have two options:

  1. Manually setup the service principal as described here
  2. Use the setup_terraform.sh found under the tools/ directory.

Procedure:

  1. If not done already, log into Azure
    $ az login
  2. Next, select a SUBSCRIPTION
    $ az account list -o table
  3. The output should look similar to this

  Name                                CloudName    SubscriptionId  State    IsDefault
  ----------------------------------  -----------  --------------  -------  ---------
   Visual Studio Enterprise            AzureCloud   XXXXXXXX-XXXX.  Enabled  True
   ...

Execute the setup_terraform.sh script:

$ cd tools/
$ chmod +x setup_terraform.sh
$ SUBSCRIPTION=XXXXXXXXXX
$ ./setup_terraform.sh -s $SUBSCRIPTION

OUTPUT

Setting up the subscription
----------------------------------------------------------
$ az account set --subscription=XXXXXXXXXXXXX
Retrying role assignment creation: 1/36
Setting environment variables for Terraform
----------------------------------------------------------
$ source terraform.rc
Here are the Terraform environment variables for your setup
----------------------------------------------------------
export ARM_SUBSCRIPTION_ID="XXXXXXXXX"
export ARM_CLIENT_ID="XXXXXXXXX"
export ARM_CLIENT_SECRET="XXXXXXXX"
export ARM_TENANT_ID="XXXXXXX"
----------------------------------------------------------
Setup is done. Your Terraform variables were saved on the terraform.rc file.

K8s-lab Terraform Setup

- Core files

Reference files are under: infra/k8s-lab/

File Comment
main.tf This is our core file. All of the definitions are here
outputs.tf Defines the output from terraform show
variables.tf Defines the variables and default values used on main.tf
common.tfvars User defined values for the variables.red[*]

We will also run a few commands after the VM is up and running as defined on the file tools/kvm_setup.sh.

  • You will be editing this file.

- Default values

The following table shows our default values:

Parameter Default Value
vm_size Standard_D4s_v3
location WestEurope
Admin user.red[*] azureuser
VNet 10.0.0.0/16
Management Subnet 10.0.1.0/24
  • Admin user will connect with ssh using public and private key pair

What resources are we creating ?

  • azurerm_resource_group
  • azurerm_virtual_network
  • azurerm_subnet
  • azurerm_network_security_group
  • azurerm_network_interface
  • azurerm_public_ip
  • azurerm_storage_account
  • azurerm_managed_disk
  • azurerm_virtual_machine
  • azurerm_virtual_machine_extension

Deploying the k8s-lab VM

  1. terraform init
  2. terraform plan -var-file=common.tfvars -out k8s-lab-infra
  3. terraform apply "k8s-lab-infra"
  4. Connect to the vm: terraform show
ssh azureuser@<publicIp>

Cleaning up

  1. terraform destroy -var-file=common.tfvars

Say yes when you see this prompt:


  Do you really want to destroy?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: .red[yes]

More Terraform Examples in

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published