The author | sessions (Jiang Yu)
OpenWhisk is an open source, serverless cloud platform that can respond to various events by executing extended code in a runtime container without requiring the user to care about the associated infrastructure architecture.
OpenWhisk profile
OpenWhisk is a cloud-based distributed event-driven programming service. OpenWhisk provides a programming model to register event handlers with cloud services to handle a variety of different services. It can support thousands of triggers and calls and can respond to events of varying sizes.
OpenWhisk is built from a number of components that make OpenWhisk an excellent open source FaaS platform.
Apache OpenWhisk component architecture
OpenWhisk deployment
The operating system of the experimental machine was Ubuntu 18.04 Desktop. Use incubator-OpenWhisk provided on GitHub to install Git. If you don’t have Git installed on your machine, install Git first:
apt install git
Copy the code
Next, clone the repo to the local directory:
git clone https://github.com/apache/incubator-openwhisk.git openwhisk
Copy the code
When the clone is complete, it will be displayed as shown.
Apache OpenWhisk Project Clone
Go to the OpenWhisk directory and execute the script. OpenWhisk is developed by Scala and requires a Java environment to run. The following script implements the Installation of the Java environment and other required software:
cd openwhisk && cd tools/ubuntu-setup && ./all.sh
Copy the code
Figure 5-1 shows the installation and configuration of Apache OpenWhisk.
Install and configure Apache OpenWhisk
Ansible OpenWhisk used for deployment, environment variables defined under ansible/environments/group_vars/all:
limits:
invocationsPerMinute: "{{ limit_invocations_per_minute | default(60) }}"
concurrentInvocations: "{{ limit_invocations_concurrent | default(30) }}"
concurrentInvocationsSystem: "{{ limit_invocations_concurrent_system | default
(5000) }}"
firesPerMinute: "{{ limit_fires_per_minute | default(60) }}"
sequenceMaxLength: "{{ limit_sequence_max_length | default(50) }}"
Copy the code
The above program defines the limitations of OpenWhisk in the system.
- InvocationsPerMinute Specifies the number of actions invoked per minute for the same Namespace.
- ConcurrentInvocations indicates the number of concurrent calls to the same Namespace.
- ConcurrentInvocationsSystem said the number of concurrent calls all the Namespace in the system.
- FiresPerMinute indicates the number of Trigger calls per minute in the same Namespace.
- SequenceMaxLength specifies the maximum sequence length of an Action.
If need to modify the default values, you can add the value of the modified to file ansible/environments/local/group_vars/at the end of all. For example, if the maximum sequence length of an Action is 100, you can add sequenceMaxLength: 120 to the end of the file.
Next, configure a persistent database for OpenWhisk, with CouchDB and Cloudant options. Using CouchDB as an example, configure the following environment:
export OW_DB=CouchDB export OW_DB_USERNAME=root export OW_DB_PASSWORD=PASSWORD export OW_DB_PROTOCOL=http export OW_DB_HOST = 172.17.0.1 export OW_DB_PORT = 5984Copy the code
In the OpenWhisk/Ansible directory, run the script as shown in the figure.
ansible-playbook -i environments/local/ setup.yml
Copy the code
Script execution procedure
Next, deploy OpenWhisk using CouchDB and make sure you have db_local.ini locally. Execute deployment command in openWhisk/directory:
./gradlew distDocker
Copy the code
If problems occur during the deployment (as shown in the following figure), NPM may not be installed. In this case, run the following command.
The deployment process may report an error example
apt install npm
Copy the code
After a moment, you can see the Build success page, as shown in the figure below.
Build successful Example
Next go to the OpenWhisk /ansible directory:
ansible-playbook -i environments/local/ couchdb.yml
ansible-playbook -i environments/local/ initdb.yml
ansible-playbook -i environments/local/ wipe.yml
ansible-playbook -i environments/local/ apigateway.yml
ansible-playbook -i environments/local/ openwhisk.yml
ansible-playbook -i environments/local/ postdeploy.yml
Copy the code
The script execution process is shown in figure 1.
The image performs the script process
After successful deployment, OpenWhisk launches several Docker containers on the system. We can check it out with Docker PS:
docker ps --format "{{.Image}} \t {{.Names }}"
Copy the code
The container list after the successful installation is shown in the figure below.
List of containers after successful installation
Developer tools
OpenWhisk provides a unified command line interface to WSK. The generated WSK is under OpenWhisk /bin. It has two properties that need to be configured.
- API host THE API for deploying the host name or IP address of OpenWhisk.
- The Authorization Key (user name or password) is the API used to authorize operation of OpenWhisk.
Set API host to 172.17.0.1, as shown in the figure.
./bin/ WSK property set -- APIhost '172.17.0.1'Copy the code
Set the API host
Set the key:
./bin/wsk property set --auth `cat ansible/files/auth.guest
Copy the code
The permission Settings are shown in the figure below.
Image Setting Permissions
OpenWhisk stores CLI configuration information in ~/.wskprops. The location of this file can also be specified using the environment variable WSK_CONFIG_FILE.
Verify the CLI:
WSK action invoke /whisk.system/utils/echo -p message hello -- result {"message": "hello"}Copy the code
Acceptance testing
Create a simple action as follows:
# test.py
def main(args):
num = args.get("number", "30")
return {"fibonacci": F(int(num))}
def F(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return F(n - 1) + F(n - 2)
Copy the code
Create action:
/bin/wsk action create myfunction ./test.py --insecure
Copy the code
The function creation is shown.Create a function
Trigger action:
./bin/wsk -i action invoke myfunction --result --blocking --param nember 20
Copy the code
The result is shown.
Executive function
At this point, we have completed the deployment and testing of the OpenWhisk project.