本文为Oracle Functions快速入门一文的续篇。在上一篇文章中,我们成功的运行了一个示例函数helloworld-func,当时使用的是私钥认证方式。
本文则尝试使用OCI Dynamic Group认证方式,虽然原理上不是那么直接,但认证方面的配置会简单很多。但前提是Fn Client必须是OCI中的实例。
本文参照文档Oracle Functions QuickStart。
以下为简要过程。
在root compartment,创建Policy FaaSPolicy
。定义如下,注意其中指定的compartment不能是child compartment:
Allow service FaaS to read repos in tenancy
Allow service FaaS to use virtual-network-family in compartment cp_mrd1_internal
在OCI中创建一实例,OCID为ocid1.instance.oc1.eu-frankfurt-1.anth...xj7a
创建dynamic group FaaSDG
,并添加rule如下:
ANY {instance.id = 'ocid1.instance.oc1.eu-frankfurt-1.anth...xj7a'}
将以下两个Policy添加到Policy FaaSPolicy
中:
Allow dynamic-group FaaSDG to manage functions-family in compartment cp_mrd1_internal
Allow dynamic-group FaaSDG to use virtual-network-family in compartment cp_mrd1_internal
Allow dynamic-group FaaSDG to manage repos in tenancy
Allow dynamic-group FaaSDG to read metrics in compartment cp_mrd1_internal
特别需要注意的是,以上4个policy的后两个不要遗漏,否则部署函数时会失败。
另外,添加以上policy后,有时不会马上生效,需要等待1-2分钟后才生效
安装Docker,由于此Instance Image中已经安装,因此无需再装。
验证如下:
$ sudo usermod -a -G docker opc
$ newgrp - docker
$ docker version
再OCI中生成Auth token并记录。
使用Auth token登录OCIR:
$ docker login fra.ocir.io
Username: ocichina001/oracleidentitycloudservice/***@***.com
Password:<Auth Token>
WARNING! Your password will be stored unencrypted in /home/opc/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
安装Fn Client:
$ curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh
$ fn version
在OCI中创建OCIR faasrepo
,属性为public。
配置Fn Context:
fn create context FnContext --provider oracle-ip
fn use context FnContext
fn update context oracle.compartment-id ocid1.compartment.oc1..aaaaaaaan7fnietgg454lxqjskw7jr6myx7otutynfxga7krwopajp5sw7la
fn update context api-url https://functions.eu-frankfurt-1.oci.oraclecloud.com
fn update context registry fra.ocir.io/ocichina001/faasrepo
在OCI中创建并部署应用helloworld-app-dg
。
$ fn init --runtime java helloworld-func
Creating function at: ./helloworld-func
Function boilerplate generated.
func.yaml created.
$ cd helloworld-func
$ fn -v deploy --app helloworld-app-dg
部署成功后,可以在OCIR中看到Docker Image:
在Application中可以看到函数已关联:
手工触发函数:
$ time fn invoke helloworld-app-dg helloworld-func
Hello, world!
real 0m44.238s
user 0m0.281s
sys 0m0.009s
$ time fn invoke helloworld-app-dg helloworld-func
Hello, world!
real 0m1.200s
user 0m0.420s
sys 0m0.019s
最后我们查看Fn Context:
$ fn list ctx
CURRENT NAME PROVIDER API URL REGISTRY
* FnContext oracle-ip https://functions.eu-frankfurt-1.oci.oraclecloud.com fra.ocir.io/ocichina001/faasrepo
default default http://localhost:8080