OpenWhisk を使用したサーバーレス コンピューティング、パート 4

OpenWhisk を使用したサーバーレス コンピューティング、パート 4

この記事で、著者による OpenWhisk に関する一連の翻訳ノートは終了します プリティ・デサイ。 今日は、現在のバージョンのアプリケーションで動作するように修正されたコマンドを使用して、Kubernetes 上に OpenWhisk をデプロイするプロセスを見ていきます。 また、Nodejs ランタイムを使用して Kubernetes 上で Knative と TektonCD を使用して OpenWhisk 関数を実行するプロセスについても説明します。

Kubernetes への OpenWhisk のデプロイ

数日間かけて、OpenWhisk を Kubernetes にデプロイして、シンプルで高速なテスト環境を作成する実験をしました。 私は Kubernetes を初めて使用するため、デプロイメントが成功するまでに XNUMX 日半かかったと思います。 で これ リポジトリには、Kubernetes に OpenWhisk をデプロイするための非常に明確な手順が記載されています。 Mac 向けに作成された展開手順は次のとおりです (私は Linux の方が好きなので、すべて Linux で行います。 — 約翻訳者)。

  1. パッケージマネージャーのインストール asdf、その後は自動的に修正されます ~/.bash_profile または次のような同等のもの:

$ brew install asdf
$ [ -s "/usr/local/opt/asdf/asdf.sh" ] && . /usr/local/opt/asdf/asdf.sh
$ source ~/.bash_profile

[Linux では、brew が利用可能ですが、この手順は必要ありません。 — 約翻訳者]

  1. プラグインの追加 minikube и kubelet:

$ asdf plugin-add kubectl
$ asdf plugin-add minikube

[繰り返しになりますが、Linux ではこの手順をスキップしてください。 — 約翻訳者]

  1. minikube と kubelet をインストールします。

$ asdf install kubectl 1.9.0
$ asdf global kubectl 1.9.0
$ asdf install minikube 0.25.2
$ asdf global minikube 0.25.2

[特定のバージョンがインストールされていますが、Linux で利用可能な最新バージョンをすべて確認しました。 最新のものを安全にインストールできると思います。 — 約翻訳者]

Linux では、このステップは次のように行われます (すべては ~/bin に置かれます。これは私の PATH にリストされています、翻訳者のメモ)。

$ curl -L0 minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && mv minikube ~/bin/
$ curl -L0 https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && mv kubectl ~/bin/

  1. minikube 仮想マシンを作成します (VirtualBox が事前にインストールされている必要があります)。

$ minikube start --cpus 2 --memory 4096 --kubernetes-version=v1.9.0 --extra-config=apiserver.Authorization.Mode=RBAC

[チームと協力すればすべてがうまくいく minikube start 、パラメータなし、デフォルト値あり。 — 約翻訳者]

$ minikube start
  minikube v1.5.2 on Debian 8.11
  Automatically selected the 'virtualbox' driver
  Downloading VM boot image ...
    > minikube-v1.5.1.iso.sha256: 65 B / 65 B [--------------] 100.00% ? p/s 0s
    > minikube-v1.5.1.iso: 143.76 MiB / 143.76 MiB [-] 100.00% 5.63 MiB p/s 26s
  Creating virtualbox VM (CPUs=2, Memory=4096MB, Disk=20000MB) ...
  Preparing Kubernetes v1.16.2 on Docker '18.09.9' ...
  Downloading kubelet v1.16.2
  Downloading kubeadm v1.16.2
  Pulling images ...
  Launching Kubernetes ...  Waiting for: apiserver
  Done! kubectl is now configured to use "minikube"

  1. Docker のネットワークを無差別モードに切り替える:

$ minikube ssh -- sudo ip link set docker0 promisc on

  1. 名前空間を作成し、ワーカー ノードをマークします。

$ kubectl create namespace openwhisk
$ kubectl label nodes --all openwhisk-role=invoker

  1. リポジトリの内容を取得し、mycluster.yaml ファイル内の ingress のタイプをオーバーライドします。

$ git clone https://github.com/apache/incubator-openwhisk-deploy-kube.git
$ cd incubator-openwhisk-deploy-kube/
$ cat << "EOF" > mycluster.yaml
whisk:
    ingress:
        type: NodePort
            api_host_name: 192.168.99.100
            api_host_port: 31001
nginx:
    httpsNodePort: 31001
EOF

  1. Helm をインストールし、それを使用してデプロイします。

$ brew install kubernetes-helm
$ helm init # init Helm Tiller, не нужно на Helm v3+
$ kubectl get pods -n kube-system # verify that tiller-deploy is in the running state, не нужно на helm v3+
$ kubectl create clusterrolebinding tiller-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default
$ helm install ./openwhisk/helm/ --namespace=openwhisk -f mycluster.yaml

[最新バージョンの Linux (v3.0.1 が利用可能でした) では、少し異なります。 — 約翻訳者]

$ curl -L0 https://get.helm.sh/helm-v3.0.1-linux-amd64.tar.gz | tar -xzvf - linux-amd64/helm --strip-components=1; sudo mv helm /usr/local/bin
$ kubectl create clusterrolebinding tiller-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default
$ helm install ./openwhisk/helm/ --namespace=openwhisk --generate-name -f mycluster.yaml

  1. すべてが起動していることを確認します (STATUS = 実行中または完了)。

$ kubectl get pods -n openwhisk
NAME                                                              READY   STATUS      RESTARTS   AGE
openwhisk-1576070780-alarmprovider-6868dc694-plvpf                1/1     Running     1          1d5h
openwhisk-1576070780-apigateway-8d56f4979-825hf                   1/1     Running     1          1d5h
openwhisk-1576070780-cloudantprovider-544bb46596-9scph            1/1     Running     1          1d5h
openwhisk-1576070780-controller-0                                 1/1     Running     2          1d5h
openwhisk-1576070780-couchdb-7fd7f6c7cc-42tw6                     1/1     Running     1          1d5h
openwhisk-1576070780-gen-certs-z9nsb                              0/1     Completed   0          1d5h
openwhisk-1576070780-init-couchdb-r2vmt                           0/1     Completed   0          1d5h
openwhisk-1576070780-install-packages-27dtr                       0/1     Completed   0          1d4h
openwhisk-1576070780-invoker-0                                    1/1     Running     1          1d5h
openwhisk-1576070780-kafka-0                                      1/1     Running     1          1d5h
openwhisk-1576070780-kafkaprovider-f8b4cf4fc-7z4gt                1/1     Running     1          1d5h
openwhisk-1576070780-nginx-6dbdbf69bc-5x76n                       1/1     Running     1          1d5h
openwhisk-1576070780-redis-cfd8756f4-hkrt6                        1/1     Running     1          1d5h
openwhisk-1576070780-wskadmin                                     1/1     Running     1          1d5h
openwhisk-1576070780-zookeeper-0                                  1/1     Running     1          1d5h
wskopenwhisk-1576070780-invoker-00-1-prewarm-nodejs10             1/1     Running     0          61s
wskopenwhisk-1576070780-invoker-00-2-prewarm-nodejs10             1/1     Running     0          61s
wskopenwhisk-1576070780-invoker-00-3-whisksystem-invokerhealtht   1/1     Running     0          59s

  1. wsk が機能するように設定します。

$ wsk property set --apihost 192.168.99.100:31001
$ wsk property set --auth 23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP

私たちはチェックします:

$ wsk -i list
Entities in namespace: default
packages
actions
triggers
rules

問題と解決策

getsockopt: 接続が拒否されました

$ wsk -i list
error: Unable to obtain the list of entities for namespace 'default': Get http://192.168.99.100:31001/api/v1/namespaces/_/actions?limit=0&skip=0: dial tcp 192.168.99.100:31001: getsockopt: connection refused

コンテナが名前空間内にあることを確認する openwhisk 状態で Running、 なぜなら時々エラーでクラッシュする CreateContainerConfigError.

実行者はまだ初期化中 — Init:1/2

さまざまなランタイム環境をダウンロードするプロセスには時間がかかることがあります。 処理を高速化するために、ファイル内で短縮された最小リストを指定できます。 mycluster.yaml:

whisk:
  runtimes: "runtimes-minimal-travis.json"

名前が入ったコンテナ -パッケージのインストール- クラッシュしてエラーになる

活性テストのタイムアウトを増やすだけです。

Knative 上に OpenWhisk をインストールする

Priti Desai は、Knative Build と BuildTemplates を使用して、IBM クラウドのクラスター上と通常の minikube 上でインストールを実行しました。 方法に基づいて、minukube の上にもインストールします。 それは説明されました 以前のブログで、最新のソフトウェア バージョンを使用しています。 Knative Build と BuildTemplates は正式に非推奨になったので、推奨される代替品である Tekton Pipelines を使用します。 記事の残りの部分は Tekton Pipelines のドキュメントを読んだ後に書かれていますが、Priti のアイデアに基づいています。 動作するには、いくつかの Docker レジストリにアクセスする必要があります。私は、オリジナルの作成者と同様に、DockerHub を使用します。

$ curl -L0 https://github.com/solo-io/gloo/releases/download/v1.2.10/glooctl-linux-amd64; chmod +x glooctl-linux-amd64; mv glooctl-linux-amd64 ~/bin
$ glooctl install knative
$ kubectl get pods -n knative-serving
NAME                              READY   STATUS    RESTARTS   AGE
activator-77fc555665-rvrst        1/1     Running   0          2m23s
autoscaler-5c98b7c9b6-x8hh4       1/1     Running   0          2m21s
autoscaler-hpa-5cfd4f6845-w87kq   1/1     Running   0          2m22s
controller-7fd74c8f67-tprm8       1/1     Running   0          2m19s
webhook-74847bb77c-txr2g          1/1     Running   0          2m17s
$ kubectl get pods -n gloo-system
NAME                                      READY   STATUS    RESTARTS   AGE
discovery-859d7fbc9c-8xhvh                1/1     Running   0          51s
gloo-545886d9c6-85mwt                     1/1     Running   0          51s
ingress-67d4996d75-lkkmw                  1/1     Running   0          50s
knative-external-proxy-767dfd656c-wwv2z   1/1     Running   0          50s
knative-internal-proxy-6fdddcc6b5-7vqd8   1/1     Running   0          51s

OpenWhisk を使用したサーバーレス コンピューティング、パート 4
Knative 上で OpenWhisk を構築して実行する

  1. コンテンツの取得 このリポジトリ:

$ git clone https://github.com/tektoncd/catalog/
$ cd catalog/openwhisk

  1. レジストリにアクセスするためのデータを環境変数として設定し、Kubernetes シークレットとして保存します。

$ export DOCKER_USERNAME=<your docker hub username>
$ export DOCKER_PASSWORD=<your docker hub password>
$ sed -e 's/${DOCKER_USERNAME}/'"$DOCKER_USERNAME"'/' -e 's/${DOCKER_PASSWORD}/'"$DOCKER_PASSWORD"'/' docker-secret.yaml.tmpl > docker-secret.yaml
$ kubectl apply -f docker-secret.yaml

私たちはチェックします:

$ kubectl get secret
NAME                    TYPE                                  DATA      AGE
dockerhub-user-pass     kubernetes.io/basic-auth              2         21s

  1. 環境を構築するためのアカウントを作成します。

$ kubectl apply -f service-account.yaml

私たちはチェックします:

$ kubectl get serviceaccount/openwhisk-runtime-builder
NAME                        SECRETS   AGE
openwhisk-runtime-builder   2         31m

  1. OpenWhisk のイメージを構築するタスクを作成する

$ kubectl apply -f openwhisk.yaml
task.tekton.dev/openwhisk created

  1. タスクを実行してイメージを構築します (例として NodeJS を使用)。

次の内容を含む taskrun.yaml ファイルを作成します。

# Git Pipeline Resource for OpenWhisk NodeJS Runtime
apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
    name: openwhisk-nodejs-runtime-git
spec:
    type: git
    params:
        - name: revision
          value: master
        - name: url
          value: https://github.com/apache/openwhisk-runtime-nodejs.git
---

# Image Pipeline Resource for OpenWhisk NodeJS Sample Application
apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
    name: openwhisk-nodejs-helloworld-image
spec:
    type: image
    params:
        - name: url
          value: docker.io/${DOCKER_USERNAME}/openwhisk-nodejs-helloworld
---

# Task Run to build NodeJS image with the action source
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
    name: openwhisk-nodejs-helloworld
spec:
    serviceAccountName: openwhisk-runtime-builder
    taskRef:
        name: openwhisk
    inputs:
        resources:
            - name: runtime-git
              resourceRef:
                name: openwhisk-nodejs-runtime-git
        params:
            - name: DOCKERFILE
              value: "./runtime-git/core/nodejs10Action/knative/Dockerfile"
            - name: OW_ACTION_NAME
              value: "nodejs-helloworld"
            - name: OW_ACTION_CODE
              value: "function main() {return {payload: 'Hello World!'};}"
            - name: OW_PROJECT_URL
              value: ""
    outputs:
        resources:
            - name: runtime-image
              resourceRef:
                name: openwhisk-nodejs-helloworld-image
---

このファイルに現在のデータを適用します。

$ sed 's/${DOCKER_USERNAME}/'"$DOCKER_USERNAME"'/' -i taskrun.yaml

適用します:

$ kubectl apply -f taskrun.yaml
pipelineresource.tekton.dev/openwhisk-nodejs-runtime-git created
pipelineresource.tekton.dev/openwhisk-nodejs-helloworld-image created
taskrun.tekton.dev/openwhisk-nodejs-helloworld created

作業の確認には、ポッドの名前の取得とそのステータスの表示が含まれます。 次のように、各ステップの実行ログを表示することもできます。

$ kubectl get taskrun
NAME                          SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
openwhisk-nodejs-helloworld   True        Succeeded   5m15s       44s
$ kubectl get pod openwhisk-nodejs-helloworld-pod-4640d3
NAME                                     READY   STATUS      RESTARTS   AGE
openwhisk-nodejs-helloworld-pod-4640d3   0/6     Completed   0          5m20s
$ kubectl logs openwhisk-nodejs-helloworld-pod-4640d3 -c step-git-source-openwhisk-nodejs-runtime-git-r8vhr
{"level":"info","ts":1576532931.5880227,"logger":"fallback-logger","caller":"logging/config.go:69","msg":"Fetch GitHub commit ID from kodata failed: open /var/run/ko/refs/heads/master: no such file or directory"}
{"level":"info","ts":1576532936.538926,"logger":"fallback-logger","caller":"git/git.go:81","msg":"Successfully cloned https://github.com/apache/openwhisk-runtime-nodejs.git @ master in path /workspace/runtime-git"}
{"level":"warn","ts":1576532936.5395331,"logger":"fallback-logger","caller":"git/git.go:128","msg":"Unexpected error: creating symlink: symlink /tekton/home/.ssh /root/.ssh: file exists"}
{"level":"info","ts":1576532936.8202565,"logger":"fallback-logger","caller":"git/git.go:109","msg":"Successfully initialized and updated submodules in path /workspace/runtime-git"}

実行後、レジストリにイメージが作成されます。このイメージは、kn ユーティリティを使用してデプロイできます。このイメージは、Knative サービスで動作するように設計されています。次に例を示します。

kn service create nodejs-helloworld --image docker.io/${DOCKER_USERNAME}/openwhisk-nodejs-helloworld
Service 'nodejs-helloworld' successfully created in namespace 'default'.
Waiting for service 'nodejs-helloworld' to become ready ... OK

Service URL:
http://nodejs-helloworld.default.example.com

Gloo を使用している場合は、次のようにしてその機能を確認できます。

$ curl -H "Host: nodejs-helloworld.default.example.com" -X POST $(glooctl proxy url --name knative-external-proxy)
{"OK":true}
$ curl -H "Host: nodejs-helloworld.default.example.com" -X POST $(glooctl proxy url --name knative-external-proxy)
{"payload":"Hello World!"}

シリーズの他の記事

OpenWhisk を使用したサーバーレス コンピューティング、パート 1
OpenWhisk を使用したサーバーレス コンピューティング、パート 2
OpenWhisk を使用したサーバーレス コンピューティング、パート 3
OpenWhisk を使用したサーバーレス コンピューティング、パート 4

出所: habr.com

コメントを追加します