Skip to content

Commit 0f953d5

Browse files
marinakogrjeberhard
authored andcommitted
added example of terraform scripts config files to create OKE cluster
1 parent c1bf788 commit 0f953d5

File tree

10 files changed

+766
-0
lines changed

10 files changed

+766
-0
lines changed

kubernetes/samples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ While these samples may be useful and usable as is, it is intended that you woul
1414
* [Sample for creating a WebLogic domain home inside a Docker image](scripts/create-weblogic-domain/domain-home-in-image/README.md), and the domain resource YAML file for deploying the generated WebLogic domain.
1515
* [Sample for configuring the Elasticsearch and Kibana](scripts/elasticsearch-and-kibana/README.md) deployments and services for the operator's logs.
1616
* [Sample for generating a self-signed certificate and private key](scripts/rest/README.md) that can be used for the operator's external REST API.
17+
* [Sample for generating OKE cluster using Terraform](scripts/terraform/README.md).
1718

1819
## Sample Helm charts
1920

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Sample to create OKE cluster using Terraform scripts
2+
3+
The provided sample will create:
4+
5+
A new Virtual Cloud Network (VCN) for the cluster
6+
7+
2 LoadBalancer subnets with seclists
8+
9+
3 Worker subnets with seclists
10+
11+
A Kubernetes Cluster with one NodePool
12+
13+
A kubeconfig file to allow access using kubectl
14+
15+
Nodes and network settings will be configured to allow SSH access, and the cluster Networking policies will allow NodePort services to be exposed.
16+
17+
By default all OCI Container Engine for Kubernetes Cluster masters are Highly Available (HA) and fronted by load balancers.
18+
19+
20+
21+
Prerequisites
22+
23+
To use these Terraform scripts, you will need fulfill the following prerequisites:
24+
25+
Have an existing tenancy with enough compute and networking resources available for the desired cluster
26+
27+
Have an OCI Container Engine for Kubernetes policy in place within that tenancy to allow the OCI Container Engine for Kubernetes service to manage tenancy resources
28+
29+
Install Terraform with the OCI plugin as described here.
30+
31+
Have a user defined within that tenancy
32+
33+
Have an API key defined for use with the OCI API, as documented here
34+
35+
Have an SSH key pair with file permission 600 ready for configuring SSH access to the nodes in the cluster
36+
37+
38+
Copy provided oci.props.template file to oci.props and add all required values.
39+
40+
The syntax of the script is:
41+
```
42+
$ kubernetes/samples/scripts/terraform/oke.create.sh oci.props
43+
```
44+
The scripts collects the values from oci.props file and performs the following steps:
45+
Create a new tfvars file based on the values from the provided oci.props file.
46+
Downloads and installs all needed binaries for Terraform, Terraform OCI Provider and Go, based on OS system ( Mac or Linux)
47+
Apply the configuration and creates OKE Cluster using Terraform
48+
49+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
variable "cluster_kubernetes_version" { default = "v1.10.3" }
2+
variable "cluster_name" { default = "tfTestCluster" }
3+
variable "cluster_options_add_ons_is_kubernetes_dashboard_enabled" { default = true }
4+
variable "cluster_options_add_ons_is_tiller_enabled" { default = true }
5+
variable "cluster_options_kubernetes_network_config_pods_cidr" { default = "10.1.0.0/16" }
6+
variable "cluster_options_kubernetes_network_config_services_cidr" { default = "10.2.0.0/16" }
7+
variable "node_pool_initial_node_labels_key" { default = "key" }
8+
variable "node_pool_initial_node_labels_value" { default = "value" }
9+
variable "node_pool_kubernetes_version" { default = "v1.10.3" }
10+
variable "node_pool_name" { default = "tfTestCluster_workers" }
11+
variable "node_pool_node_image_name" { default = "Oracle-Linux-7.4" }
12+
variable "node_pool_node_shape" { default = "VM.Standard1.1" }
13+
variable "node_pool_quantity_per_subnet" { default = 2 }
14+
variable "node_pool_ssh_public_key" { }
15+
16+
data "oci_identity_availability_domains" "test_availability_domains" {
17+
compartment_id = "${var.compartment_ocid}"
18+
}
19+
20+
// Defined in oke.tf
21+
/*resource "oci_core_virtual_network" "oke-vcn" {
22+
cidr_block = "${var.vcn_cidr}"
23+
compartment_id = "${var.compartment_ocid}"
24+
display_name = "${var.cluster_name}_vcn"
25+
}*/
26+
27+
resource "oci_containerengine_cluster" "test_cluster" {
28+
#Required
29+
compartment_id = "${var.compartment_ocid}"
30+
kubernetes_version = "${var.cluster_kubernetes_version}"
31+
name = "${var.cluster_name}"
32+
vcn_id = "${oci_core_virtual_network.oke-vcn.id}"
33+
34+
#Optional
35+
options {
36+
service_lb_subnet_ids = ["${oci_core_subnet.oke-subnet-loadbalancer-1.id}", "${oci_core_subnet.oke-subnet-loadbalancer-2.id}"]
37+
38+
#Optional
39+
add_ons {
40+
#Optional
41+
is_kubernetes_dashboard_enabled = "${var.cluster_options_add_ons_is_kubernetes_dashboard_enabled}"
42+
is_tiller_enabled = "${var.cluster_options_add_ons_is_tiller_enabled}"
43+
}
44+
#kubernetes_network_config {
45+
#Optional
46+
#pods_cidr = "${var.cluster_options_kubernetes_network_config_pods_cidr}"
47+
#services_cidr = "${var.cluster_options_kubernetes_network_config_services_cidr}"
48+
#}
49+
}
50+
}
51+
52+
resource "oci_containerengine_node_pool" "test_node_pool" {
53+
#Required
54+
cluster_id = "${oci_containerengine_cluster.test_cluster.id}"
55+
compartment_id = "${var.compartment_ocid}"
56+
kubernetes_version = "${var.node_pool_kubernetes_version}"
57+
name = "${var.node_pool_name}"
58+
node_image_name = "${var.node_pool_node_image_name}"
59+
node_shape = "${var.node_pool_node_shape}"
60+
subnet_ids = ["${oci_core_subnet.oke-subnet-worker-1.id}", "${oci_core_subnet.oke-subnet-worker-2.id}","${oci_core_subnet.oke-subnet-worker-3.id}"]
61+
62+
#Optional
63+
#initial_node_labels {
64+
65+
#Optional
66+
# key = "${var.node_pool_initial_node_labels_key}"
67+
# value = "${var.node_pool_initial_node_labels_value}"
68+
#}
69+
quantity_per_subnet = "${var.node_pool_quantity_per_subnet}"
70+
ssh_public_key = "${var.node_pool_ssh_public_key}"
71+
}
72+
73+
output "cluster_id" {
74+
value = "${oci_containerengine_cluster.test_cluster.id}"
75+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
variable "cluster_kube_config_expiration" { default = 2592000 }
2+
variable "cluster_kube_config_token_version" { default = "1.0.0" }
3+
4+
data "oci_containerengine_cluster_kube_config" "test_cluster_kube_config" {
5+
#Required
6+
cluster_id = "${oci_containerengine_cluster.test_cluster.id}"
7+
8+
#Optional
9+
#expiration = "${var.cluster_kube_config_expiration}"
10+
#token_version = "${var.cluster_kube_config_token_version}"
11+
}
12+
13+
resource "local_file" "test_cluster_kube_config_file" {
14+
content = "${data.oci_containerengine_cluster_kube_config.test_cluster_kube_config.content}"
15+
filename = "${path.module}/${var.cluster_name}_kubeconfig"
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
user.ocid=
2+
okeclustername=
3+
tfvars.filename=
4+
tenancy.ocid=
5+
compartment.ocid=
6+
compartment.name=
7+
ociapi.pubkey.fingerprint=
8+
ocipk.path=
9+
vcn.cidr.prefix=
10+
vcn.cidr=
11+
nodepool.shape=
12+
k8s.version=
13+
nodepool.ssh.pubkey=
14+
terraform.installdir=
15+
go.installdir=
16+
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/bin/bash
2+
# Copyright 2017, Oracle Corporation and/or its affiliates. All rights reserved.
3+
4+
5+
function prop {
6+
grep "${1}" ${propsFile}|cut -d'=' -f2
7+
}
8+
function generateTFVarFile {
9+
10+
tfVarsFiletfVarsFile=${terraformVarDir}/${clusterTFVarsFile}.tfvars
11+
rm -f ${tfVarsFiletfVarsFile}
12+
cp ${terraformVarDir}/template.tfvars $tfVarsFiletfVarsFile
13+
chmod 777 ${terraformVarDir}/template.tfvars $tfVarsFiletfVarsFile
14+
15+
sed -i -e "s:@TENANCYOCID@:${tenancy_ocid}:g" ${tfVarsFiletfVarsFile}
16+
sed -i -e "s:@USEROCID@:${user_ocid}:g" ${tfVarsFiletfVarsFile}
17+
sed -i -e "s:@COMPARTMENTOCID@:${compartment_ocid}:g" ${tfVarsFiletfVarsFile}
18+
sed -i -e "s:@COMPARTMENTNAME@:${compartment_name}:g" ${tfVarsFiletfVarsFile}
19+
sed -i -e "s:@OKECLUSTERNAME@:${okeclustername}:g" ${tfVarsFiletfVarsFile}
20+
sed -i -e "s:@OCIAPIPUBKEYFINGERPRINT@:"${ociapi_pubkey_fingerprint}":g" ${tfVarsFiletfVarsFile}
21+
sed -i -e "s:@OCIPRIVATEKEYPATH@:${ocipk_path}:g" ${tfVarsFiletfVarsFile}
22+
sed -i -e "s:@VCNCIDRPREFIX@:${vcn_cidr_prefix}:g" ${tfVarsFiletfVarsFile}
23+
sed -i -e "s:@VCNCIDR@:${vcn_cidr_prefix}.0.0/16:g" ${tfVarsFiletfVarsFile}
24+
sed -i -e "s:@OKEK8SVERSION@:${k8s_version}:g" ${tfVarsFiletfVarsFile}
25+
sed -i -e "s:@NODEPOOLSHAPE@:${nodepool_shape}:g" ${tfVarsFiletfVarsFile}
26+
sed -i -e "s:@NODEPOOLSSHPUBKEY@:${nodepool_ssh_pubkey}:g" ${tfVarsFiletfVarsFile}
27+
echo "Generated TFVars file [${tfVarsFiletfVarsFile}]"
28+
29+
}
30+
31+
function setupTerraform () {
32+
mkdir ${terraformDir}
33+
cd ${terraformDir}
34+
if [[ "${OSTYPE}" == "darwin"* ]]; then
35+
curl -O https://releases.hashicorp.com/terraform/0.11.10/terraform_0.11.10_darwin_amd64.zip
36+
unzip terraform_0.11.10_darwin_amd64.zip
37+
elif [[ "${OSTYPE}" == "linux"* ]]; then
38+
curl -O https://releases.hashicorp.com/terraform/0.11.8/terraform_0.11.8_linux_amd64.zip
39+
unzip terraform_0.11.8_linux_amd64.zip
40+
else
41+
echo "Unsupported OS"
42+
fi
43+
chmod 777 ${terraformDir}/terraform
44+
export PATH=${PATH}:${terraformDir}
45+
46+
}
47+
48+
function buildTerraformOCIProvider() {
49+
mkdir -p ${goDir}/go/src/github.com/terraform-providers; cd ${goDir}/go/src/github.com/terraform-providers
50+
git clone https://github.com/terraform-providers/terraform-provider-oci.git
51+
cd ${goDir}/go/src/github.com/terraform-providers/terraform-provider-oci
52+
#somehow it does not build first time need to run gofmt and rebuild
53+
make gofmt
54+
make fmt
55+
make build
56+
if ! [ -d ~/.terraform.d ]; then
57+
echo "Creating terraform plugins dir"
58+
mkdir ~/.terraform.d
59+
fi
60+
if ! [ -d ~/.terraform.d/plugins ]; then
61+
mkdir ~/.terraform.d/plugins
62+
fi
63+
cp ${goDir}/go/bin/terraform-provider-oci ~/.terraform.d/plugins/
64+
if [ -e ~/.terraformrc ]; then
65+
rm ~/.terraformrc
66+
fi
67+
cat ${terraformVarDir}/terraformrc >> ~/.terraformrc
68+
}
69+
70+
function createCluster () {
71+
cd ${terraformVarDir}
72+
echo "terraform init -var-file=${terraformVarDir}/${clusterTFVarsFile}.tfvars"
73+
terraform init -var-file=${terraformVarDir}/${clusterTFVarsFile}.tfvars
74+
terraform plan -var-file=${terraformVarDir}/${clusterTFVarsFile}.tfvars
75+
terraform apply -auto-approve -var-file=${terraformVarDir}/${clusterTFVarsFile}.tfvars
76+
}
77+
78+
function setupGo () {
79+
mkdir ${goDir}
80+
cd ${goDir}
81+
if [[ "${OSTYPE}" == "darwin"* ]]; then
82+
curl -O https://dl.google.com/go/go1.11.2.darwin-amd64.tar.gz
83+
tar -xvf go1.11.2.darwin-amd64.tar.gz
84+
elif [[ "${OSTYPE}" == "linux"* ]]; then
85+
curl -O https://dl.google.com/go/go1.11.linux-amd64.tar.gz
86+
tar -xvf go1.11.linux-amd64.tar.gz
87+
else
88+
echo "Unsupported OS"
89+
fi
90+
chmod 777 ${goDir}/go/bin
91+
export PATH=${PATH}:${goDir}/go/bin
92+
93+
}
94+
95+
96+
97+
#MAIN
98+
terraformVarDir=${2:-$PWD}
99+
propsFile=${1:-$PWD/oci.props}
100+
101+
102+
clusterTFVarsFile=$(prop 'tfvars.filename')
103+
tenancy_ocid=$(prop 'tenancy.ocid')
104+
user_ocid=$(prop 'user.ocid')
105+
compartment_ocid=$(prop 'compartment.ocid')
106+
compartment_name=$(prop 'compartment.name')
107+
okeclustername=$(prop 'okeclustername')
108+
ociapi_pubkey_fingerprint=$(prop 'ociapi.pubkey.fingerprint')
109+
ocipk_path=$(prop 'ocipk.path')
110+
vcn_cidr_prefix=$(prop 'vcn.cidr.prefix')
111+
k8s_version=$(prop 'k8s.version')
112+
nodepool_shape=$(prop 'nodepool.shape')
113+
nodepool_ssh_pubkey=$(prop 'nodepool.ssh.pubkey')
114+
terraformDir=$(prop 'terraform.installdir')
115+
goDir=$(prop 'go.installdir')
116+
generateTFVarFile
117+
rm -rf ${goDir} ${terraformDir}
118+
setupTerraform
119+
setupGo
120+
buildTerraformOCIProvider
121+
chmod 600 ${ocipk_path}
122+
createCluster
123+
export KUBECONFIG=${terraformVarDir}/${okeclustername}_kubeconfig
124+
125+
126+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* This example file shows how to configure the oci provider to target the a single region.
3+
*/
4+
5+
// These variables would commonly be defined as environment variables or sourced in a .env file
6+
variable "tenancy_ocid" {}
7+
variable "user_ocid" {}
8+
variable "fingerprint" {}
9+
variable "private_key_path" {}
10+
variable "region" { default = "us-phoenix-1" }
11+
12+
provider "oci" {
13+
region = "${var.region}"
14+
tenancy_ocid = "${var.tenancy_ocid}"
15+
user_ocid = "${var.user_ocid}"
16+
fingerprint = "${var.fingerprint}"
17+
private_key_path = "${var.private_key_path}"
18+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Example TF variables file for cluster creation
2+
#
3+
# Clone this and upate it with your own info as needed
4+
#
5+
6+
#
7+
# User-specific vars - you can get these easily from the OCI console from your user page
8+
#
9+
10+
# OCID can be obtained from the user info page in the OCI console
11+
user_ocid="@USEROCID@"
12+
# API key fingerprint and private key location, needed for API access -- you should have added a public API key through the OCI console first
13+
fingerprint="@OCIAPIPUBKEYFINGERPRINT@"
14+
private_key_path="@OCIPRIVATEKEYPATH@"
15+
16+
# Required tenancy vars
17+
tenancy_ocid="@TENANCYOCID@"
18+
compartment_ocid="@COMPARTMENTOCID@"
19+
compartment_name="@COMPARTMENTNAME@"
20+
21+
#
22+
# Cluster-specific vars
23+
#
24+
25+
# VCN CIDR -- must be unique within the compartment in the tenancy
26+
# - assuming 1:1 cluster:vcn
27+
# - this can be obtained either through OCI console, or from the Otto clusters page https://confluence.oraclecorp.com/confluence/display/ODX/Otto+OKE+Clusters
28+
#
29+
# BE SURE TO SET BOTH VARS -- the first 2 octets for each variable have to match
30+
vcn_cidr_prefix="@VCNCIDRPREFIX@"
31+
vcn_cidr="@VCNCIDR@"
32+
33+
# Cluster name and k8s version
34+
cluster_kubernetes_version="@OKEK8SVERSION@"
35+
cluster_name="@OKECLUSTERNAME@"
36+
37+
# Node pool info
38+
node_pool_kubernetes_version="@OKEK8SVERSION@"
39+
node_pool_name="@OKECLUSTERNAME@_workers"
40+
node_pool_node_shape="@NODEPOOLSHAPE@"
41+
node_pool_quantity_per_subnet=1
42+
43+
# SSH public key, for SSH access to nodes in the cluster
44+
node_pool_ssh_public_key="@NODEPOOLSSHPUBKEY@"
45+
46+
47+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Example of terraform plugin file
2+
providers {
3+
oci = "${HOME}/.terraform.d/plugins/terraform-provider-oci"
4+
}

0 commit comments

Comments
 (0)