Skip to content

Commit d2f2d31

Browse files
authored
feat: Added support for cloud init (#484)
* feat: added support for cloud-init. Closes #457. Added support for cloud-init. Updated docs and added subsections for bastion, operator instructions to make table of content render better. * fix: changed timezone from hardcoded in cloud-init, changed default timezone to Etc/UTC
1 parent 80c9805 commit d2f2d31

File tree

10 files changed

+198
-47
lines changed

10 files changed

+198
-47
lines changed

docs/instructions.adoc

Lines changed: 113 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@
5555
:uri-psp: https://docs.cloud.oracle.com/en-us/iaas/Content/ContEng/Tasks/contengusingpspswithoke.htm#Using_Pod_Security_Polices_with_Container_Engine_for_Kubernetes
5656
:uri-kubernetes-vpa: https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler
5757

58-
=== Assumptions
58+
== Assumptions
5959

6060
This section assumes you have completed the following:
6161

6262
. all the {uri-prereqs}[prerequisites]
6363
. all the required {uri-configuration}[configuration]
6464

65-
=== KMS Integration
65+
== KMS Integration
6666

67-
==== OKE Variables
67+
== OKE Variables
6868

6969
If you wish to use {uri-oci-kms}[OCI KMS] to encrypt Kubernetes secrets, the following is required:
7070

@@ -86,7 +86,7 @@ If you wish to encrypt data in transit between the instance, the boot volume, an
8686

8787
* enable_pv_encryption_in_transit must be _true_
8888

89-
==== Operator Variables
89+
== Operator Variables
9090

9191
If you wish to use {uri-oci-kms}[OCI KMS] to encrypt operator boot/block volume, the following is required:
9292

@@ -96,7 +96,7 @@ If you wish to encrypt data in transit between the instance, the boot volume, an
9696

9797
* enable_operator_pv_encryption_in_transit must be _true_
9898

99-
=== Creating the OKE Cluster
99+
== Creating the OKE Cluster
100100

101101
Initialize a working directory containing Terraform configuration files:
102102

@@ -114,14 +114,11 @@ You can create a Kubernetes cluster with the latest version of Kubernetes availa
114114

115115
Use the parameter *cluster_name* to change the name of the cluster as per your needs.
116116

117-
117+
== Using the bastion host
118118
=== Adding the bastion host
119119

120120
If you want to use bastion host, set the parameter *create_bastion_host* to *true* in terraform.tfvars. Refer to {uri-terraform-options}#bastion-host[Bastion Host] for other available bastion related parameters.
121121

122-
123-
=== Using the bastion host
124-
125122
****
126123
*Assumption: you have set the create_bastion_host parameter to true in terraform.tfvars*
127124
****
@@ -134,13 +131,22 @@ terraform output
134131

135132
You can then copy the ssh_to_bastion command, paste and run it in a terminal.
136133

134+
=== Shutting down the bastion host
135+
136+
You can also shutdown the bastion host. Shutting down the bastion host does not destroy it and its public IP address will be preserved.
137+
138+
To turn off the bastion host, set `bastion_state= "STOPPED"` and run `terraform apply` again.
139+
140+
To turn it on again, set `bastion_state= "RUNNING"` and run `terraform apply` again.
141+
142+
== Using the operator host
137143
=== Adding the operator host
138144

139145
The operator host is used to minimize local dependencies such as oci-cli, kubectl and so on.
140146

141147
If you want to use the operator host, set the parameter *create_operator* to *true* in terraform.tfvars. Refer to {uri-terraform-options}#operator-host[Admin Host] for other available bastion related parameters.
142148

143-
==== Upgrading the operator host
149+
=== Upgrading the operator host
144150

145151
There is 1 additional parameter for the operator:
146152

@@ -152,12 +158,6 @@ _upgrade_operator_ will upgrade the operator compute packages on first boot.
152158
N.B. It is good and recommended practice to upgrade your package host to the latest packages to minimize the possibility of vulnerabilities. However, it will also take slightly longer before the package host is available.
153159
****
154160

155-
=== Using the operator host
156-
157-
****
158-
*Assumption: you have set the create_operator parameter to true in terraform.tfvars*
159-
****
160-
161161
Once the terraform apply is successful you will get the operator_private_ip as output and also a ssh command. You can also run the below command to get the output:
162162

163163
----
@@ -166,6 +166,15 @@ terraform output
166166

167167
You can then copy the ssh_to_operator command, paste and run it in a terminal.
168168

169+
=== Shutting down the operator host
170+
171+
You can also shutdown the operator host. Shutting down the operator host does not destroy it.
172+
173+
To turn off the operator host, set `operator_state= "STOPPED"` and run `terraform apply` again.
174+
175+
To turn it on again, set `operator_state= "RUNNING"` and run `terraform apply` again.
176+
177+
=== Using instance_principal on the operator host
169178
==== Enabling instance_principal on the operator host
170179
{uri-oci-instance-principal}[instance_principal] is an IAM service feature that enables instances to be authorized actors (or principals) to perform actions on service resources. Each compute instance has its own identity, and it authenticates using the certificates that are added to it. These certificates are automatically created, assigned to instances and rotated, preventing the need for you to distribute credentials to your hosts and rotate them.
171180

@@ -211,7 +220,7 @@ terraform apply
211220
. Enable instance_principal *_if and only if_* you are using link:#kms-integration[KMS Integration], calico, metricserver or creating the OCIR secret.
212221
. Disable instance_principal once the cluster is created
213222

214-
=== Interacting with the OKE Cluster
223+
== Interacting with the OKE Cluster
215224

216225
kubectl installed on the operator host by default and the kubeconfig file is set in the default location (~/.kube/config) so you don't need to set the KUBECONFIG environment variable every time you log in to the operator host.
217226

@@ -231,7 +240,7 @@ export KUBECONFIG=generated/kubeconfig
231240
*Ensure you install the same kubectl version as the OKE Kubernetes version for compatibility.*
232241
****
233242

234-
=== Creating a Secret for OCIR
243+
== Creating a Secret for OCIR
235244

236245
{uri-oci-ocir}[Oracle Cloud Infrastructure Registry] is a highly available private container registry service for storing and sharing container images within the same regions as the OKE Cluster. Use the following rules to determine if you need to create a Kubernetes Secret for OCIR:
237246

@@ -244,15 +253,15 @@ You must then {uri-oci-secret}[create a Secret in OCI Vault to store] the value
244253

245254
Finally, assign the Secret OCID to *secret_id* in terraform.tfvars. Refer to {uri-terraform-options}#ocir[OCIR parameters] for other parameters to be set.
246255

247-
=== Installing Calico
256+
== Installing Calico
248257

249258
Calico enables network policy in Kubernetes clusters. To install calico set the parameter *enable_calico = true* in terraform.tfvars. By default its set to false. Refer to {uri-terraform-options}#calico[Calico parameters] for other available parameters.
250259

251-
=== Installing Kubernetes Metrics Server
260+
== Installing Kubernetes Metrics Server
252261

253262
{uri-metricserver}[Kubernetes Metrics Server] can be installed by setting the parameter *enable_metric_server = true* in terraform.tfvars. By default, the latest version is installed in kube-system namespace. This is required if you need to use Horizontal Pod Autoscaling.
254263

255-
=== Installing Vertical Pod Autoscaler
264+
== Installing Vertical Pod Autoscaler
256265

257266
{uri-kubernetes-vpa}[Vertical Pod Autoscaler] can be installed by configuring the `vpa` parameter:
258267

@@ -263,7 +272,7 @@ Calico enables network policy in Kubernetes clusters. To install calico set the
263272

264273
NOTE: Installing the Vertical Pod Autoscaler also requires installing the Metrics Server, so you need to enable that too.
265274

266-
=== Scaling the node pools
275+
== Scaling the node pools
267276

268277
There are 2 ways you can scale the node pools:
269278

@@ -276,7 +285,83 @@ Scaling changes to the number and size of node pools are immediate after changin
276285

277286
Set the parameter *node_pools* to the desired quantities to scale the node pools accordingly. Refer to {uri-topology}#node-pools[Nodepool].
278287

279-
=== Accessing the Kubernetes dashboard
288+
== Configuring cloud-init for the nodepools
289+
290+
To install additional packages, configure settings or execute certain extra commands on node pools, you can use cloud-init to do that for you.
291+
292+
By default, there is a `worker.template.sh` file under the cloudinit directory in the `oke` module. Follow the following steps to add your own:
293+
294+
. Add your own script in the cloudinit directory. An example is provided by `second.template.sh`. If you need to parameterize the script, enclose the parameter within `${}`. For example:
295+
296+
+
297+
----
298+
yum install -y package-${version}
299+
----
300+
301+
302+
. Declare your script as a template in the cloudinit.tf in the locals section. A corresponding example for the `second.template.sh` is provided.
303+
304+
+
305+
If you do *_not_* need to pass a parameter to the script, leave the 2^nd^ argument to the template file function as empty:
306+
307+
+
308+
----
309+
second_script_template = templatefile("${path.module}/cloudinit/second.template.sh",{})
310+
----
311+
312+
+
313+
Otherwise, pass a parameter to the template script as follows:
314+
315+
+
316+
----
317+
second_script_template = templatefile("${path.module}/cloudinit/second.template.sh",
318+
{
319+
version = "5.4.0"
320+
}
321+
)
322+
----
323+
324+
. Add the part in the `cloudinit_config worker` section:
325+
326+
+
327+
----
328+
part {
329+
filename = "second.sh"
330+
content_type = "text/x-shellscript"
331+
content = local.second_script_template
332+
}
333+
----
334+
335+
. You can add more scripts and have them loaded in the order you need. For example:
336+
337+
+
338+
----
339+
data "cloudinit_config" "worker" {
340+
gzip = false
341+
base64_encode = true
342+
343+
part {
344+
filename = "worker.sh"
345+
content_type = "text/x-shellscript"
346+
content = local.worker_script_template
347+
}
348+
349+
part {
350+
filename = "second.sh"
351+
content_type = "text/x-shellscript"
352+
content = local.second_script_template
353+
}
354+
355+
part {
356+
filename = "third.sh"
357+
content_type = "text/x-shellscript"
358+
content = local.third_script_template
359+
}
360+
}
361+
----
362+
363+
364+
== Accessing the Kubernetes dashboard
280365

281366
By default, the Kubernetes dashboard is now disabled. To enable it, set the *dashboard_enabled = true* _before_ creating the cluster. The dashboard will then be deployed.
282367

@@ -288,7 +373,7 @@ kubectl proxy
288373

289374
Open a browser and go to {uri-k8s-dashboard}[Kubernetes Dashboard] to display the Kubernetes Dashboard.
290375

291-
=== Destroying the cluster
376+
== Destroying the cluster
292377

293378
Run the below command to destroy the infrastructure created by terraform:
294379

@@ -301,7 +386,7 @@ terraform destroy
301386
****
302387

303388

304-
=== Creating a service account for CI/CD tools
389+
== Creating a service account for CI/CD tools
305390

306391
OKE now uses Kubeconfig v2 which means the default token has a limited lifespan. In order to allow CI/CD tools to deploy to OKE, a service account must be created.
307392

@@ -317,7 +402,7 @@ service_account_namespace = "kube-system"
317402
service_account_cluster_role_binding = ""
318403
----
319404

320-
=== Enabling WAF
405+
== Enabling WAF
321406

322407
You can monitor and protect the load balancers created by OKE using {uri-oci-waf}[OCI Web Application Firewall].
323408

@@ -340,7 +425,7 @@ N.B.
340425
. WAF protection currently only works if you use a public load balancer as a front end to your services. This means that services deployed as NodePort services are currently *not protected* by WAF.
341426
****
342427

343-
=== Enabling PodSecurityPolicy
428+
== Enabling PodSecurityPolicy
344429

345430
If you would like to enable the PodSecurityPolicy Admission Controller, set
346431

@@ -355,7 +440,7 @@ Ensure you also read {uri-psp}[the documentation] before enabling it.
355440
N.B. This field is updatable. You can set to `true` and `false` and run terraform apply again.
356441
****
357442

358-
=== Using Dynamic and Flexible Load Balancers
443+
== Using Dynamic and Flexible Load Balancers
359444

360445
When you create a service of type LoadBalancer, by default, an OCI Load Balancer with dynamic shape 100Mbps will be created.
361446

docs/terraformoptions.adoc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ EOT
299299
|bastion_timezone
300300
|The preferred timezone for the bastion host. {uri-timezones}[List of timezones]. *Required*
301301
|
302-
|Australia/Sydney
302+
|Etc/UTC
303303

304304
|bastion_type
305305
|Whether to make the bastion host public or private.
@@ -419,8 +419,8 @@ EOT
419419

420420
|`operator_timezone`
421421
|The preferred timezone for the operator host. {uri-timezones}[List of timezones]. *Required*
422-
|e.g. Australia/Sydney
423-
|Australia/Sydney
422+
|e.g. Etc/UTC
423+
|Etc/UTC
424424

425425
|`upgrade_operator`
426426
|Whether to also upgrade the packages for the operator host.
@@ -695,6 +695,11 @@ node_pools = {
695695
|
696696
|7.9
697697

698+
|node_pool_timezone
699+
|The preferred timezone for the worker nodes. {uri-timezones}[List of timezones].
700+
|
701+
|Etc/UTC
702+
698703
|`worker_nsgs`
699704
|An additional list of network security groups (NSG) ids for the worker nodes that can be created subsequently.
700705
|["ocid1.networksecuritygroup.oc1....","ocid1.networksecuritygroup.oc1...."]

main.tf

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,15 @@ module "oke" {
228228
admission_controller_options = var.admission_controller_options
229229

230230
# oke node pool parameters
231-
node_pools = var.node_pools
232-
node_pool_name_prefix = var.node_pool_name_prefix
233-
node_pool_image_id = var.node_pool_image_id
234-
node_pool_os = var.node_pool_os
235-
node_pool_os_version = var.node_pool_os_version
236-
enable_pv_encryption_in_transit = var.enable_pv_encryption_in_transit
237-
use_node_pool_volume_encryption = var.use_node_pool_volume_encryption
238-
node_pool_volume_kms_key_id = var.node_pool_volume_kms_key_id
231+
node_pools = var.node_pools
232+
node_pool_name_prefix = var.node_pool_name_prefix
233+
node_pool_image_id = var.node_pool_image_id
234+
node_pool_os = var.node_pool_os
235+
node_pool_os_version = var.node_pool_os_version
236+
node_pool_timezone = var.node_pool_timezone
237+
enable_pv_encryption_in_transit = var.enable_pv_encryption_in_transit
238+
use_node_pool_volume_encryption = var.use_node_pool_volume_encryption
239+
node_pool_volume_kms_key_id = var.node_pool_volume_kms_key_id
239240

240241
# oke load balancer parameters
241242
preferred_load_balancer = var.preferred_load_balancer
@@ -244,7 +245,7 @@ module "oke" {
244245
worker_nsgs = concat(var.worker_nsgs, [module.network.worker_nsg_id])
245246

246247
# freeform_tags
247-
freeform_tags = var.freeform_tags["oke"]
248+
freeform_tags = var.freeform_tags["oke"]
248249

249250
depends_on = [
250251
module.network

modules/oke/cloudinit.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
locals {
2+
worker_script_template = templatefile("${path.module}/cloudinit/worker.template.sh",
3+
{
4+
worker_timezone = var.node_pool_timezone
5+
}
6+
)
7+
# example for adding more script
8+
# second_script_template = templatefile("${path.module}/cloudinit/second.template.sh",{})
9+
}
10+
11+
# cloud-init for workers
12+
data "cloudinit_config" "worker" {
13+
gzip = false
14+
base64_encode = true
15+
16+
part {
17+
filename = "worker.sh"
18+
content_type = "text/x-shellscript"
19+
content = local.worker_script_template
20+
}
21+
22+
# example for adding more script
23+
# part {
24+
# filename = "second.sh"
25+
# content_type = "text/x-shellscript"
26+
# content = local.second_script_template
27+
# }
28+
}
29+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
touch /var/log/second.done
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# DO NOT MODIFY
4+
curl --fail -H "Authorization: Bearer Oracle" -L0 http://169.254.169.254/opc/v2/instance/metadata/oke_init_script | base64 --decode >/var/run/oke-init.sh
5+
6+
## run oke provisioning script
7+
bash -x /var/run/oke-init.sh
8+
9+
### adjust block volume size
10+
/usr/libexec/oci-growfs -y
11+
12+
timedatectl set-timezone ${worker_timezone}
13+
14+
touch /var/log/oke.done

0 commit comments

Comments
 (0)