Skip to content

Commit 0f97829

Browse files
authored
Add updated examples for AKS, GKE, EKS (#1115)
Add some examples of using cloud providers with version 2 of the Kubernetes and Helm providers.
1 parent 3774dcd commit 0f97829

File tree

41 files changed

+1096
-518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1096
-518
lines changed

GNUmakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ depscheck:
3333
@git diff --exit-code -- vendor || \
3434
(echo; echo "Unexpected difference in vendor/ directory. Run 'go mod vendor' command or revert any go.mod/go.sum/vendor changes and commit."; exit 1)
3535

36+
examples-lint: tools
37+
@echo "==> Checking _examples dir formatting..."
38+
@./scripts/fmt-examples.sh || (echo; \
39+
echo "Terraform formatting errors found in _examples dir."; \
40+
echo "To see the full differences, run: ./scripts/fmt-examples.sh diff"; \
41+
echo "To automatically fix the formatting, run 'make examples-lint-fix' and commit the changes."; \
42+
exit 1)
43+
44+
examples-lint-fix: tools
45+
@echo "==> Fixing terraform formatting of _examples dir..."
46+
@./scripts/fmt-examples.sh fix
47+
3648
fmt:
3749
gofmt -w $(GOFMT_FILES)
3850

_examples/aks/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# AKS (Azure Kubernetes Service)
2+
3+
This example shows how to use the Terraform Kubernetes Provider and Terraform Helm Provider to configure an AKS cluster. The example config in this directory builds the AKS cluster and applies the Kubernetes configurations in a single operation. This guide will also show you how to make changes to the underlying AKS cluster in such a way that Kuberntes/Helm resources are recreated after the underlying cluster is replaced.
4+
5+
You will need the following environment variables to be set:
6+
7+
- `ARM_SUBSCRIPTION_ID`
8+
- `ARM_TENANT_ID`
9+
- `ARM_CLIENT_ID`
10+
- `ARM_CLIENT_SECRET`
11+
12+
Ensure that `KUBE_CONFIG_FILE` and `KUBE_CONFIG_FILES` environment variables are NOT set, as they will interfere with the cluster build.
13+
14+
```
15+
unset KUBE_CONFIG_FILE
16+
unset KUBE_CONFIG_FILES
17+
```
18+
19+
To install the AKS cluster using default values, run terraform init and apply from the directory containing this README.
20+
21+
```
22+
terraform init
23+
terraform apply
24+
```
25+
26+
## Kubeconfig for manual CLI access
27+
28+
This example generates a kubeconfig file in the current working directory, which can be used for manual CLI access to the cluster.
29+
30+
```
31+
export KUBECONFIG=$(terraform output -raw kubeconfig_path)
32+
kubectl get pods -n test
33+
```
34+
35+
However, in a real-world scenario, this config file would have to be replaced periodically as the AKS client certificates eventually expire (see the [Azure documentation](https://docs.microsoft.com/en-us/azure/aks/certificate-rotation) for the exact expiry dates). If the certificates (or other authentication attributes) are replaced, run a targeted `terraform apply` to save the new credentials into state.
36+
37+
```
38+
terraform plan -target=module.aks-cluster
39+
terraform apply -target=module.aks-cluster
40+
```
41+
42+
Once the targeted apply is finished, the Kubernetes and Helm providers will be available for use again. Run `terraform apply` again (without targeting) to apply any updates to Kubernetes resources.
43+
44+
```
45+
terraform plan
46+
terraform apply
47+
```
48+
49+
This approach prevents the Kubernetes and Helm providers from attempting to use cached, invalid credentials, which would cause provider configuration errors durring the plan and apply phases.
50+
51+
## Replacing the AKS cluster and re-creating the Kubernetes / Helm resources
52+
53+
When the cluster is initially created, the Kubernetes and Helm providers will not be initialized until authentication details are created for the cluster. However, for future operations that may involve replacing the underlying cluster (for example, changing VM sizes), the AKS cluster will have to be targeted without the Kubernetes/Helm providers, as shown below. This is done by removing the `module.kubernetes-config` from Terraform State prior to replacing cluster credentials, to avoid passing outdated credentials into the providers.
54+
55+
This will create the new cluster and the Kubernetes resources in a single apply.
56+
57+
```
58+
terraform state rm module.kubernetes-config
59+
terraform apply
60+
```

_examples/aks/aks-cluster/main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
resource "azurerm_resource_group" "test" {
2+
name = var.cluster_name
3+
location = var.location
4+
}
5+
6+
resource "azurerm_kubernetes_cluster" "test" {
7+
name = var.cluster_name
8+
location = azurerm_resource_group.test.location
9+
resource_group_name = azurerm_resource_group.test.name
10+
dns_prefix = var.cluster_name
11+
12+
default_node_pool {
13+
name = "default"
14+
node_count = 1
15+
vm_size = "Standard_DS2_v2"
16+
}
17+
18+
identity {
19+
type = "SystemAssigned"
20+
}
21+
}
22+
23+
resource "local_file" "kubeconfig" {
24+
content = azurerm_kubernetes_cluster.test.kube_config_raw
25+
filename = "${path.root}/kubeconfig"
26+
}
27+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
output "client_cert" {
2+
value = azurerm_kubernetes_cluster.test.kube_config.0.client_certificate
3+
}
4+
5+
output "client_key" {
6+
value = azurerm_kubernetes_cluster.test.kube_config.0.client_key
7+
}
8+
9+
output "ca_cert" {
10+
value = azurerm_kubernetes_cluster.test.kube_config.0.cluster_ca_certificate
11+
}
12+
13+
output "endpoint" {
14+
value = azurerm_kubernetes_cluster.test.kube_config.0.host
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "kubernetes_version" {
2+
default = "1.18"
3+
}
4+
5+
variable "workers_count" {
6+
default = "3"
7+
}
8+
9+
variable "cluster_name" {
10+
type = string
11+
}
12+
13+
variable "location" {
14+
type = string
15+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
resource "kubernetes_namespace" "test" {
2+
metadata {
3+
name = "test"
4+
}
5+
}
6+
7+
resource "kubernetes_deployment" "test" {
8+
metadata {
9+
name = "test"
10+
namespace= kubernetes_namespace.test.metadata.0.name
11+
}
12+
spec {
13+
replicas = 2
14+
selector {
15+
match_labels = {
16+
app = "test"
17+
}
18+
}
19+
template {
20+
metadata {
21+
labels = {
22+
app = "test"
23+
}
24+
}
25+
spec {
26+
container {
27+
image = "nginx:1.19.4"
28+
name = "nginx"
29+
30+
resources {
31+
limits = {
32+
memory = "512M"
33+
cpu = "1"
34+
}
35+
requests = {
36+
memory = "256M"
37+
cpu = "50m"
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
resource helm_release nginx_ingress {
47+
name = "nginx-ingress-controller"
48+
49+
repository = "https://charts.bitnami.com/bitnami"
50+
chart = "nginx-ingress-controller"
51+
52+
set {
53+
name = "service.type"
54+
value = "ClusterIP"
55+
}
56+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variable "cluster_name" {
2+
type = string
3+
}

_examples/aks/main.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
terraform {
2+
required_providers {
3+
kubernetes = {
4+
source = "hashicorp/kubernetes"
5+
version = ">= 2.0.0"
6+
}
7+
azurerm = {
8+
source = "hashicorp/azurerm"
9+
version = "2.42"
10+
}
11+
helm = {
12+
source = "hashicorp/helm"
13+
version = ">= 2.0.1"
14+
}
15+
}
16+
}
17+
18+
provider "kubernetes" {
19+
host = module.aks-cluster.endpoint
20+
client_key = base64decode(module.aks-cluster.client_key)
21+
client_certificate = base64decode(module.aks-cluster.client_cert)
22+
cluster_ca_certificate = base64decode(module.aks-cluster.ca_cert)
23+
}
24+
25+
provider "helm" {
26+
kubernetes {
27+
host = module.aks-cluster.endpoint
28+
client_key = base64decode(module.aks-cluster.client_key)
29+
client_certificate = base64decode(module.aks-cluster.client_cert)
30+
cluster_ca_certificate = base64decode(module.aks-cluster.ca_cert)
31+
}
32+
}
33+
34+
provider "azurerm" {
35+
features {}
36+
}
37+
38+
module "aks-cluster" {
39+
providers = { azurerm = azurerm }
40+
source = "./aks-cluster"
41+
cluster_name = local.cluster_name
42+
location = var.location
43+
}
44+
45+
module "kubernetes-config" {
46+
providers = { kubernetes = kubernetes, helm = helm }
47+
depends_on = [module.aks-cluster]
48+
source = "./kubernetes-config"
49+
cluster_name = local.cluster_name
50+
}

_examples/aks/outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "kubeconfig_path" {
2+
value = abspath("${path.root}/kubeconfig")
3+
}
4+
5+
output "cluster_name" {
6+
value = local.cluster_name
7+
}

_examples/aks/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "location" {
2+
type = string
3+
default = "westus2"
4+
}
5+
6+
resource "random_id" "cluster_name" {
7+
byte_length = 5
8+
}
9+
10+
locals {
11+
cluster_name = "tf-k8s-${random_id.cluster_name.hex}"
12+
}

0 commit comments

Comments
 (0)