头图

Author | Liu Yu (Jiang Yu)

Foreword: OpenWhisk is an open source, serverless cloud platform that can respond to various events by executing extended code in the runtime container without requiring users to care about the related infrastructure architecture.

Introduction to OpenWhisk

OpenWhisk is a cloud-based distributed event-driven programming service. OpenWhisk provides a programming model to register event handlers to cloud services to handle various services. It can support thousands of triggers and calls, and can respond to events of different scales.

OpenWhisk is built from many components that make OpenWhisk an excellent open source FaaS platform.

1.png
Apache OpenWhisk component structure

OpenWhisk deployment

The experimental machine operating system is Ubuntu 18.04 Desktop. Use the incubator-openwhisk provided on GitHub to install. If Git is not installed on this machine, you need to install Git first:

apt install git

Next, clone the repo to the local directory:

git clone https://github.com/apache/incubator-openwhisk.git openwhisk

After the cloning is complete, the display is shown as shown in the figure.

2.png
Apache OpenWhisk project Clone

Enter 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

The installation and configuration of Apache OpenWhisk is shown in the figure.

3.png
Apache OpenWhisk installation and configuration

OpenWhisk uses ansible for deployment, and environment variables are 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) }}"

The above program defines the limits of OpenWhisk in the system.

  • invocationsPerMinute represents the number of invocations of Action per minute for the same Namespace.
  • concurrentInvocations represents the number of concurrent invocations of the same Namespace.
  • concurrentInvocationsSystem represents the number of concurrent invocations of all Namespaces in the system.
  • firesPerMinute represents the number of Trigger calls per minute in the same Namespace.
  • sequenceMaxLength represents the maximum sequence length of the Action.

If you need to modify the above default values, you can add the modified values to the end of the file ansible/environments/local/group_vars/all. For example, if the maximum sequence length of Action is 100, you can add sequenceMaxLength: 120 to the end of the file.

Next, configure a persistent storage database for OpenWhisk, with CouchDB and Cloudant options. Take CouchDB as an example, configure the 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=5984

In the openwhisk/ansible directory, run the script as shown in the figure.

ansible-playbook -i environments/local/ setup.yml

4.png
Execute script procedure

Next, use CouchDB to deploy OpenWhisk and make sure that db_local.ini is available locally. Execute the deployment command in the openwhisk/ directory:

./gradlew distDocker

If there is a problem in the deployment process (as shown in the figure below), it may be caused by not installing npm. At this time, you can execute the following instructions.

5.png
Examples of possible errors during deployment

apt install npm

After a while, you can see the Build success page, as shown in the figure.

6.png
Build successful example

Next enter 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

The process of executing the script is shown in the figure.

7.png
Picture execution script process

After successful deployment, OpenWhisk will start several Docker containers in the system. We can check through docker ps:

docker ps --format "{{.Image}} \t {{.Names }}"

The container list after successful installation is shown in the figure.

8.png
List of containers after successful installation

Developer tools

OpenWhisk provides a unified command line interface wsk. The generated wsk is under openwhisk/bin. There are two properties that need to be configured.

  • API host is used to deploy the API of the host name or IP address of OpenWhisk.
  • Authorization key (user name or password) is used to authorize the operation of OpenWhisk's API.

Set the API host, the IP in the stand-alone configuration should be 172.17.0.1, as shown in the figure.

./bin/wsk property set --apihost '172.17.0.1'

9.png
Set up API host

Set the key:

./bin/wsk property set --auth `cat ansible/files/auth.guest

The permissions are set as shown in the figure.

10.png
Picture setting permissions

OpenWhisk stores CLI configuration information in ~/.wskprops. The location of this file can also be specified by the environment variable WSK_CONFIG_FILE.

Verify CLI:

wsk action invoke /whisk.system/utils/echo –p message hello –result
{
    "message": "hello"
}

Experience test

Create a simple action (action), the code is 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)

Create action:

/bin/wsk action create myfunction ./test.py  --insecure

The function is created as shown in the figure.
11.png
Create function

Trigger action:

./bin/wsk -i action invoke myfunction --result --blocking --param nember 20

Get the result, as shown in the figure.

12.png
Execution function

So far, we have completed the deployment and testing of the OpenWhisk project.


阿里云云原生
1k 声望302 粉丝