Autonomous and Flexible Management of Edge Services
local graph
Chapter 9 — Autonomous and Flexible Management of Edge Services
Section titled “Chapter 9 — Autonomous and Flexible Management of Edge Services”Now that we have a solid foundation regarding Edge computing concepts and the architectural features required to build an Edge-deployable containerized workload, we are ready to build the components of our application using Open Horizon. The previous chapter on the hzn CLI provided a comprehensive overview of how to use various commands and provided you with the mechanics for interacting with the Edge Agent and Management Hub. Armed with conceptual and practical hands-on experience, in this chapter, we will define, develop, publish, and deploy the actual workload on the Edge device node.
In this chapter, we will cover the following main topics:
- Developing an Open Horizon Service
- Developing an Open Horizon Pattern to deploy services
- Publishing a Service and Pattern to the Management Hub
- Deploying a workload on an Edge device node using the Pattern
- Combining multiple Services and deployments
Technical requirements
Section titled “Technical requirements”To complete this chapter, you will need to do the following:
- Verify the current setup of the All-in-One (AIO) Management Hub and the Edge Agent on your Edge device node, as per the instructions in Chapter 7, in the Technical requirements section.
- Create an account in Docker Hub or any other container image registry where you can push a Docker image and pull from.
- You have observed that the JSON file format is used extensively while providing input for a particular operation or when the system generates output that a user can view or work with.
- We will be using several commonly available tools, such as
makeandjq, while doing development work.
Developing an Open Horizon Service
Section titled “Developing an Open Horizon Service”As noted earlier, a Service is the most atomic artifact of the Open Horizon managed workload deployment construct. An Open Horizon Service wraps all the pieces that are required to run a containerized microservice on an Edge node.
To define what makes an Open Horizon Service, let’s look at the sample service-definition.json file you created when you used the hzn dev service new command in the previous chapter to create various template files.
Tip
Although it’s not required to utilize the files generated by the preceding command, you have the option to create your own. However, it’s a good idea to adhere to the directory structure and keep all the files under a horizon sub-directory as some of the commands that help check the deployment expect certain files to be in certain directories.
Let’s examine the sample service-definition.json file to understand various fields and their applicability:
{ "org": "$HZN_ORG_ID", "label": "$SERVICE_NAME for $ARCH", "description": " ", "public": false, "documentation": " ", "url": "$SERVICE_NAME", "version": "$SERVICE_VERSION", "arch": "$ARCH", "sharable": "multiple", "requiredServices": [], "userInput": [ { "name": "", "label": "", "type": "", "defaultValue": "" } ], "deployment": { "services": { "$SERVICE_NAME": { "image": "${DOCKER_IMAGE_BASE}_$ARCH:$SERVICE_ VERSION", "privileged": false, "network": "", "secrets": {} } } }}While many of the fields may be self-explanatory, we will describe them and provide additional context. As we describe each field, we will identify whether a field requires a value or can be left empty ("" or [``]):
org(required): Open Horizon supports multi-tenancy, implying that multiple organizations can be hosted on one Management Hub to keep the overall cost low. This field identifies the organization that this service belongs to.label(optional): A short text label for the service.description(optional): A detailed description of the service.public(required): Usually this isfalse, though it can also betrue. Here,trueimplies that the service can be used by organizations other than the organization that originally published It. This requires that the underlying container image is publicly available. Open Horizon examples published in the IBM organization have this field set totrue.documentation(optional): Link to the documentation URL, if any.url(required): Though it’s namedurl, it’s not a URL but a unique name for the service within the Docker network that this service will belong in. We usually follow a dot naming convention;, and, in this book, we will follow a name likerwec.edge.example.network.service1for example. You may also organize the service name as per your own guidelines.version(required): The service version is specified with thex.y.zconvention, a subset of the semantic version scheme. The service version is used by the system to deploy or to locate the version of the service when performing a rollback in case of any issues. By convention,1.0.1is newer than1.0.0.arch(required): The CPU architecture that the container image is compiled for. You can have support for multiple architectures, such asamd64,arm,arm64, and others, and have compiled service code for each separately.sharable(required): Usually, this is set tomultiple, though its value can also besingleton. Here,multipleimplies that multiple instances of the same service can be used by the application, whereassingletonmeans that there is only one instance of the service that can be used by the application. This will depend on the needs of your application design.requiredServices(optional): This field enables one of the core features of the Open Horizon service management scheme by managing the dependencies of the services. If a top-level primary service requires a secondary service below to function properly, the Open Horizon Edge Agent will run the secondary level service before rendering the primary service running. A secondary service may have its own secondary service or services. A lower-level service may refer to yet another service aspeer-service, resulting in a complex graph of services. UsingrequiredServices, you can build a complex graph of services, not just hierarchical services. Open Horizon Edge Agent manages the asynchronous life cycle of all the dependent services in the service graph, avoiding the need to manage them in your application code. Though the Edge Agent will manage the life cycle of the service by restarting if a service fails, your application code still needs to tolerate the failed service.userInput(optional): A service may require some inputs. The supported input types arestring,boolean,int, andfloat. Services may have a default value and can be also overridden for a node to provide flexibility.
This set of fields manages the metadata that is used to manage the service.
The following fields, which can be found in the deployment section, relate to the containerized image of the service:
deployment(required): This section describes how to deploy the containerized service image itself.services(required): A list of services that are defined and managed by thisservice-definitionfile. At least one Service needs to be defined.$SERVICE_NAME(required): A valid and unique Service name that the Docker network will create and which other services will use to identify this service. If there is only one service, then$SERVICE_NAMEcould be the same as theurlservice name defined previously. In the case of multiple services, each Service name needs to be unique and theurlservice name identifies a collection of these services together.image: A fully qualified path to access the Docker image in a container image repository, including the version number.privileged: Specifytrueorfalse, depending on the need of your application to run the image inprivilegedmode. This is typically required when the container image needs access to host services such as the filesystem, network, and others.network: Uses the network services of the host machine.
We encourage you to review the content of the two following references. As Open Horizon capabilities are constantly being enhanced, the deployment section specifies many more optional fields that an application may want to specify:
- https://open-horizon.github.io/docs/anax/docs/deployment_string/
- https://open-horizon.github.io/docs/anax/docs/service_def/
Developing an Open Horizon Pattern
Section titled “Developing an Open Horizon Pattern”Once your service has been defined and published (you’ll learn how to publish your service later in the Publishing an Open Horizon service and pattern section), for the services to be deployed and running in the Edge nodes, they need to be managed as a collection using a Pattern-based scheme or a Policy-based scheme. In this chapter, we will consider the Pattern-based scheme. A Pattern is a construct that groups a set of Service(s) into a single deployable unit on a particular node.
Note
One of the restrictions of using an Open Horizon Pattern is that an Edge node can only be registered with one Pattern at a time. You cannot deploy additional services by registering the node with another Pattern that has other Services. If you are using Pattern-based Service deployment, then you must create another Pattern with such desired combinations. Policy-based deployment alleviates this restriction. We will discuss this in the next chapter.
To define what makes an Open Horizon pattern, let’s look at the sample pattern.json file that you created when you used the hzn dev service new command in the previous chapter while creating various template files:
{ "name": "pattern-${SERVICE_NAME}-$ARCH", "label": "Edge $SERVICE_NAME Service Pattern for $ARCH", "description": "Pattern for $SERVICE_NAME for $ARCH", "public": false, "clusterNamespace": "", "services": [ { "serviceUrl": "$SERVICE_NAME", "serviceOrgid": "$HZN_ORG_ID", "serviceArch": "$ARCH", "serviceVersions": [ { "version": "$SERVICE_VERSION" } ] } ]}Relatively, the Pattern definition file has fewer options, and its simplicity makes it attractive for simpler projects. As you can see, many of the fields are self-explanatory, but we will describe them here and provide additional context:
name(required): The Pattern name that will be used while registering the Edge node.label(optional): A short text label for the Pattern.description(optional): A detailed description of the Pattern.public(required): Usually, this isfalse, though it can also betrue. Here,trueimplies that the Pattern can be used by organizations other than the organization that originally published it.clusterNamespace(optional): This is used when deploying on an Edge cluster node.- The fields of the
servicessection are important as they list the Services(s) that make up the Pattern. services(required): A list of services described by their own set of fields. There can be multiple of these blocks corresponding to different Services or architectures.serviceUrl(required): The Service name’surlfield defined inservice-definition.json.serviceOrgid: The organization that published this Service.serviceArch: The CPU architecture.serviceVersions: This is a list of Service versions that make up this Pattern. This field also optionally includes versioning information about upgrades and rollbacks of Services.version: The published Service version.
The Open Horizon documentation describes many more options that may be applicable based on your project: https://open-horizon.github.io/docs/using_edge_services/using_patterns/#using_patterns.
Publishing an Open Horizon Service and Pattern
Section titled “Publishing an Open Horizon Service and Pattern”Having understood what it would take to prepare the service definition, let’s create a simple Service and publish it in the Management Hub. In the previous chapter, we assumed that the container image of the Service exists, which usually may be the case. However, in this book, we will start from scratch.
A typical application development cycle may involve the following steps:
- Develop the microservice code.
- Build a container image for the microservice image.
- Publish the microservice in a container repository.
- Create
service-definition JSONfor the service. - Publish the service to the Management Hub.
- Create an Open Horizon Pattern or Deployment Policy for the service.
- Register the node with a Pattern.
- Verify that the Service and the application are running.
We will go through these steps by following the code provided in the example-services repository.
Tip
In the book, we’ll adhere to a directory structure similar to the repository, organizing content into two main directories: publish and register. Additionally, we’ll utilize the readily available make utility found on Linux systems to automate tasks such as building and publishing. It’s important to note that this isn’t mandatory for Open Horizon, and you’re welcome to structure your code according to your preferences and use any IDE you prefer. We’ll also leverage the concept of Linux environment variables to ensure flexibility and portability in building and publishing code.
Let’s take a closer look at publish and register:
1. publish: The content in this directory covers the source code of the application, and all the other artifacts required to publish a Service and Pattern to the Management Hub
2. register: The content in this directory either documents or has the code to register an Edge device node using a Pattern or Policy
Typical content of the publish directory
Section titled “Typical content of the publish directory”The publish directory typically contains a makefile file that serves as the top-level file. It contains various targets to automate tasks such as building and publishing application artifacts.
To illustrate the functionality of the interacting services as an example application in this chapter, we will make use of two services: service1 and service2. Both are identical services with a simple variation in messages to help identify the content provided by either of the Services as it’s displayed in the output.
Let’s look inside src/service1. It contains several directories, a Dockerfile, and a Makefile:
horizon: Contains content related to Open Horizonservice: This contains application code for a very simple service that outputs text.Dockerfile.amd64: A very simple Dockerfle for building a container image for the applicationMakefile: This contains various targets, as outlined here:build: Builds the Docker image using the specified architecture-specific Dockerfile and tags the image as per the specified image name. This is composed of severalENVIRONMENTvariables to help automate the setup process:push: Pushes the image to Docker Hub using your credentials. You should have your own Docker container registry credentials.publish-service: Publishes the service as per the specifiedservice-definition.jsonfile. Note that the image that’s published to Docker Hub is publicly available, which is why we didn’t specify any credentials while publishing. Otherwise, we would have used the-roption to specify the registry token. If the registry token was provided, it would be stored in the Management Hub securely and provided to the Edge Agent when it later registers with a Pattern or Policy to pull the image from the authenticated container registry.publish-pattern: Once a Service has been published, an Open Horizon Pattern can be published to the Management Hub. This can be later used by any registering Edge Agent to deploy the Service on the Edge device node.
Now that we have established the general structure of the various files and techniques that we will use to build, publish, and deploy the services and patterns, let’s begin the actual tasks.
Deploying a workload on an Edge device node using a pattern
Section titled “Deploying a workload on an Edge device node using a pattern”In this hands-on exercise, we will build and push container images, publish a Service and Pattern, register the Edge device node with a pattern, and verify that the deployed services are running.
To complete this exercise, take a look at the accompanying example-services repository for any additional instructions. You should look at the content of the Makefile and corresponding targets to understand the underlying commands being used, especially how the Service and Pattern are published to the Management Hub. Also, take a look at service-definition.json and pattern-service.json to develop a good understanding based on real examples and the concepts described so far:
- Set up the
ENVIRONMENTsource ~/project/rwec/ENV_RWEC - Build the service1 Docker image:
make build
- Verify the built
docker images | grep service1
rwec.edge.example.service.service1_amd64 - Verify the built
- Push the
service1container image into themake push - Publish
service1to the Management Hub:make publish-service- Verify the
hzn exchange service list
["myorg/rwec.edge.example.service.service1_1.0.0_amd64"] - Verify the
- Publish the pattern to the
make publish-pattern
- Verify the
hzn exchange pattern list
["myorg/pattern-rwec.edge.example.service.service1"] - Verify the
- Register an Edge device node with
make register
- Verify the registration process by looking at the config state. Its state should be configured when you run the
hzn node list
- As
service1starts to be deployed, an Open Horizon agreement will start to form. You can view this by running the following command. When running this command multiple times, you will see different timestamp values start to
hzn agreement list- Once
agreement_execution_start_timehas been filled in, you can view the deployed container by running thedocker pscommand and verify that the container is running.
- As
- Verify the registration process by looking at the config state. Its state should be configured when you run the
- Test and verify the deployed
The precedingmake test-app-service
maketarget does a simplecurlcall togetthe content returned byservice1. Your output should be similar to this:{"hostname": "0e078496c3f9","service": "Service One"} - Unregister the Edge
The preceding command will cancel the agreement, stop the service, and unregister the Edge device node.make unregister
Here, you practiced building a container image, pushing it to a container registry, and publishing an Open Horizon Service and Pattern. Additionally, you registered an Edge device node with a Pattern and observed the deployment of a containerized microservice on the Edge device node. In the next section, we’ll publish an additional service and combine the services in different combinations of Patterns to illustrate their use.
Combining multiple services and deployments
Section titled “Combining multiple services and deployments”Now that you’ve learned how to deploy a simple service, let’s explore how multiple services can be deployed using an Open Horizon Pattern.
As an exercise, deploy service2 while following the instructions we used for service1. The code for this can be found in the example-services service2 directory of this book’s GitHub repository.
Simply running make in the service2 directory will build and push the container image, and then publish service2 and a corresponding pattern.
Run make register to register the Edge device node with the pattern we created previously. Once the Edge device node has been fully registered and the service workload has been deployed and is running, you can test the service’s output with make test-app-service.
You should see an output similar to this:
{ "hostname": "08a1cd0fbf31", "service": "Service Two"}At this point, you should review the horizon/service.definition.json and horizon/pattern-service.json files for both services – especially the requiredServices and deployment sections in the service definition file.
So far, you have published two services and two patterns to the Management Hub. They should look similar to the following:
hzn exchange service list[ "myorg/rwec.edge.example.service.service2_1.0.0_amd64", "myorg/rwec.edge.example.service.service1_1.0.0_amd64"]hzn exchange pattern list[ "myorg/pattern-rwec.edge.example.service.service2", "myorg/pattern-rwec.edge.example.service.service1"]Patterns with complex service combinations
Section titled “Patterns with complex service combinations”As mentioned earlier, an Edge device node can only be registered with one pattern at a time. You cannot deploy additional services by registering the node with an additional pattern that has other services. If you’re using pattern-based service deployment, then you must create another pattern with such desired combinations.
Let’s use the two services mentioned previously to create higher-level service patterns by using them in different combinations.
Peer services
Section titled “Peer services”In this combination, we will combine two services into one service definition file and create a higher-level service and a corresponding pattern that an Edge device node can register with.
Let’s look inside example-services/publish/src/service-peer/horizon and examine the service-definition.json file. You will find that the deployment section has two different Services and that the Service name for this combination is aptly called service-peer.
Note that we are not going to rebuild the container image for the participating Services but rather combine the already built Docker images using the Open Horizon service definition construct.
Let’s go ahead and publish service-peer by running the following command:
make publish-serviceWe can also view the list of services:
hzn exchange service listAt this point, three services have been published in the Management Hub. We can see this by running the previous command:
[ "myorg/rwec.edge.example.service.service1_1.0.0_amd64", "myorg/rwec.edge.example.service.service2_1.0.0_amd64", "myorg/rwec.edge.example.service.service-peer_1.0.0_amd64"]Let’s create another pattern:
make publish-patternWe can view the list of patterns:
hzn exchange pattern listAt this point, we have an additional pattern in the Management Hub called service-peer:
[ "myorg/pattern-rwec.edge.example.service.service2", "myorg/pattern-rwec.edge.example.service.service-peer", "myorg/pattern-rwec.edge.example.service.service1"]Let’s register the node with the service-peer pattern. Make sure that the Edge device node is unregistered before you register again with a new pattern:
make unregistermake registerhzn agreement listAfter registration and agreement formation, you should see two Docker containers running that correspond to two different Services using docker ps.
When you run make test-app-servicz, which uses curl, you should see output from both services on two different ports:
curl http://localhost:8881 | jq{ "hostname": "a768310f25c7", "service": "Service One"}curl http://localhost:8882 | jq{ "hostname": "a822ec1f69f1", "service": "Service Two"}This approach provides a simple technique you can use to wire multiple services as peers to be deployed and managed by the Edge Agent running on the node. Note that these peer services do not have dependencies on each other and will be started in a random order, as determined by the Docker runtime.
Required services
Section titled “Required services”It is possible to create dependencies among services to ensure they run in an orderly fashion by using the requiredServices construct, where the top-level service has another service as a required service. A pattern can be created with this combination of services.
Let’s look inside example-services/publish/src/service-required/horizon and examine the service-definition.json file. You will find that this top-level service1 has service2 as a requiredServices construct. The deployment section has one service, service1, and the service name for this composition is service-required.
Once again, we aren’t going to rebuild the container image for the participating Services but rather combine the already built Docker images using the Open Horizon service definition construct.
Let’s go ahead and publish service-required by running the following command:
make publish-serviceWe can view the list of services:
hzn exchange service listWith that, four services have been published in the Management Hub. We can see this by running the following command:
[ "myorg/rwec.edge.example.service.service1_1.0.0_amd64", "myorg/rwec.edge.example.service.service2_1.0.0_amd64", "myorg/rwec.edge.example.service.service-peer_1.0.0_amd64", "myorg/rwec.edge.example.service.service-required_1.0.0_amd64"]Let’s create another Pattern:
make publish-patternWe can view the list of Patterns:
hzn exchange pattern listNow, we have a fourth pattern in the Management Hub service-required:
[ "myorg/pattern-rwec.edge.example.service.service2", "myorg/pattern-rwec.edge.example.service.service-peer", "myorg/pattern-rwec.edge.example.service.service1", "myorg/pattern-rwec.edge.example.service.service-required"]Let’s register the node with the service-required pattern. Ensure that the Edge device node is unregistered before you register again with a new pattern:
make unregistermake registerhzn agreement listAfter registration and agreement formation, you should see two Docker containers running corresponding to two different services using docker ps.
When you run make test-app-service, which uses curl, you should see output from both services on two different ports:
curl http://localhost:8881 | jq{ "hostname": "6c6c2edccd44", "service": "Service One"}curl http://localhost:8882 | jq{ "hostname": "8aa9be804509", "service": "Service Two"}The Open Horizon pattern construct provides a mechanism to deploy a single service and multiple services on an Edge node in a tightly defined manner.
Summary
Section titled “Summary”In this chapter, you gained a detailed understanding of how services are individually developed, published, and deployed on registered Edge device nodes. You also learned how a pattern-based deployment can be used to deploy a set of services individually, as peer services, and as dependent services.
In the next chapter, we will explore the Policy-based deployment process so that we can deploy Services on the Edge device node.