|
1 | 1 | # Example: In-cluster |
2 | 2 |
|
3 | | -Running terraform in a kubernetes cluster and using in-cluster config. |
| 3 | +Running Terraform in a Kubernetes cluster using in-cluster config. |
4 | 4 |
|
5 | | -## Prerequisites |
| 5 | +## Steps |
6 | 6 |
|
7 | | -*This example uses syntax elements specific to Terraform version 0.12+. |
8 | | -It will not work out-of-the-box with Terraform 0.11.x and lower.* |
| 7 | +Executing Terraform in a Kubernetes cluster using an in-cluster config would require a service account with appropriate privileges attached to the Pod where Terraform is running. |
9 | 8 |
|
| 9 | +Below are the necessary steps to create a new service account `terraform` and grant permissions to create a Pod in a `default` namespace using the `kubernetes_pod_v1` Terraform resource as a namespaced resource example. |
10 | 10 |
|
11 | | -Standard run: |
| 11 | +1. Create a new service account: |
12 | 12 |
|
13 | | -``` |
14 | | -# terraform apply \ |
15 | | - -var "minikube_host_ip=$(minikube --profile kubernetes-1.16 ip)" |
16 | | -``` |
| 13 | + ```yaml |
| 14 | + apiVersion: v1 |
| 15 | + kind: ServiceAccount |
| 16 | + metadata: |
| 17 | + name: terraform |
| 18 | + ``` |
17 | 19 |
|
18 | | -With a custom build: |
| 20 | +1. Create a Role to grant permissions that are enought to manage Pods via Terraform: |
19 | 21 |
|
20 | | -``` |
21 | | -# terraform apply \ |
22 | | - -var "minikube_host_ip=$(minikube --profile kubernetes-1.16 ip)" \ |
23 | | - -var "in_cluster_provider_version=v1.10.1-dev" \ |
24 | | - -var "in_cluster_provider_url=https://storage.googleapis.com/my-custom-bucket/terraform-provider-kubernetes" |
25 | | -``` |
| 22 | + ```yaml |
| 23 | + apiVersion: rbac.authorization.k8s.io/v1 |
| 24 | + kind: Role |
| 25 | + metadata: |
| 26 | + name: terraform |
| 27 | + rules: |
| 28 | + - apiGroups: |
| 29 | + - "" |
| 30 | + resources: |
| 31 | + - pods |
| 32 | + verbs: |
| 33 | + - create |
| 34 | + - get |
| 35 | + - delete |
| 36 | + - patch |
| 37 | + ``` |
| 38 | +
|
| 39 | +1. Create a RoleBinding to attach service account `terraform` to the target Role: |
| 40 | + |
| 41 | + ```yaml |
| 42 | + apiVersion: rbac.authorization.k8s.io/v1 |
| 43 | + kind: RoleBinding |
| 44 | + metadata: |
| 45 | + name: terraform |
| 46 | + roleRef: |
| 47 | + apiGroup: rbac.authorization.k8s.io |
| 48 | + kind: Role |
| 49 | + name: terraform |
| 50 | + subjects: |
| 51 | + - kind: ServiceAccount |
| 52 | + name: terraform |
| 53 | + ``` |
| 54 | + |
| 55 | +1. Create a Pod that will initialize and apply Terraform code: |
| 56 | + |
| 57 | + ```yaml |
| 58 | + apiVersion: v1 |
| 59 | + kind: Pod |
| 60 | + metadata: |
| 61 | + name: terraform |
| 62 | + spec: |
| 63 | + serviceAccount: terraform |
| 64 | + initContainers: |
| 65 | + - name: init |
| 66 | + image: "hashicorp/terraform" |
| 67 | + command: [ "terraform", "-chdir=/terraform", "init" ] |
| 68 | + volumeMounts: |
| 69 | + - name: terraform |
| 70 | + mountPath: /terraform |
| 71 | + containers: |
| 72 | + - name: apply |
| 73 | + image: "hashicorp/terraform" |
| 74 | + command: [ "terraform", "-chdir=/terraform", "apply", "-auto-approve" ] |
| 75 | + volumeMounts: |
| 76 | + - name: terraform |
| 77 | + mountPath: /terraform |
| 78 | + volumes: |
| 79 | + - name: terraform |
| 80 | + persistentVolumeClaim: |
| 81 | + claimName: terraform |
| 82 | + restartPolicy: Never |
| 83 | + ``` |
| 84 | + |
| 85 | +Terraform code example that will work with the above configuration resides in files [`provider.tf`](provider.tf) and [`pod.tf`](pod.tf). As you can see, the provider configuration block is empty. In this case, all the necessary privileges are granted via the service account. |
| 86 | + |
| 87 | +Let's extend the previous example with privileges that are enough to create a Namespace using the `kubernetes_namespace_v1` Terraform resource as a cluster-level resource example. |
| 88 | + |
| 89 | +1. Create a ClusterRole to grant permissions that are enought to manage Pods via Terraform: |
| 90 | + |
| 91 | + ```yaml |
| 92 | + apiVersion: rbac.authorization.k8s.io/v1 |
| 93 | + kind: ClusterRole |
| 94 | + metadata: |
| 95 | + name: terraform |
| 96 | + rules: |
| 97 | + - apiGroups: |
| 98 | + - "" |
| 99 | + resources: |
| 100 | + - namespaces |
| 101 | + verbs: |
| 102 | + - create |
| 103 | + - get |
| 104 | + - delete |
| 105 | + - list |
| 106 | + - patch |
| 107 | + - update |
| 108 | + ``` |
| 109 | + |
| 110 | +1. Create a ClusterRoleBinding to attach service account `terraform` to the target ClusterRole: |
| 111 | + |
| 112 | + ```yaml |
| 113 | + apiVersion: rbac.authorization.k8s.io/v1 |
| 114 | + kind: ClusterRoleBinding |
| 115 | + metadata: |
| 116 | + name: terraform |
| 117 | + roleRef: |
| 118 | + apiGroup: rbac.authorization.k8s.io |
| 119 | + kind: ClusterRole |
| 120 | + name: terraform |
| 121 | + subjects: |
| 122 | + - kind: ServiceAccount |
| 123 | + name: terraform |
| 124 | + namespace: default |
| 125 | + ``` |
| 126 | + |
| 127 | +Terraform code example can be extanded with [`namespace.tf`](namespace.tf) file. To apply changes restart the Pod where Terraform is running. |
| 128 | + |
| 129 | +Please, always consult with the security team and follow the guidance accepted in your organization when granting RBAC privileges in a Kubernetes cluster. |
0 commit comments