Manage Oracle Container Engine for Kubernetes with Ansible Modules

Rohit Chaware
Oracle Developers
Published in
4 min readApr 4, 2019

--

This post was jointly authored by Rohit Chaware and Sivakumar Thyagarajan.

Last year, we announced the availability of Ansible modules for Oracle Cloud Infrastructure. These Ansible modules enable you to automate the provisioning and configuration of Oracle Cloud Infrastructure resources.

Many developers and deployers are adopting Kubernetes to reliably deploy, orchestrate, scale, and manage distributed applications in the cloud. However, setting up and maintaining a Kubernetes cluster on the infrastructure (compute, network, and storage) of a cloud provider is complex and time consuming, and requires deep knowledge of Kubernetes cluster operations and the infrastructure layer of the cloud provider.

Oracle Cloud Infrastructure Container Engine for Kubernetes is a fully managed, scalable, and highly available service that you can use to deploy your containerized applications to the cloud. This service enables you to quickly create, manage, and consume Kubernetes clusters that leverage underlying Oracle Cloud Infrastructures compute, network, and storage services without the need to install and maintain the complex supporting Kubernetes infrastructure on your own. Developers get to focus on building container-native microservices and serverless applications, and don’t need to worry about deploying and managing the Kubernetes cluster.

The Oracle Cloud Infrastructure Console, Terraform provider, and various SDKs provide support for Container Engine for Kubernetes (sometimes abbreviated OKE). But if you use Ansible for infrastructure automation and configuration management, our Ansible modules include modules specific to Container Engine for Kubernetes to help you automate the creation of Kubernetes clusters, node pools, and other related artifacts. This post describes how to create and manage a Kubernetes cluster by using Container Engine for Kubernetes and Ansible.

The steps described here show you how to perform the following tasks:

  1. Create all the necessary prerequisite resources for a Container Engine for Kubernetes cluster (VCN, subnets, and related resources).
  2. Create a Kubernetes cluster by using Container Engine for Kubernetes.
  3. Create a node pool.
  4. Download the kubeconfig file for the cluster.
  5. Deploy a sample application on the cluster.

Note: This post shows only relevant snippets from a bigger playbook. For a complete playbook that you can use to create and manage a Container Engine for Kubernetes cluster, see the deploy_app_on_k8s_cluster sample. The sample’s README file provides instructions on how to reproduce the steps in this playbook against your Oracle Cloud Infrastructure tenancy.

Step 1: Create Prerequisite Resources

To create a Kubernetes cluster with Container Engine for Kubernetes, you need a virtual cloud network (VCN) along with five subnets, an internet gateway, a route table, and two security lists as detailed in the documentation. You can refer to this playbook to set up the prerequisites for Container Engine for Kubernetes. This playbook employs Ansible modules for the Oracle Cloud Infrastructure Networking and Identity and Access Management (IAM) services to create the various artifacts.

Step 2: Create a Kubernetes Cluster

Using the VCN and subnets that you created in the previous step, you can create a new Kubernetes cluster by using the oci_cluster module. You can dynamically retrieve the Kubernetes version to use for the master and worker nodes in the cluster by using the oci_cluster_options_facts module (refer to the sample for an example).

- name: Create a Kubernetes cluster with OKE
oci_cluster:
compartment_id: "{{ cluster_compartment }}"
name: "{{ cluster_name }}"
vcn_id: "{{ vcn_id }}"
kubernetes_version: "{{ k8s_version }}"
options:
service_lb_subnet_ids:
- "{{ lb_subnet1_id }}"
- "{{ lb_subnet2_id }}"
register: result
- set_fact:
cluster_id: "{{ result.cluster.id }}"

Step 3: Create a Node Pool

Now, you can use the oci_node_pool module to create a node pool with one node in each of the three worker node subnets (each subnet is in a different availability domain to ensure high availability). You can also use the oci_node_pool_options_facts module to choose an image and a shape for the nodes (refer to the sample for an example).

- name: Create a node pool
oci_node_pool:
cluster_id: "{{ cluster_id }}"
compartment_id: "{{ cluster_compartment }}"
name: "{{ node_pool_name }}"
kubernetes_version: "{{ k8s_version }}"
node_image_name: "{{ node_image_name }}"
node_shape: "{{ node_shape }}"
quantity_per_subnet: 1
subnet_ids:
- "{{ ad1_subnet_id }}"
- "{{ ad2_subnet_id }}"
- "{{ ad3_subnet_id }}"

Step 4: Download the kubeconfig File for the Cluster

You can download the kubeconfig file for the cluster by using the oci_kubeconfig module. Specify the OCID of the cluster, and specify a file path as the value for dest to indicate where to write the kubeconfig file to.

- name: Download kubeconfig
oci_kubeconfig:
cluster_id: "{{ cluster_id }}"
dest: "{{ kubeconfig_path }}"
force: true

Step 5: Deploy a Sample Application on the Cluster

You can now deploy a sample application by using the k8s_raw module. To assert successful deployment, retrieve the deployment details by using the same module.

- name: Create a deployment and a service on the created OKE cluster
k8s_raw:
kubeconfig: "{{ kubeconfig_path }}"
state: present
src: "{{ deployment_yaml_path }}"
register: result
- name: Get the deployment to assert successful deployment
k8s_raw:
kubeconfig: "{{ kubeconfig_path }}"
namespace: default
kind: Deployment
name: "{{ deployment_name }}"
register: deployment

Conclusion

In this blog post, we demonstrated how to use Oracle Cloud Infrastructure Ansible modules to provision a Kubernetes cluster along with a node pool in Container Engine for Kubernetes, and deploy a sample application to the new Kubernetes cluster. For more information about the modules, watch this video:

To try more samples and solutions with our Ansible modules, explore the samples directory in the oci-ansible-modules project on GitHub. For documentation of these Ansible modules and details about the Ansible dynamic inventory script, see the getting started and Ansible modules documentation.

If you need help, please use the following channels:

Happy automating using Ansible!

--

--

Rohit Chaware
Oracle Developers

Rohit is a Senior Member of Technical Staff at Oracle, India.