Skip to content

Commit 6862f54

Browse files
authored
Merge pull request #468 from ovh/add-kube-example
feat: add a full example with kube, node pool and an app
2 parents 568ef4c + bcece77 commit 6862f54

File tree

9 files changed

+215
-0
lines changed

9 files changed

+215
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ terraform-provider-ovh
3232
# Test exclusions
3333
!command/test-fixtures/**/*.tfstate
3434
!command/test-fixtures/**/.terraform/
35+
examples/kube-nodepool-deployment/.terraform.lock.hcl
36+
examples/kube-nodepool-deployment/.terraform.tfstate.lock.info
37+
examples/kube-nodepool-deployment/logs
38+
examples/kube-nodepool-deployment/my-kube-cluster-*.yml

examples/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# OVH Terraform Provider examples
2+
3+
The following examples demonstrate various ways to create and work with [OVHcloud](https://www.ovhcloud.com/fr/) Cloud provider.
4+
5+
1. [Deploy an OVHcloud Manager Kubernetes cluster, a Node Pool and an application](./kube-nodepool-deployment)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Deploy an OVHcloud Manager Kubernetes cluster, a Node Pool and an application
2+
3+
Through this procedure, you can [create multiple OVHcloud Managed Kubernetes, through Terraform](https://docs.ovh.com/gb/en/kubernetes/creating-a-cluster-through-terraform/).
4+
5+
You will create:
6+
7+
* in parallel, several Kubernetes clusters in OVH (depending on the number setted in `scripts/create.sh` script, by default is one cluster)
8+
* a node pool with 3 nodes
9+
* a functional deployment (an app)
10+
* and a service of type Load Balancer
11+
12+
# Prerequisites
13+
14+
Generate [OVH API credentials](https://api.ovh.com/createToken/?GET=/*&POST=/*&PUT=/*&DELETE=/*) and then export in environment variables in your machine like this:
15+
16+
```
17+
$ export OVH_ENDPOINT="ovh-eu"
18+
$ export OVH_APPLICATION_KEY="xxx"
19+
$ export OVH_APPLICATION_SECRET="xxxxx"
20+
$ export OVH_CONSUMER_KEY="xxxxx"
21+
```
22+
23+
Or you can directly put them in `provider.tf` in OVH provider definition:
24+
25+
```
26+
provider "ovh" {
27+
version = "~> 0.16"
28+
endpoint = "ovh-eu"
29+
application_key = "xxx"
30+
application_secret = "xxx"
31+
consumer_key = "xxx"
32+
}
33+
```
34+
35+
Set in `variables.tf` your service_name parameter (Public Cloud project ID):
36+
37+
```
38+
variable "service_name" {
39+
default = "xxxxx"
40+
}
41+
```
42+
43+
# How To
44+
45+
Create the Kubernetes clusters and for each, apply a deployment a service and when the OVHcloud Load Balancer is created, curl the app:
46+
47+
```
48+
./scripts/create.sh
49+
```
50+
51+
Output are writted in `logs` file.
52+
Display the logs in realtime:
53+
54+
```sh
55+
$ tail -f logs
56+
```
57+
58+
# Clean
59+
60+
You can remove/destroy generated files and OVHcloud resources:
61+
62+
```
63+
./scripts/clean.sh
64+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: hello-world
5+
labels:
6+
app: hello-world
7+
spec:
8+
type: LoadBalancer
9+
ports:
10+
- port: 80
11+
targetPort: 80
12+
protocol: TCP
13+
name: http
14+
selector:
15+
app: hello-world
16+
---
17+
apiVersion: apps/v1
18+
kind: Deployment
19+
metadata:
20+
name: hello-world-deployment
21+
labels:
22+
app: hello-world
23+
spec:
24+
replicas: 1
25+
selector:
26+
matchLabels:
27+
app: hello-world
28+
template:
29+
metadata:
30+
labels:
31+
app: hello-world
32+
spec:
33+
containers:
34+
- name: hello-world
35+
image: ovhplatform/hello
36+
ports:
37+
- containerPort: 80
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "ovh_cloud_project_kube" "my_kube_cluster" {
2+
count = var.nb_cluster
3+
service_name = var.service_name
4+
name = "my-cluster-${count.index}"
5+
region = "GRA5"
6+
}
7+
8+
resource "ovh_cloud_project_kube_nodepool" "node_pool" {
9+
count = var.nb_cluster
10+
service_name = var.service_name
11+
kube_id = ovh_cloud_project_kube.my_kube_cluster[count.index].id
12+
name = "my-pool-${count.index}" //Warning: "_" char is not allowed!
13+
flavor_name = "b2-7"
14+
desired_nodes = 3
15+
max_nodes = 3
16+
min_nodes = 3
17+
}
18+
19+
resource "local_file" "kubeconfig" {
20+
count = var.nb_cluster
21+
content = ovh_cloud_project_kube.my_kube_cluster[count.index].kubeconfig
22+
filename = "my-kube-cluster-${count.index}.yml"
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Configure the OVHcloud Provider
2+
terraform {
3+
required_providers {
4+
ovh = {
5+
source = "ovh/ovh"
6+
}
7+
}
8+
}
9+
10+
provider "ovh" {
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# kill still running processes in case of errors
4+
ps -ef| grep -i "sleep 10" | grep -v grep| awk '{print "kill "$2}' | sh
5+
ps -ef| grep -i "create" | grep -v grep| awk '{print "kill "$2}' | sh
6+
7+
# Destroy/Remove created resources
8+
terraform destroy -auto-approve
9+
10+
# Clean logs
11+
> ../logs
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
exec &> logs
4+
5+
NB_CLUSTER=1
6+
let "MAX=NB_CLUSTER-1"
7+
8+
terraform init
9+
10+
START=$(date +%s)
11+
date
12+
13+
terraform plan -var="nb_cluster=${NB_CLUSTER}"
14+
15+
terraform apply -var="nb_cluster=${NB_CLUSTER}" -auto-approve
16+
17+
date
18+
19+
apply_deploy() {
20+
KUBECONFIG_FILE="my-kube-cluster-$1.yml"
21+
kubectl --kubeconfig=$KUBECONFIG_FILE get nodes
22+
kubectl --kubeconfig=$KUBECONFIG_FILE apply -f hello.yaml -n default
23+
kubectl --kubeconfig=$KUBECONFIG_FILE get all -n default
24+
kubectl --kubeconfig=$KUBECONFIG_FILE get services -n default -l app=hello-world
25+
26+
ip=""
27+
while [ -z $ip ]; do
28+
echo "Waiting for external IP"
29+
ip=$(kubectl --kubeconfig=${KUBECONFIG_FILE} -n default get service hello-world -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
30+
[ -z "$ip" ] && sleep 10
31+
done
32+
echo 'Found external IP: '$ip
33+
export APP_IP=$ip
34+
echo $APP_IP
35+
curl $APP_IP
36+
}
37+
38+
wait
39+
40+
# Deploy on each cluster as jobs
41+
for ((i=0;i<=$MAX;i++));
42+
do
43+
apply_deploy $i &
44+
done
45+
46+
# Wait for jobs to finish
47+
wait
48+
49+
date
50+
END=$(date +%s)
51+
52+
echo $((END-START)) | awk '{printf "%d:%02d:%02d", $1/3600, ($1/60)%60, $1%60}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
variable "service_name" {
2+
default = "xxxxx"
3+
}
4+
5+
variable "nb_cluster" {
6+
default = 1
7+
}

0 commit comments

Comments
 (0)