Note:
- This tutorial requires access to Oracle Cloud. To sign up for a free account, see Get started with Oracle Cloud Infrastructure Free Tier.
- It uses example values for Oracle Cloud Infrastructure credentials, tenancy, and compartments. When completing your lab, substitute these values with ones specific to your cloud environment.
Use OCI Container Instances to schedule start and stop for OCI Compute Instances
Introduction
Oracle Cloud Infrastructure (OCI) Container Instances is a serverless compute service that enables you to quickly and easily run containers without managing any servers.
You can use the OCI Compute service to create bare metal compute or virtual machine (VM) instances to deploy and run your applications. OCI Compute lets you provision and manage compute hosts, known as instances.
This tutorial describes an example about how to use Container Instances to manage the lifecycle state of Compute Instances in a compartment (it can be extended to tenancy as well). There are cases when you need an automated way to schedule stop/start of Compute Instances – like a crontab job. For example, you need some of your compute instances to run during the day and to stop them during the night to save on cost. This sort of “crontab” job can be created using the OCI Container Instance service.
Basically, the container instance runs Python code looking for a specific freeform tag in each compute instance in a compartment, in a loop. If it finds the specific tag, it will compare the tag value with the current time (when the script runs) to establish if the compute instance should be in running or stopped state at that time and decides to stop or start the compute instance.
Objectives
- Build a Docker image
- Build a container instance from your own image
- Work with Oracle Cloud Infrastructure Registry
- Pass an argument to container ENTRYPOINT
Prerequisites
- Knowledge of Docker, images, registry.
- User privileges to be able to create dynamic group, policies (these will allow the container instance to manage Compute Instances). If the user does not have these privileges, then a tenancy admin can create the dynamic group and policy.
- Docker running on the machine where you run this tutorial.
- User privileges to work with images (push, pull, tag) in Oracle Cloud Infrastructure Registry and a token created for login into Oracle Cloud Infrastructure Registry.
- Familiar with pushing images to Oracle Cloud Infrastructure Registry.
Task 1: Create a dynamic group to allow Container Instance to manage Compute Instances
-
This is required to allow (using a policy) your running container instance to manage compute instances in your compartment.
-
Create a matching rule for your dynamic group as shown in the following command.
ALL {resource.type = 'computecontainerinstance', resource.compartment.id = 'ocid1.compartment.oc1…… '}
Task 2: Create a policy to allow resources in dynamic group to manage compute instances
-
Create the following policy.
allow dynamic-group <dynamic group name> to manage instances in compartment <compartment name>
Task 3: Build the Docker image
-
Create a local folder.
-
Extract the contents of files.zip and copy all files to the local folder.
-
The
requirements.txt
file contains Python packages that will be installed. For now we only need the oci package. -
Build the image using Docker.
docker build -t start_stop_compute:latest .
Task 4: Upload the image to Oracle Cloud Infrastructure Registry
-
Log in before you push the image into Oracle Cloud Infrastructure Registry. For more information, see Logging in to Oracle Cloud Infrastructure Registry.
-
Tag the image you just created as shown in the following example. For more information about how to set the image identifier and target tag for your tenancy/Oracle Cloud Infrastructure Registry repo, see Pushing Images Using the Docker CLI.
docker tag start_stop_compute:latest fra.ocir.io/orasenatdpltintegration03/fvassrepo1/start_stop_compute:latest
-
Push the image to Oracle Cloud Infrastructure Registry.
docker push fra.ocir.io/orasenatdpltintegration03/fvassrepo1/start_stop_compute:latest
Task 5: Define freeform tag on compute instance
-
If you are using the code provided in this tutorial, the freeform tag must be named
myschedule
. This is because the Python code running inside the container is searching for a freeform tag (in compute metadata) namedmyschedule
. -
If you want your compute instances to be managed by provided in this tutorial, you must add the freeform tag named
myschedule
to each compute instance. -
The value of the freeform tag must be as shown in the following section, where “s” means STOPPED and “r” means RUNNING.
s,s,s,s,s,s,s,s,r,r,r,r,r,r,r,r,r,s,s,s,s,s,s,s
-
Each element of the list corresponds to an hour of the day starting with midnight (from 00 to 01 AM). So the element 0 corresponds to the interval between 00 and 01 AM of the day and “s” means the instance should be stopped.
myschedule[0] from 00 to 01 AM => s means instance should be stopped myschedule[8] from 08 to 09 AM => r means instance should be running
-
Based on the above details, the instance should execute as shown in the following section.
00-08 stopped 08-17 running 17-00 stopped
Task 6: Create the container instance
-
Follow the workflow from OCI Web console and create a new container instance.
-
Choose the image you have pushed to the Oracle Cloud Infrastructure Registry in Task 4.
-
Pass as an entrypoint argument the OCID of the compartment in which you will manage the compute instances with the code as shown in the following image.
Related Links
Acknowledgments
- Francisc Vass (Principal Cloud Architect)
More Learning Resources
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Use OCI Container Instances to schedule start and stop for OCI Compute Instances
F79241-01
March 2023
Copyright © 2023, Oracle and/or its affiliates.