{"id":15874191,"url":"https://github.com/garutilorenzo/k8s-oci-cluster","last_synced_at":"2025-07-14T10:33:08.924Z","repository":{"id":154321658,"uuid":"483597219","full_name":"garutilorenzo/k8s-oci-cluster","owner":"garutilorenzo","description":"Deploy a Kubernetes cluster for free, with kubeadm and Oracle always free resources.","archived":false,"fork":false,"pushed_at":"2023-07-27T12:26:27.000Z","size":174,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T18:23:41.813Z","etag":null,"topics":["automation","iac","k8s","k8s-cluster","kubernetes","kubernetes-cluster","oci","oracle","oracle-cloud","terraform","terraform-module"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/garutilorenzo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"security.tf","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"garutilorenzo"}},"created_at":"2022-04-20T09:51:18.000Z","updated_at":"2024-05-10T20:32:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"76fadb7d-84d8-4514-9696-bd18e5e60c5e","html_url":"https://github.com/garutilorenzo/k8s-oci-cluster","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/garutilorenzo/k8s-oci-cluster","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garutilorenzo%2Fk8s-oci-cluster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garutilorenzo%2Fk8s-oci-cluster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garutilorenzo%2Fk8s-oci-cluster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garutilorenzo%2Fk8s-oci-cluster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/garutilorenzo","download_url":"https://codeload.github.com/garutilorenzo/k8s-oci-cluster/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garutilorenzo%2Fk8s-oci-cluster/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265280615,"owners_count":23739851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","iac","k8s","k8s-cluster","kubernetes","kubernetes-cluster","oci","oracle","oracle-cloud","terraform","terraform-module"],"created_at":"2024-10-06T01:21:17.992Z","updated_at":"2025-07-14T10:33:08.900Z","avatar_url":"https://github.com/garutilorenzo.png","language":"HCL","funding_links":["https://github.com/sponsors/garutilorenzo"],"categories":[],"sub_categories":[],"readme":"[![Wordpress CI](https://github.com/garutilorenzo/k8s-oci-cluster/actions/workflows/ci.yml/badge.svg)](https://github.com/garutilorenzo/k8s-oci-cluster/actions/workflows/ci.yml)\n[![GitHub issues](https://img.shields.io/github/issues/garutilorenzo/k8s-oci-cluster)](https://github.com/garutilorenzo/k8s-oci-cluster/issues)\n![GitHub](https://img.shields.io/github/license/garutilorenzo/k8s-oci-cluster)\n[![GitHub forks](https://img.shields.io/github/forks/garutilorenzo/k8s-oci-cluster)](https://github.com/garutilorenzo/k8s-oci-cluster/network)\n[![GitHub stars](https://img.shields.io/github/stars/garutilorenzo/k8s-oci-cluster)](https://github.com/garutilorenzo/k8s-oci-cluster/stargazers)\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://garutilorenzo.github.io/images/k8s-logo.png?\" alt=\"k8s Logo\"/\u003e\n\u003c/p\u003e\n\n# Deploy Kubernetes on Oracle Cloud Infrastructure (OCI)\n\nDeploy a Kubernetes cluster for free, with kubeadm and Oracle [always free](https://docs.oracle.com/en-us/iaas/Content/FreeTier/freetier_topic-Always_Free_Resources.htm) resources.\n\nThis project is based on the [OCI K3s cluster](https://github.com/garutilorenzo/k3s-oci-cluster) repository. The final result is the same, but in this repo  kubeadm is used to install the official Kubernetes release. For more information read [Kubernetes setup](#kubernetes-setup).\n\n# Table of Contents\n\n- [Kubernetes on OCI](#deploy-kubernetes-on-amazon-aws)\n- [Table of Contents](#table-of-contents)\n    - [Important notes](#important-notes)\n    - [Requirements](#requirements)\n    - [Supported OS](#supported-os)\n    - [Terraform OCI user creation (Optional)](#terraform-oci-user-creation-optional)\n      - [Example RSA key generation](#example-rsa-key-generation)\n    - [Project setup](#project-setup)\n    - [Oracle provider setup](#oracle-provider-setup)\n    - [Pre flight checklist](#pre-flight-checklist)\n      - [How to find the availability doamin name](#how-to-find-the-availability-doamin-name)\n      - [How to list all the OS images](#how-to-list-all-the-os-images)\n  - [Notes about OCI always free resources](#notes-about-oci-always-free-resources)\n  - [Infrastructure overview](#infrastructure-overview)\n  - [Kubernetes setup](#kubernetes-setup)\n  - [Cluster resource deployed](#cluster-resource-deployed)\n    - [Nginx ingress controller](#nginx-ingress-controller)\n    - [Cert-manager](#cert-manager)\n  - [Deploy](#deploy)\n  - [Deploy a sample stack](#deploy-a-sample-stack)\n  - [Clean up](#clean-up)\n\n**Note** choose a region with enough ARM capacity\n\n### Important notes\n\n* This is repo shows only how to use terraform with the Oracle Cloud infrastructure and use only the **always free** resources. This examples are **not** for a production environment.\n* At the end of your trial period (30 days). All the paid resources deployed will be stopped/terminated\n* At the end of your trial period (30 days), if you have a running compute instance it will be stopped/hibernated\n\n### Requirements\n\nTo use this repo you will need:\n\n* an Oracle Cloud account. You can register [here](https://cloud.oracle.com)\n\nOnce you get the account, follow the *Before you begin* and *1. Prepare* step in [this](https://docs.oracle.com/en-us/iaas/developer-tutorials/tutorials/tf-provider/01-summary.htm) document.\n\n### Supported OS\n\nThis module was tested with:\n\n* Ubuntu 22.04 (ubuntu remote user)\n* Ubuntu 22.04 Minimal (ubuntu remote user)\n* Oracle Linux 8 (opc remote user)\n\n### Terraform OCI user creation (Optional)\n\nIs always recommended to create a separate user and group in your preferred [domain](https://cloud.oracle.com/identity/domains) to use with Terraform.\nThis user must have less privileges possible (Zero trust policy). Below is an example policy that you can [create](https://cloud.oracle.com/identity/policies) allow `terraform-group` to manage all the resources needed by this module:\n\n```\nAllow group terraform-group to manage virtual-network-family in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage instance-family in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage compute-management-family in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage volume-family in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage load-balancers in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage network-load-balancers in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage dynamic-groups in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage policies in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage secret-family in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage key-family in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage secrets in compartment id \u003ccompartment_ocid\u003e\nAllow group terraform-group to manage vaults in compartment id \u003ccompartment_ocid\u003e\n```\n\nSee [how](#oracle-provider-setup) to find the compartment ocid. The user and the group have to be manually created before using this module.\nTo create the user go to **Identity \u0026 Security -\u003e Users**, then create the group in **Identity \u0026 Security -\u003e Groups** and associate the newly created user to the group. The last step is to create the policy in **Identity \u0026 Security -\u003e Policies**.\n\n#### Example RSA key generation\n\nTo use terraform with the Oracle Cloud infrastructure you need to generate an RSA key. Generate the rsa key with:\n\n```\nopenssl genrsa -out ~/.oci/\u003cyour_name\u003e-oracle-cloud.pem 4096\nchmod 600 ~/.oci/\u003cyour_name\u003e-oracle-cloud.pem\nopenssl rsa -pubout -in ~/.oci/\u003cyour_name\u003e-oracle-cloud.pem -out ~/.oci/\u003cyour_name\u003e-oracle-cloud_public.pem\n```\n\nreplace `\u003cyour_name\u003e` with your name or a string you prefer.\n\n**NOTE**: `~/.oci/\u003cyour_name\u003e-oracle-cloud_public.pem` will be used in  `terraform.tfvars` by the Oracle provider plugin, so please take note of this string.\n\n### Project setup\n\nClone this repo and go in the `example/` directory:\n\n```\ngit clone https://github.com/garutilorenzo/k8s-oci-cluster.git\ncd k8s-oci-cluster/example/\n```\n\nNow you have to edit the `main.tf` file and you have to create the `terraform.tfvars` file. For more detail see [Oracle provider setup](#oracle-provider-setup) and [Pre flight checklist](#pre-flight-checklist).\n\nOr if you prefer you can create an new empty directory in your workspace and create this three files:\n\n* `terraform.tfvars` - More details in [Oracle provider setup](#oracle-provider-setup)\n* `main.tf`\n* `provider.tf`\n\nThe `main.tf` file will look like:\n\n```\nvariable \"compartment_ocid\" {\n\n}\n\nvariable \"tenancy_ocid\" {\n\n}\n\nvariable \"user_ocid\" {\n\n}\n\nvariable \"fingerprint\" {\n\n}\n\nvariable \"private_key_path\" {\n\n}\n\nvariable \"region\" {\n  default = \"\u003cchange_me\u003e\"\n}\n\nmodule \"k8s_cluster\" {\n  public_key_path     = \"\u003cchange_me\u003e\"\n  region                 = var.region\n  availability_domain    = \"\u003cchange_me\u003e\"\n  compartment_ocid       = var.compartment_ocid\n  my_public_ip_cidr      = \"\u003cchange_me\u003e\"\n  environment            = \"staging\"\n  install_longhorn       = true\n  install_nginx_ingress  = true\n  source                 = \"github.com/garutilorenzo/k8s-oci-cluster\"\n}\n\noutput \"k8s_servers_ips\" {\n  value = module.k8s_cluster.k8s_servers_ips\n}\n\noutput \"k8s_workers_ips\" {\n  value = module.k8s_cluster.k8s_workers_ips\n}\n\noutput \"public_lb_ip\" {\n  value = module.k8s_cluster.public_lb_ip\n}\n```\n\nFor all the possible variables see [Pre flight checklist](#pre-flight-checklist)\n\nThe `provider.tf` will look like:\n\n```\nprovider \"oci\" {\n  tenancy_ocid     = var.tenancy_ocid\n  user_ocid        = var.user_ocid\n  private_key_path = var.private_key_path\n  fingerprint      = var.fingerprint\n  region           = var.region\n}\n```\n\nNow we can init terraform with:\n\n```\nterraform init\n\nterraform init\nInitializing modules...\nDownloading git::https://github.com/garutilorenzo/k8s-oci-cluster.git for k8s_cluster...\n- k8s_cluster in .terraform/modules/k8s_cluster\n\nInitializing the backend...\n\nInitializing provider plugins...\n- Reusing previous version of hashicorp/oci from the dependency lock file\n- Reusing previous version of hashicorp/template from the dependency lock file\n- Using previously-installed hashicorp/template v2.2.0\n- Using previously-installed hashicorp/oci v4.64.0\n\nTerraform has been successfully initialized!\n\nYou may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.\n```\n\n### Oracle provider setup\n\nIn the `example/` directory of this repo you need to create a `terraform.tfvars` file, the file will look like:\n\n```\nfingerprint      = \"\u003crsa_key_fingerprint\u003e\"\nprivate_key_path = \"~/.oci/\u003cyour_name\u003e-oracle-cloud_public.pem\"\nuser_ocid        = \"\u003cuser_ocid\u003e\"\ntenancy_ocid     = \"\u003ctenency_ocid\u003e\"\ncompartment_ocid = \"\u003ccompartment_ocid\u003e\"\n```\n\nTo find your `tenency_ocid` in the Ocacle Cloud console go to: **Governance and Administration \u003e Tenency details**, then copy the OCID.\n\nTo find you `user_ocid` in the Ocacle Cloud console go to **User setting** (click on the icon in the top right corner, then click on User settings), click your username and then copy the OCID.\n\nThe `compartment_ocid` is the same as `tenency_ocid`.\n\nThe fingerprint is the fingerprint of your RSA key, you can find this vale under **User setting \u003e API Keys**.\n\n### Pre flight checklist\n\nOnce you have created the terraform.tfvars file edit the `main.tf` file (always in the `example/` directory) and set the following variables:\n\n| Var   | Required | Desc |\n| ------- | ------- | ----------- |\n| `region`       | `yes`       | set the correct OCI region based on your needs  |\n| `availability_domain` | `yes`        | Set the correct availability domain. See [how](#how-to-find-the-availability-doamin-name) to find the availability domain|\n| `compartment_ocid` | `yes`        | Set the correct compartment ocid. See [how](#oracle-provider-setup) to find the compartment ocid |\n| `my_public_ip_cidr` | `yes`        |  your public ip in cidr format (Example: 195.102.xxx.xxx/32) |\n| `environment`  | `yes`  | Current work environment (Example: staging/dev/prod). This value is used for tag all the deployed resources |\n| `os_image_id`  | `yes`  | Image id to use. See [how](#how-to-list-all-the-os-images) to list all available OS images |\n| `compute_shape`  | `no`  | Compute shape to use. Default VM.Standard.A1.Flex. **NOTE** Is mandatory to use this compute shape for provision 4 always free VMs |\n| `oci_core_vcn_dns_label`  | `no`  | VCN DNS label. Default: defaultvcn |\n| `oci_core_subnet_dns_label10`  | `no`  | First subnet DNS label. Default: defaultsubnet10 |\n| `oci_core_subnet_dns_label11`  | `no`  | Second subnet DNS label. Default: defaultsubnet11 |\n| `oci_core_vcn_cidr`  | `no`  | VCN CIDR. Default: oci_core_vcn_cidr |\n| `oci_core_subnet_cidr10`  | `no`  | First subnet CIDR. Default: 10.0.0.0/24 |\n| `oci_core_subnet_cidr11`  | `no`  | Second subnet CIDR. Default: 10.0.1.0/24 |\n| `oci_identity_dynamic_group_name`  | `no`  | Dynamic group name. This dynamic group will contains all the instances of this specific compartment. Default: Compute_Dynamic_Group |\n| `oci_identity_policy_name`  | `no`  | Policy name. This policy will allow dynamic group 'oci_identity_dynamic_group_name' to read OCI api without auth. Default: Compute_To_Oci_Api_Policy |\n| `k8s_load_balancer_name`  | `no`  | Internal LB name. Default: k8s internal load balancer  |\n| `public_load_balancer_name`  | `no`  | Public LB name. Default: k8s public LB  |\n| `cluster_name`  | `no`  | Kubernetes cluster name. Default: kubernetes  |\n| `k8s_version`  | `no`  | Kubernetes version to install  |\n| `k8s_pod_subnet`  | `no`  | Kubernetes pod subnet managed by the CNI (Flannel). Default: 10.244.0.0/16 |\n| `k8s_service_subnet`  | `no`  | Kubernetes pod service managed by the CNI (Flannel). Default: 10.96.0.0/12 |\n| `k8s_dns_domain`  | `no`  | Internal kubernetes DNS domain. Default: cluster.local |\n| `kube_api_port`  | `no`  | Kube api default port Default: 6443  |\n| `public_lb_shape`  | `no`  | LB shape for the public LB. Default: flexible. **NOTE** is mandatory to use this kind of shape to provision two always free LB (public and private)  |\n| `http_lb_port`  | `no`  | http port used by the public LB. Default: 80  |\n| `https_lb_port`  | `no`  | http port used by the public LB. Default: 443  |\n| `ingress_controller_http_nodeport`  | `no`  | HTTP nodeport where nginx ingress controller will listen. Default: 30080 |\n| `ingress_controller_https_nodeport`  | `no`  | HTTPS nodeport where nginx ingress controller will listen. Default 30443 |\n| `k8s_server_pool_size`  | `no`  | Number of k8s servers deployed. Default 1  |\n| `k8s_worker_pool_size`  | `no`  | Number of k8s workers deployed. Default 2  |\n| `k8s_extra_worker_node`  | `no`  | Boolean value, default true. Deploy the third worker nodes. The node will be deployed outside the worker instance pools. Using OCI always free account you can't create instance pools with more than two servers. This workaround solve this problem.  |\n| `install_nginx_ingress`  | `no`  | Boolean value, install kubernetes nginx ingress controller. Default: false. |\n| `nginx_ingress_release`  | `no`  | Nginx ingress release to install. Default: v1.5.1|\n| `install_longhorn`  | `no`  | Boolean value, install longhorn \"Cloud native distributed block storage for Kubernetes\". Default: false  |\n| `longhorn_release`  | `no`  | Longhorn release. Default: v1.4.0  |\n| `install_certmanager`  | `no`  | Boolean value, install [cert manager](https://cert-manager.io/) \"Cloud native certificate management\". Default: true  |\n| `certmanager_release`  | `no`  | Cert manager release. Default: v1.11.0  |\n| `certmanager_email_address`  | `no`  | Email address used for signing https certificates. Defaul: changeme@example.com  |\n| `expose_kubeapi`  | `no`  | Boolean value, default false. Expose or not the kubeapi server to the internet. Access is granted only from my_public_ip_cidr for security reasons.  |\n| `hash_secret_name`  | `no`  | Secret name where kubernetes hash is stored  |\n| `token_secret_name`  | `no`  | Secret name where kubernetes token is stored  |\n| `cert_secret_name`  | `no`  | Secret name where kubernetes cert is stored  |\n| `kubeconfig_secret_name  | `no`  | Secret name where kubernetes kubeconfig is stored  |\n| `public_key_path`     | `no`       | Path to your public ssh key (Default: \"~/.ssh/id_rsa.pub) |\n\n#### How to find the availability doamin name\n\nTo find the list of the availability domains run this command on che Cloud Shell:\n\n```\noci iam availability-domain list\n{\n  \"data\": [\n    {\n      \"compartment-id\": \"\u003ccompartment_ocid\u003e\",\n      \"id\": \"ocid1.availabilitydomain.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\n      \"name\": \"iAdc:EU-ZURICH-1-AD-1\"\n    }\n  ]\n}\n```\n\n#### How to list all the OS images\n\nTo filter the OS images by shape and OS run this command on che Cloud Shell. You can filter by OS: Canonical Ubuntu or Oracle Linux:\n\n```\noci compute image list --compartment-id \u003ccompartment_ocid\u003e --operating-system \"Canonical Ubuntu\" --shape \"VM.Standard.A1.Flex\"\n{\n  \"data\": [\n    {\n      \"agent-features\": null,\n      \"base-image-id\": null,\n      \"billable-size-in-gbs\": 2,\n      \"compartment-id\": null,\n      \"create-image-allowed\": true,\n      \"defined-tags\": {},\n      \"display-name\": \"Canonical-Ubuntu-20.04-aarch64-2022.01.18-0\",\n      \"freeform-tags\": {},\n      \"id\": \"ocid1.image.oc1.eu-zurich-1.aaaaaaaag2uyozo7266bmg26j5ixvi42jhaujso2pddpsigtib6vfnqy5f6q\",\n      \"launch-mode\": \"NATIVE\",\n      \"launch-options\": {\n        \"boot-volume-type\": \"PARAVIRTUALIZED\",\n        \"firmware\": \"UEFI_64\",\n        \"is-consistent-volume-naming-enabled\": true,\n        \"is-pv-encryption-in-transit-enabled\": true,\n        \"network-type\": \"PARAVIRTUALIZED\",\n        \"remote-data-volume-type\": \"PARAVIRTUALIZED\"\n      },\n      \"lifecycle-state\": \"AVAILABLE\",\n      \"listing-type\": null,\n      \"operating-system\": \"Canonical Ubuntu\",\n      \"operating-system-version\": \"20.04\",\n      \"size-in-mbs\": 47694,\n      \"time-created\": \"2022-01-27T22:53:34.270000+00:00\"\n    },\n```\n\n**Note:** this setup was tested with Ubuntu 22.04 and Oracle Linux 8\n\n## Notes about OCI always free resources\n\nIn order to get the maximum resources available within the oracle always free tier and to get the k8s cluster in HA `k8s_server_pool_size` is set to 1 (single master node) and `k8s_worker_pool_size` is set to 2.\nWith the latest release was introduced the new variable `k8s_extra_worker_node`. With this variable set to `true` (Default) a third worker node will be added.\nIn previous releases the master nodws where two, but this didn't guarantee the HA of the cluster.\n\nIn this setup we use two LB, one internal LB (Layer 7) and one public LB (Layer 4). In order to use two LB using the always free resources, one lb must be a [network load balancer](https://docs.oracle.com/en-us/iaas/Content/NetworkLoadBalancer/introducton.htm#Overview) an the other must be a [load balancer](https://docs.oracle.com/en-us/iaas/Content/Balance/Concepts/balanceoverview.htm). The public LB **must** use the `flexible` shape (`public_lb_shape` variable).\n\n## Infrastructure overview\n\nThe final infrastructure will be made by:\n\n* two instance pool:\n  * one instance pool for the server nodes named `k8s-servers`\n  * one instance pool for the worker nodes named `k8s-workers`\n* one internal load balancer that will route traffic to k8s servers\n* one external load balancer that will route traffic to k8s workers\n\nThe other resources created by terraform are:\n\n* two instance configurations (one for the servers and one for the workers) used by the instance pools\n* one vcn\n* two public subnets\n* two security list\n* one dynamic group\n* one identity policy\n\n## Kubernetes setup\n\nThe installation of K8s id done by [kubeadm](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/). In this installation [Containerd](https://containerd.io/) is used as CRI and [flannel](https://github.com/flannel-io/flannel) is used as CNI.\n\nYou can optionally install [Nginx ingress controller](https://kubernetes.github.io/ingress-nginx/) and [Longhorn](#https://longhorn.io/).\n\nThis modules uses OCI vault secrets to store join certificates.\n\n## Cluster resource deployed\n\nYou can optionally install [longhorn](https://longhorn.io/). Longhorn is a *Cloud native distributed block storage for Kubernetes*. To enable the longhorn deployment set `install_longhorn` variable to `true`.\n\n**NOTE** to use longhorn set the `k8s_version` \u003c `v1.25.x` [Ref.](https://github.com/longhorn/longhorn/issues/4003)\n\n### Nginx ingress controller\n\nYou can optionally install [Nginx ingress controller](https://kubernetes.github.io/ingress-nginx/) To enable the longhorn deployment set `install_nginx_ingress` variable to `true`.\n\nThe installation is the [bare metal](https://kubernetes.github.io/ingress-nginx/deploy/#bare-metal-clusters) installation, the ingress controller then is exposed via a NodePort Service.\n\n```yaml\n---\napiVersion: v1\nkind: Service\nmetadata:\n  name: ingress-nginx-controller-loadbalancer\n  namespace: ingress-nginx\nspec:\n  selector:\n    app.kubernetes.io/component: controller\n    app.kubernetes.io/instance: ingress-nginx\n    app.kubernetes.io/name: ingress-nginx\n  ports:\n    - name: http\n      port: 80\n      protocol: TCP\n      targetPort: 80\n      nodePort: ${ingress_controller_http_nodeport}\n    - name: https\n      port: 443\n      protocol: TCP\n      targetPort: 443\n      nodePort: ${ingress_controller_https_nodeport}\n  type: NodePort\n```\n\nTo get the real ip address of the clients using a public L4 load balancer we need to use the proxy protocol feature of nginx ingress controller:\n\n```yaml\n---\napiVersion: v1\ndata:\n  allow-snippet-annotations: \"true\"\n  enable-real-ip: \"true\"\n  proxy-real-ip-cidr: \"0.0.0.0/0\"\n  proxy-body-size: \"20m\"\n  use-proxy-protocol: \"true\"\nkind: ConfigMap\nmetadata:\n  labels:\n    app.kubernetes.io/component: controller\n    app.kubernetes.io/instance: ingress-nginx\n    app.kubernetes.io/name: ingress-nginx\n    app.kubernetes.io/part-of: ingress-nginx\n    app.kubernetes.io/version: ${nginx_ingress_release}\n  name: ingress-nginx-controller\n  namespace: ingress-nginx\n```\n\n**NOTE** to use nginx ingress controller with the proxy protocol enabled, an external nginx instance is used as proxy (since OCI LB doesn't support proxy protocol at the moment). Nginx will be installed on each worker node and the configuation of nginx will:\n\n* listen in proxy protocol mode\n* forward the traffic from port `80` to `extlb_listener_http_port` (default to `30080`) on any server of the cluster\n* forward the traffic from port `443` to `extlb_listener_https_port` (default to `30443`) on any server of the cluster\n\nThis is the final result:\n\nClient -\u003e Public L4 LB -\u003e nginx proxy (with proxy protocol enabled) -\u003e nginx ingress (with proxy protocol enabled) -\u003e k3s service -\u003e pod(s)\n\n### Cert-manager\n\n[cert-manager](https://cert-manager.io/docs/) is used to issue certificates from a variety of supported source. To use cert-manager take a look at [nginx-ingress-cert-manager.yml](https://github.com/garutilorenzo/k3s-oci-cluster/blob/master/deployments/nginx/nginx-ingress-cert-manager.yml) and [nginx-configmap-cert-manager.yml](https://github.com/garutilorenzo/k3s-oci-cluster/blob/master/deployments/nginx/nginx-configmap-cert-manager.yml) example. To use cert-manager and get the certificate you **need** set on your DNS configuration the public ip address of the load balancer.\n\n## Deploy\n\nWe are now ready to deploy our infrastructure. First we ask terraform to plan the execution with:\n\n```\nterraform plan\n\n...\n...\n      # module.k8s_cluster.oci_vault_secret.kubeconfig_secret_name will be created\n      + resource \"oci_vault_secret\" \"kubeconfig_secret_name\" {\n      + compartment_id                 = \"ocid1.tenancy.oc1..aaaaaaaacuobj3enmdjf3j7heb3vwr2iqtcb266xlkczo3ifxubiuep6fvpq\"\n      + current_version_number         = (known after apply)\n      + defined_tags                   = (known after apply)\n      + description                    = \"Kubeconfig hash\"\n      + freeform_tags                  = {\n          + \"application\"      = \"k8s\"\n          + \"environment\"      = \"staging\"\n          + \"k3s_cluster_name\" = \"kubernetes\"\n          + \"provisioner\"      = \"terraform\"\n          + \"terraform_module\" = \"https://github.com/garutilorenzo/k8s-oci-cluster\"\n        }\n      + id                             = (known after apply)\n      + key_id                         = \"ocid1.key.oc1.eu-zurich-1.c5r5vcceaaanu.ab5heljr74iutguojeaogcb5or5kqb3q6uxs2njuua5fkr6nlhlh67uukrbq\"\n      + lifecycle_details              = (known after apply)\n      + metadata                       = (known after apply)\n      + secret_name                    = \"k8s-hash-staging\"\n      + state                          = (known after apply)\n      + time_created                   = (known after apply)\n      + time_of_current_version_expiry = (known after apply)\n      + time_of_deletion               = (known after apply)\n      + vault_id                       = \"ocid1.vault.oc1.eu-zurich-1.c5r5vcceaaanu.ab5heljr6pnyytb2bn7fdfacyo2eses7mcgbnv7wqhvwcckjjfvekn2tmefq\"\n\n      + secret_content {\n          + content      = \"ZW1wdHkga3ViZWNvbmZpZyBzZWNyZXQ=\"\n          + content_type = \"BASE64\"\n          + name         = (known after apply)\n          + stage        = (known after apply)\n        }\n\n      + secret_rules {\n          + is_enforced_on_deleted_secret_versions        = (known after apply)\n          + is_secret_content_retrieval_blocked_on_expiry = (known after apply)\n          + rule_type                                     = (known after apply)\n          + secret_version_expiry_interval                = (known after apply)\n          + time_of_absolute_expiry                       = (known after apply)\n        }\n    }\n\n  # module.k8s_cluster.oci_vault_secret.token_secret will be updated in-place\n  ~ resource \"oci_vault_secret\" \"token_secret\" {\n        id                     = \"ocid1.vaultsecret.oc1.eu-zurich-1.amaaaaaa5kjm7pyaob2ryqa5q4awwgkugs2it37pjmnlj7ldd6kr2ssqubpa\"\n        # (11 unchanged attributes hidden)\n\n      + secret_content {\n          + content      = \"ZW1wdHkgdG9rZW4gc2VjcmV0\"\n          + content_type = \"BASE64\"\n        }\n\n        # (1 unchanged block hidden)\n    }\n\nPlan: 41 to add, 3 to change, 0 to destroy.\n\nChanges to Outputs:\n  + k8s_servers_ips = [\n      + (known after apply),\n    ]\n  + k8s_workers_ips = [\n      + (known after apply),\n      + (known after apply),\n    ]\n  + public_lb_ip    = (known after apply)\n\nNote: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run \"terraform apply\" now.\n```\n\nnow we can deploy our resources with:\n\n```\nterraform apply\n\n...\n...\n\nPlan: 41 to add, 3 to change, 0 to destroy.\n\nChanges to Outputs:\n  + k8s_servers_ips = [\n      + (known after apply),\n    ]\n  + k8s_workers_ips = [\n      + (known after apply),\n      + (known after apply),\n    ]\n  + public_lb_ip    = (known after apply)\n\nDo you want to perform these actions?\n  Terraform will perform the actions described above.\n  Only 'yes' will be accepted to approve.\n\n  Enter a value: yes\n\n...\n...\n\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_http_backend_extra_node[0]: Still creating... [1m40s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_https_backend_extra_node[0]: Still creating... [1m40s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_http_backend_extra_node[0]: Still creating... [1m50s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_https_backend_extra_node[0]: Still creating... [1m50s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_https_backend_extra_node[0]: Still creating... [2m0s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_http_backend_extra_node[0]: Still creating... [2m0s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_https_backend_extra_node[0]: Creation complete after 2m8s [id=networkLoadBalancers/ocid1.networkloadbalancer.oc1.eu-zurich-1.xxxxxx/backendSets/k8s_https_backend/backends/ocid1.instance.oc1.eu-zurich-1.xxxxxx:443]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_http_backend_extra_node[0]: Still creating... [2m10s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_http_backend_extra_node[0]: Still creating... [2m20s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_http_backend_extra_node[0]: Still creating... [2m30s elapsed]\nmodule.k8s_cluster.oci_network_load_balancer_backend.k8s_http_backend_extra_node[0]: Creation complete after 2m34s [id=networkLoadBalancers/ocid1.networkloadbalancer.oc1.eu-zurich-1.xxxxxx/backendSets/k8s_http_backend/backends/ocid1.instance.oc1.eu-zurich-1.xxxxxx:80]\n\nApply complete! Resources: 41 added, 3 changed, 0 destroyed.\n\nOutputs:\n\nk8s_servers_ips = [\n  \"140.x.x.x\",\n]\nk8s_workers_ips = [\n  \"140.x.x.x\",\n  \"152.x.x.x\",\n]\npublic_lb_ip = tolist([\n  {\n    \"ip_address\" = \"152.x.x.x\"\n    \"ip_version\" = \"IPV4\"\n    \"is_public\" = true\n    \"reserved_ip\" = tolist([])\n  },\n  {\n    \"ip_address\" = \"10.x.x.x\"\n    \"ip_version\" = \"IPV4\"\n    \"is_public\" = false\n    \"reserved_ip\" = tolist([])\n  },\n])\n```\n\nNow on one master node you can check the status of the cluster with:\n\n```\nssh 152.X.X.X -lubuntu\n\nubuntu@inst-ctrpq-k8s-servers:~$ sudo su -\nroot@inst-ctrpq-k8s-servers:~# kubectl get nodes\nNAME                     STATUS   ROLES                  AGE     VERSION\ninst-4iekh-k8s-workers   Ready    \u003cnone\u003e                 3m37s   v1.23.5\ninst-ctrpq-k8s-servers   Ready    control-plane,master   4m33s   v1.23.5\ninst-gasrn-k8s-workers   Ready    \u003cnone\u003e                 3m40s   v1.23.5\ninst-ned7t-k8s-servers   Ready    control-plane,master   5m45s   v1.23.5\n```\n\n#### Public LB check\n\nWe can now test the public load balancer, nginx ingress controller and the security list ingress rules. On your local PC run:\n\n```\ncurl -v http://PUBLIC_LB_IP\n*   Trying PUBLIC_LB_IP:80...\n* TCP_NODELAY set\n* Connected to PUBLIC_LB_IP (PUBLIC_LB_IP) port 80 (#0)\n\u003e GET / HTTP/1.1\n\u003e Host: PUBLIC_LB_IP\n\u003e User-Agent: curl/7.68.0\n\u003e Accept: */*\n\u003e \n* Mark bundle as not supporting multiuse\n\u003c HTTP/1.1 404 Not Found\n\u003c Date: Sat, 04 Feb 2023 11:06:30 GMT\n\u003c Content-Type: text/html\n\u003c Content-Length: 146\n\u003c Connection: keep-alive\n\u003c \n\u003chtml\u003e\n\u003chead\u003e\u003ctitle\u003e404 Not Found\u003c/title\u003e\u003c/head\u003e\n\u003cbody\u003e\n\u003ccenter\u003e\u003ch1\u003e404 Not Found\u003c/h1\u003e\u003c/center\u003e\n\u003chr\u003e\u003ccenter\u003enginx\u003c/center\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n* Connection #0 to host PUBLIC_LB_IP left intact\n```\n\n*404* is a correct response since the cluster is empty. We can test also the https listener/backends:\n\n```\ncurl -k -v https://PUBLIC_LB_IP\n*   Trying PUBLIC_LB_IP:443...\n* TCP_NODELAY set\n* Connected to PUBLIC_LB_IP (PUBLIC_LB_IP) port 443 (#0)\n* ALPN, offering h2\n* ALPN, offering http/1.1\n* successfully set certificate verify locations:\n*   CAfile: /etc/ssl/certs/ca-certificates.crt\n  CApath: /etc/ssl/certs\n* TLSv1.3 (OUT), TLS handshake, Client hello (1):\n* TLSv1.3 (IN), TLS handshake, Server hello (2):\n* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):\n* TLSv1.3 (IN), TLS handshake, Certificate (11):\n* TLSv1.3 (IN), TLS handshake, CERT verify (15):\n* TLSv1.3 (IN), TLS handshake, Finished (20):\n* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):\n* TLSv1.3 (OUT), TLS handshake, Finished (20):\n* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384\n* ALPN, server accepted to use h2\n* Server certificate:\n*  subject: O=Acme Co; CN=Kubernetes Ingress Controller Fake Certificate\n*  start date: Feb  3 09:26:14 2023 GMT\n*  expire date: Feb  3 09:26:14 2024 GMT\n*  issuer: O=Acme Co; CN=Kubernetes Ingress Controller Fake Certificate\n*  SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.\n* Using HTTP2, server supports multi-use\n* Connection state changed (HTTP/2 confirmed)\n* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0\n* Using Stream ID: 1 (easy handle 0x5566c6bfa2f0)\n\u003e GET / HTTP/2\n\u003e Host: PUBLIC_LB_IP\n\u003e user-agent: curl/7.68.0\n\u003e accept: */*\n\u003e \n* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):\n* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):\n* old SSL session ID is stale, removing\n* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!\n\u003c HTTP/2 404 \n\u003c date: Sat, 04 Feb 2023 11:07:06 GMT\n\u003c content-type: text/html\n\u003c content-length: 146\n\u003c strict-transport-security: max-age=15724800; includeSubDomains\n\u003c \n\u003chtml\u003e\n\u003chead\u003e\u003ctitle\u003e404 Not Found\u003c/title\u003e\u003c/head\u003e\n\u003cbody\u003e\n\u003ccenter\u003e\u003ch1\u003e404 Not Found\u003c/h1\u003e\u003c/center\u003e\n\u003chr\u003e\u003ccenter\u003enginx\u003c/center\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n* Connection #0 to host PUBLIC_LB_IP left intact\n```\n\n#### Longhorn check\n\nTo check if longhorn was successfully installed run on one master nodes:\n\n```\nroot@inst-ctrpq-k8s-servers:~# kubectl get ns\nNAME              STATUS   AGE\ndefault           Active   7m2s\ningress-nginx     Active   3m24s\nkube-node-lease   Active   7m4s\nkube-public       Active   7m4s\nkube-system       Active   7m4s\nlonghorn-system   Active   3m22s \u003c- longhorn namespace \n\nroot@inst-ctrpq-k8s-servers:~# kubectl get pods -n longhorn-system\nNAME                                        READY   STATUS    RESTARTS        AGE\ncsi-attacher-6454556647-2rjmz               1/1     Running   0               2m53s\ncsi-attacher-6454556647-6cmbp               1/1     Running   0               2m53s\ncsi-attacher-6454556647-dpt8x               1/1     Running   0               2m53s\ncsi-provisioner-869bdc4b79-25qgq            1/1     Running   0               2m52s\ncsi-provisioner-869bdc4b79-6cpws            1/1     Running   0               2m52s\ncsi-provisioner-869bdc4b79-8rbd2            1/1     Running   0               2m52s\ncsi-resizer-6d8cf5f99f-g7p89                1/1     Running   0               2m51s\ncsi-resizer-6d8cf5f99f-kjr7l                1/1     Running   0               2m51s\ncsi-resizer-6d8cf5f99f-wffrt                1/1     Running   0               2m51s\ncsi-snapshotter-588457fcdf-lt5g2            1/1     Running   0               2m47s\ncsi-snapshotter-588457fcdf-rwzkr            1/1     Running   0               2m47s\ncsi-snapshotter-588457fcdf-sqpnf            1/1     Running   0               2m49s\nengine-image-ei-fa2dfbf0-4c92z              1/1     Running   0               3m11s\nengine-image-ei-fa2dfbf0-5lg9g              1/1     Running   0               3m11s\nengine-image-ei-fa2dfbf0-85qw7              1/1     Running   0               3m11s\nengine-image-ei-fa2dfbf0-pvdb4              1/1     Running   0               3m11s\ninstance-manager-e-0875a5db                 1/1     Running   0               3m2s\ninstance-manager-e-2de68cb1                 1/1     Running   0               3m8s\ninstance-manager-e-dc60e8b8                 1/1     Running   0               2m53s\ninstance-manager-e-eafd289e                 1/1     Running   0               3m11s\ninstance-manager-r-1b69bd4c                 1/1     Running   0               3m2s\ninstance-manager-r-2b769288                 1/1     Running   0               3m7s\ninstance-manager-r-c540059f                 1/1     Running   0               2m53s\ninstance-manager-r-d17351ca                 1/1     Running   0               3m10s\nlonghorn-csi-plugin-hft24                   2/2     Running   0               2m45s\nlonghorn-csi-plugin-ljcfp                   2/2     Running   0               2m45s\nlonghorn-csi-plugin-s5ww2                   2/2     Running   0               2m45s\nlonghorn-csi-plugin-s77qm                   2/2     Running   0               2m46s\nlonghorn-driver-deployer-7dddcdd5bb-8pjjm   1/1     Running   0               3m41s\nlonghorn-manager-7rfwc                      1/1     Running   0               3m39s\nlonghorn-manager-gmn9n                      1/1     Running   1 (3m13s ago)   3m39s\nlonghorn-manager-k9kms                      1/1     Running   1 (3m13s ago)   3m42s\nlonghorn-manager-zt5dw                      1/1     Running   1 (3m13s ago)   3m42s\nlonghorn-ui-7648d6cd69-2llgx                1/1     Running   0               3m42s\n```\n\n## Deploy a sample stack\n\nFinally to test all the components of the cluster we can deploy a sample stack. The stack is composed by the following components:\n\n* MariaDB\n* Nginx\n* Wordpress\n\nEach component is made by: one deployment and one service.\nWordpress and nginx share the same persistent volume (ReadWriteMany with longhorn storage class). The nginx configuration is stored in four ConfigMaps and  the nginx service is exposed by the nginx ingress controller.\n\nDeploy the resources with:\n\n```\nkubectl apply -f https://raw.githubusercontent.com/garutilorenzo/k8s-oci-cluster/master/deployments/mariadb/all-resources.yml\nkubectl apply -f https://raw.githubusercontent.com/garutilorenzo/k8s-oci-cluster/master/deployments/nginx/all-resources.yml\nkubectl apply -f https://raw.githubusercontent.com/garutilorenzo/k8s-oci-cluster/master/deployments/wordpress/all-resources.yml\n```\n\nand check the status:\n\n```\nkubectl get deployments\nNAME        READY   UP-TO-DATE   AVAILABLE   AGE\nmariadb       1/1     1            1           92m\nnginx         1/1     1            1           79m\nwordpress     1/1     1            1           91m\n\nkubectl get svc\nNAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE\nkubernetes        ClusterIP   10.43.0.1       \u003cnone\u003e        443/TCP    5h8m\nmariadb-svc       ClusterIP   10.43.184.188   \u003cnone\u003e        3306/TCP   92m\nnginx-svc         ClusterIP   10.43.9.202     \u003cnone\u003e        80/TCP     80m\nwordpress-svc     ClusterIP   10.43.242.26    \u003cnone\u003e        9000/TCP   91m\n```\n\nNow you are ready to setup WP, open the LB public ip and follow the wizard. **NOTE** nginx and the Kubernetes Ingress rule are configured without virthual host/server name.\n\nTo clean the deployed resources:\n\n```\nkubectl delete -f https://raw.githubusercontent.com/garutilorenzo/k3s-oci-cluster/master/deployments/mariadb/all-resources.yml\nkubectl delete -f https://raw.githubusercontent.com/garutilorenzo/k3s-oci-cluster/master/deployments/nginx/all-resources.yml\nkubectl delete -f https://raw.githubusercontent.com/garutilorenzo/k3s-oci-cluster/master/deployments/wordpress/all-resources.yml\n```\n\n## Clean up\n\n```\nterraform destroy\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgarutilorenzo%2Fk8s-oci-cluster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgarutilorenzo%2Fk8s-oci-cluster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgarutilorenzo%2Fk8s-oci-cluster/lists"}