Follow the steps in this page to create your first simple Horizon edge service.
- If you have not already done so, complete the steps in these sections:
- Preconditions for Using the Hello World Example Edge Service
- Using the Hello World Example Edge Service with Deployment Pattern
- If you are using macOS as your development host, configure Docker to store credentials in
~/.docker
:
- Open the Docker Preferences dialog
- Uncheck Securely store Docker logins in macOS keychain
- If you do not already have a docker ID, obtain one at https://hub.docker.com/ . Log in to Docker Hub using your Docker Hub ID:
export DOCKER_HUB_ID="<dockerhubid>"
echo "<dockerhubpassword>" | docker login -u $DOCKER_HUB_ID --password-stdin
Output example:
WARNING! Your password will be stored unencrypted in /home/pi/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
- Create a cryptographic signing key pair. This enables you to sign services when publishing them to the exchange. This step only needs to be done once.
hzn key create "<x509-org>" "<x509-cn>"
where <x509-org>
is your company name, and <x509-cn>
is typically set to your email address.
- Install
git
andjq
:
On Linux:
sudo apt install -y git jq
On macOS:
brew install git jq
cd
to the directory in which you want to create your new service and then run this command to create the files for a simple edge service and associated Horizon metadata files:
hzn dev service new -s myhelloworld -i "$DOCKER_HUB_ID/myhelloworld"
Notice that some project variables are defined in horizon/hzn.json
and referenced in other files, for example horizon/service.definition.json
.
- Edit
service.sh
and change something simple, for example change "Hello" to "Hey there"
Note: This service is a shell script for brevity, but you can write your service in any language.
- Set the required environment variables:
eval $(hzn util configconv -f horizon/hzn.json)
export ARCH=$(hzn architecture)
- Build the service docker image:
make
- Test the service by running it the simulated agent environment:
hzn dev service start -S
- Check that the container is running:
sudo docker ps
- Display the environment variables Horizon passes into your service container:
sudo docker inspect $(sudo docker ps -q --filter name=myhelloworld) | jq '.[0].Config.Env'
- See your helloworld service output:
hzn dev service log -f $SERVICE_NAME
- Stop the service:
hzn dev service stop
- Instruct Horizon to push your docker image to your registry and publish your service in the Horizon Exchange:
hzn exchange service publish -f horizon/service.definition.json
hzn exchange service list
- Get the required deployment, service, and node policy files to to deploy your new
helloworld
service to you edge node:
wget https://raw.githubusercontent.com/open-horizon/examples/master/edge/services/helloworld/policy/node.policy.json
wget https://raw.githubusercontent.com/open-horizon/examples/master/edge/services/helloworld/policy/service.policy.json
wget https://raw.githubusercontent.com/open-horizon/examples/master/edge/services/helloworld/policy/deployment.policy.json
- Publish and view your service policy in the Horizon Exchange:
hzn exchange service addpolicy -f service.policy.json ${HZN_ORG_ID}/${SERVICE_NAME}_${SERVICE_VERSION}_${ARCH}
hzn exchange service listpolicy ${HZN_ORG_ID}/${SERVICE_NAME}_${SERVICE_VERSION}_${ARCH}
- Publish and view your deployment policy in the Horizon Exchange:
hzn exchange deployment addpolicy -f deployment.policy.json ${HZN_ORG_ID}/policy-${SERVICE_NAME}_${SERVICE_VERSION}
hzn exchange deployment listpolicy ${HZN_ORG_ID}/policy-${SERVICE_NAME}_${SERVICE_VERSION}
- Register your edge device with the node policy:
hzn register --policy node.policy.json
- The edge device will make an agreement with one of the Horizon agreement bots (this typically takes about 15 seconds). Repeatedly query the agreements of this device until the
agreement_finalized_time
andagreement_execution_start_time
fields are filled in:
hzn agreement list
- After the agreement is made, list the docker container edge service that has been started as a result:
sudo docker ps
- See the myhelloworld service output:
hzn service log -f $SERVICE_NAME
- Unregister your edge device (which will also stop the myhelloworld service):
hzn unregister -f
- Publish and view your edge node deployment pattern in the Horizon Exchange:
hzn exchange pattern publish -f horizon/pattern.json
hzn exchange pattern list
- Register your edge node with Horizon to use your deployment pattern:
hzn register -p pattern-${SERVICE_NAME}-$(hzn architecture)
- The edge device will make an agreement with one of the Horizon agreement bots (this typically takes about 15 seconds). Repeatedly query the agreements of this device until the
agreement_finalized_time
andagreement_execution_start_time
fields are filled in:
hzn agreement list
- After the agreement is made, list the docker container edge service that has been started as a result:
sudo docker ps
- See the myhelloworld service output:
hzn service log -f $SERVICE_NAME
- Unregister your edge node (which will also stop the myhelloworld service):
hzn unregister -f