This commit is contained in:
marcel-dempers 2020-07-17 13:00:33 +10:00
parent 877810d147
commit 837f69f019
6 changed files with 97 additions and 31 deletions

View File

@ -1,5 +1,11 @@
provider "azurerm" {
version = "=2.0.0"
version = "=2.5.0"
subscription_id = var.subscription_id
client_id = var.serviceprinciple_id
client_secret = var.serviceprinciple_key
tenant_id = var.tenant_id
features {}
}

View File

@ -30,7 +30,6 @@ resource "azurerm_kubernetes_cluster" "aks-getting-started" {
}
}
network_profile {
network_plugin = "kubenet"
load_balancer_sku = "Standard"
@ -57,6 +56,7 @@ resource "azurerm_kubernetes_cluster" "aks-getting-started" {
enabled = false
}
}
}
/*

View File

@ -4,9 +4,6 @@ variable "serviceprinciple_id" {
variable "serviceprinciple_key" {
}
variable "ssh_key" {
}
variable "location" {
default = "australiaeast"
}
@ -14,3 +11,6 @@ variable "location" {
variable "kubernetes_version" {
default = "1.16.10"
}
variable "ssh_key" {
}

View File

@ -7,6 +7,7 @@ provider "kubernetes" {
cluster_ca_certificate = var.cluster_ca_certificate
}
resource "kubernetes_deployment" "example" {
metadata {
name = "terraform-example"
@ -66,3 +67,20 @@ resource "kubernetes_deployment" "example" {
}
}
}
resource "kubernetes_service" "example" {
metadata {
name = "terraform-example"
}
spec {
selector = {
test = "MyExampleApp"
}
port {
port = 80
target_port = 80
}
type = "LoadBalancer"
}
}

View File

@ -6,22 +6,13 @@ Terraform provider for Azure [here](https://github.com/terraform-providers/terra
## Azure CLI
You can get the Azure CLI on [Docker-Hub](https://hub.docker.com/_/microsoft-azure-cli) <br/>
We'll need the Azure CLI to gather information so we can build our Terraform file.
```
# Run Azure CLI
docker run -it --rm -v ${PWD}:/work -w /work --entrypoint /bin/sh mcr.microsoft.com/azure-cli:2.6.0
# Get Terraform
curl -o /tmp/terraform.zip -LO https://releases.hashicorp.com/terraform/0.12.28/terraform_0.12.28_linux_amd64.zip
unzip /tmp/terraform.zip
chmod +x terraform && mv terraform /usr/local/bin/
cd kubernetes/cloud/azure/terraform/
terraform init
```
## Login to Azure
@ -29,6 +20,7 @@ terraform init
```
#login and follow prompts
az login
TENTANT_ID=<your-tenant-id>
# view and select your subscription account
@ -38,13 +30,6 @@ az account set --subscription $SUBSCRIPTION
```
## Create our Resource Group
```
RESOURCEGROUP=aks-getting-started
RESOURCEGROUP_ID=$(az group create -n $RESOURCEGROUP -l australiaeast | jq -r '.id')
```
## Create Service Principal
@ -55,20 +40,36 @@ Lets create one! </br>
SERVICE_PRINCIPAL_JSON=$(az ad sp create-for-rbac --skip-assignment --name aks-getting-started-sp -o json)
#Keep the `appId` and `password` for later use!
# Keep the `appId` and `password` for later use!
SERVICE_PRINCIPAL=$(echo $SERVICE_PRINCIPAL_JSON | jq -r '.appId')
SERVICE_PRINCIPAL_SECRET=$(echo $SERVICE_PRINCIPAL_JSON | jq -r '.password')
#grant contributor role over the resource group to our service principal
#note: reset the credential if you have any sinlge or double quote on password
az ad sp credential reset --name "aks-getting-started-sp"
# Grant contributor role over the subscription to our service principal
az role assignment create --assignee $SERVICE_PRINCIPAL \
--scope "/subscriptions/$SUBSCRIPTION/resourceGroups/$RESOURCEGROUP" \
--scope "/subscriptions/$SUBSCRIPTION" \
--role Contributor
```
For extra reference you can also take a look at the Microsoft Docs: [here](https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/aks/kubernetes-service-principal.md) </br>
```
# Get Terraform
curl -o /tmp/terraform.zip -LO https://releases.hashicorp.com/terraform/0.12.28/terraform_0.12.28_linux_amd64.zip
unzip /tmp/terraform.zip
chmod +x terraform && mv terraform /usr/local/bin/
cd kubernetes/cloud/azure/terraform/
```
# Generate SSH key
```
@ -81,9 +82,43 @@ SSH_KEY=$(cat ~/.ssh/id_rsa.pub)
Documentation on all the Kubernetes fields for terraform [here](https://www.terraform.io/docs/providers/azurerm/r/kubernetes_cluster.html)
```
terraform plan -var serviceprinciple_id=$SERVICE_PRINCIPAL -var serviceprinciple_key="$SERVICE_PRINCIPAL_SECRET" -var ssh_key="$SSH_KEY"
terraform init
# Import existing resource group
terraform import -var serviceprinciple_id=$SERVICE_PRINCIPAL -var serviceprinciple_key="$SERVICE_PRINCIPAL_SECRET" -var ssh_key="$SSH_KEY" module.cluster.azurerm_resource_group.aks-getting-started $RESOURCEGROUP_ID
terraform apply -var serviceprinciple_id=$SERVICE_PRINCIPAL -var serviceprinciple_key="$SERVICE_PRINCIPAL_SECRET" -var ssh_key="$SSH_KEY"
terraform plan -var serviceprinciple_id=$SERVICE_PRINCIPAL \
-var serviceprinciple_key="$SERVICE_PRINCIPAL_SECRET" \
-var tenant_id=$TENTANT_ID \
-var subscription_id=$SUBSCRIPTION \
-var ssh_key="$SSH_KEY"
terraform apply -var serviceprinciple_id=$SERVICE_PRINCIPAL \
-var serviceprinciple_key="$SERVICE_PRINCIPAL_SECRET" \
-var tenant_id=$TENTANT_ID \
-var subscription_id=$SUBSCRIPTION \
-var ssh_key="$SSH_KEY"
```
# Lets see what we deployed
```
# grab our AKS config
az aks get-credentials -n aks-getting-started -g aks-getting-started
# Get kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
mv ./kubectl /usr/local/bin/kubectl
kubectl get svc
```
# Clean up
```
terraform destroy -var serviceprinciple_id=$SERVICE_PRINCIPAL \
-var serviceprinciple_key="$SERVICE_PRINCIPAL_SECRET" \
-var tenant_id=$TENTANT_ID \
-var subscription_id=$SUBSCRIPTION \
-var ssh_key="$SSH_KEY"
```

View File

@ -4,6 +4,13 @@ variable "serviceprinciple_id" {
variable "serviceprinciple_key" {
}
variable "tenant_id" {
}
variable "subscription_id" {
}
variable "ssh_key" {
}