跟我一起學Knative(9)--整合Camel-K

iyacontrol發表於2020-05-18

在一般的大型IT組織中,很少有人會構建完全隔離的新應用程式,而該應用程式會與所有其他舊系統或新系統完全分離。許多實時用例要求新舊系統共享和交換資料。

Apache Camel是可幫助您整合系統的開源框架。 Apache Camel允許整合系統在它們之間生成和使用資料。它提供了300多個元件,其中包括與TCP,ActiveMQ,FTP,http://Salesforce.com之類的源的整合聯結器,這使得整合異構系統變得更加容易。企業整合模式(EIP)提供了許多常見整合問題的解決方案,Apache Camel透過其豐富的領域特定語言(DSL)提供了這些模式的實現,從而使開發人員更容易輕鬆地應用EIP。

Apache Camel-K旨在簡化Apache Camel整合的程式設計和部署模型。透過使用Apache Camel-K,整合開發人員現在可以專注於使用Java,JavaScript,Groovy,XML或YAML中的Camel DSL編寫整合,而無需擔心如何打包和部署它們。

Camel K 工作原理

下圖說明了Camel K的基本概念。

實際情況是該指令碼被包裝到“整合”自定義資源中並新增到Kubernetes名稱空間中。然後,Camel K Operator(基於operator SDK)將檢測到新的Integration並將其具體化為正在執行的容器。通常,Camel K實現與Kubernetes部署的整合,但是在Knative上執行時,它使用自動縮放服務。

整合

Camel-K 如何與Knative整合那?主要體現在下面兩個方面。

  • 使用Camel-K,我們可以將Apache Camel路由部署為Knative Serving服務,即Apache Camel路由將自動擴充套件,並在不需要時自動縮小為零。
  • 使用Camel-K,我們還可以透過Apache Camel端點交換Knative Eventing事件;交換訊息(事件有效負載)會自動轉換為Cloud Events格式並與其他使用者交換。

部署

1:部署Apache Camel K

在要執行Camel源的任何名稱空間中安裝Apache Camel K Operator。

與Camel來源相容的首選版本是Camel K v1.0.0-M4

Apache Camel K手冊中提供了安裝說明。文件包括針對常見Kubernetes環境(包括開發叢集)的特定說明。

2:從“事件源”釋出頁面中的camel.yaml安裝Camel源:

kubectl apply --filename camel.yaml

我們檢視一下camel.yam的具體內容:

# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Namespace
metadata:
  name: knative-sources
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"

---
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ServiceAccount
metadata:
  name: camel-controller-manager
  namespace: knative-sources
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"

---
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: camel-controller
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
rules:
- apiGroups:
  - apps
  resources:
  - deployments
  verbs: &everything
  - get
  - list
  - watch
  - create
  - update
  - patch
  - delete
- apiGroups:
  - rbac.authorization.k8s.io
  resources:
  - clusterroles
  verbs:
  - list
- apiGroups:
  - ""
  resources:
  - events
  - configmaps
  verbs: *everything
- apiGroups:
  - sources.knative.dev
  resources:
  - camelsources
  verbs: *everything
- apiGroups:
  - sources.knative.dev
  resources:
  - camelsources/status
  - camelsources/finalizers
  verbs:
  - get
  - update
  - patch
- apiGroups:
  - camel.apache.org
  resources:
  - '*'
  verbs: *everything
---
# The role is needed for the aggregated role source-observer in knative-eventing to provide readonly access to "Sources".
# See https://github.com/knative/eventing/blob/master/config/200-source-observer-clusterrole.yaml.
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: eventing-contrib-camel-source-observer
  labels:
    eventing.knative.dev/release: devel
    duck.knative.dev/source: "true"
rules:
- apiGroups:
  - "sources.knative.dev"
  resources:
  - "camelsources"
  verbs:
  - get
  - list
  - watch

---
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: camel-controller-rolebinding
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: camel-controller
subjects:
- kind: ServiceAccount
  name: camel-controller-manager
  namespace: knative-sources
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: eventing-sources-camel-controller-addressable-resolver
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
subjects:
- kind: ServiceAccount
  name: camel-controller-manager
  namespace: knative-sources
# An aggregated ClusterRole for all Addressable CRDs.
# Ref: https://github.com/knative/eventing/blob/master/config/200-addressable-resolvers-clusterrole.yaml
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: addressable-resolver

---
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: camel-source-observer
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
    duck.knative.dev/source: "true"
rules:
- apiGroups:
  - sources.knative.dev
  resources:
  - camelsources
  verbs:
  - get
  - list
  - watch

---
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
    eventing.knative.dev/source: "true"
    duck.knative.dev/source: "true"
    knative.dev/crd-install: "true"
  name: camelsources.sources.knative.dev
spec:
  group: sources.knative.dev
  names:
    categories:
    - all
    - knative
    - eventing
    - sources
    kind: CamelSource
    plural: camelsources
  scope: Namespaced
  subresources:
    status: {}
  additionalPrinterColumns:
  - name: Ready
    type: string
    JSONPath: '.status.conditions[?(@.type=="Ready")].status'
  - name: Reason
    type: string
    JSONPath: '.status.conditions[?(@.type=="Ready")].reason'
  - name: Age
    type: date
    JSONPath: .metadata.creationTimestamp
  validation:
    openAPIV3Schema:
      properties:
        apiVersion:
          type: string
        kind:
          type: string
        metadata:
          type: object
        spec:
          properties:
            sink:
              type: object
              description: "Reference to an object that will resolve to a domain name
                to use as the sink."
            ceOverrides:
              type: object
              description: "Defines overrides to control modifications of the event
                sent to the sink."
            source:
              properties:
                flow:
                  type: object
                integration:
                  type: object
              type: object
          required:
          - source
          type: object
        status:
          properties:
            conditions:
              items:
                properties:
                  lastTransitionTime:
                    type: string
                  message:
                    type: string
                  reason:
                    type: string
                  severity:
                    type: string
                  status:
                    type: string
                  type:
                    type: string
                required:
                - type
                - status
                type: object
              type: array
            sinkUri:
              type: string
          type: object
  version: v1alpha1

---
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Service
metadata:
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
    control-plane: camel-controller-manager
  name: camel-controller-manager
  namespace: knative-sources
spec:
  selector:
    control-plane: camel-controller-manager
  ports:
  - name: https-camel
    port: 443

---
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
    control-plane: camel-controller-manager
  name: camel-controller-manager
  namespace: knative-sources
spec:
  selector:
    matchLabels:
      control-plane: camel-controller-manager
  serviceName: camel-controller-manager
  template:
    metadata:
      labels:
        control-plane: camel-controller-manager
    spec:
      containers:
      - image: gcr.io/knative-releases/knative.dev/eventing-contrib/camel/source/cmd/controller@sha256:a1dc668acca5be07e290131d7497eb5ad2f0fe44b53aaac210afbe3401d25158
        name: manager
        resources:
          requests:
            cpu: 20m
            memory: 20Mi
        env:
        - name: SYSTEM_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: CONFIG_LOGGING_NAME
          value: config-logging
        - name: CONFIG_OBSERVABILITY_NAME
          value: config-observability
        - name: CONFIG_LEADERELECTION_NAME
          value: config-leader-election-camel
        - name: METRICS_DOMAIN
          value: knative.dev/sources
      serviceAccount: camel-controller-manager
      terminationGracePeriodSeconds: 10

---
# Copyright 2020 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-logging
  namespace: knative-sources
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
data:
  # Common configuration for all Knative codebase
  zap-logger-config: |
    {
      "level": "info",
      "development": false,
      "outputPaths": ["stdout"],
      "errorOutputPaths": ["stderr"],
      "encoding": "json",
      "encoderConfig": {
        "timeKey": "ts",
        "levelKey": "level",
        "nameKey": "logger",
        "callerKey": "caller",
        "messageKey": "msg",
        "stacktraceKey": "stacktrace",
        "lineEnding": "",
        "levelEncoder": "",
        "timeEncoder": "iso8601",
        "durationEncoder": "",
        "callerEncoder": ""
      }
    }
  # Log level overrides
  # For all components changes are be picked up immediately.
  loglevel.controller: "info"
  loglevel.webhook: "info"

---
# Copyright 2020 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-observability
  namespace: knative-sources
  labels:
    contrib.eventing.knative.dev/release: "v0.14.1"
data:
  _example: |
    ################################
    #                              #
    #    EXAMPLE CONFIGURATION     #
    #                              #
    ################################

    # This block is not actually functional configuration,
    # but serves to illustrate the available configuration
    # options and document them in a way that is accessible
    # to users that `kubectl edit` this config map.
    #
    # These sample configuration options may be copied out of
    # this example block and unindented to be in the data block
    # to actually change the configuration.

    # metrics.backend-destination field specifies the system metrics destination.
    # It supports either prometheus (the default) or stackdriver.
    # Note: Using stackdriver will incur additional charges
    metrics.backend-destination: prometheus

    # metrics.request-metrics-backend-destination specifies the request metrics
    # destination. If non-empty, it enables queue proxy to send request metrics.
    # Currently supported values: prometheus, stackdriver.
    metrics.request-metrics-backend-destination: prometheus

    # metrics.stackdriver-project-id field specifies the stackdriver project ID. This
    # field is optional. When running on GCE, application default credentials will be
    # used if this field is not provided.
    metrics.stackdriver-project-id: "<your stackdriver project id>"

    # metrics.allow-stackdriver-custom-metrics indicates whether it is allowed to send metrics to
    # Stackdriver using "global" resource type and custom metric type if the
    # metrics are not supported by "knative_broker", "knative_trigger", and "knative_source" resource types.
    # Setting this flag to "true" could cause extra Stackdriver charge.
    # If metrics.backend-destination is not Stackdriver, this is ignored.
    metrics.allow-stackdriver-custom-metrics: "false"

    # profiling.enable indicates whether it is allowed to retrieve runtime profiling data from
    # the pods via an HTTP server in the format expected by the pprof visualization tool. When
    # enabled, the Knative Eventing pods expose the profiling data on an alternate HTTP port 8008.
    # The HTTP context root for profiling is then /debug/pprof/.
    profiling.enable: "false"

---

建立測試資源

所有CamelSource示例都使用一些測試資源來顯示生成的事件。需要建立以下資源:

  • 一個簡單的Knative事件顯示服務,它將進入的事件列印到其日誌中
  • 名為camel-test的記憶體channel,用於緩衝由事件源建立的事件
  • 從測試Chanel到事件顯示服務的直接事件訂閱

部署 顯示服務:

kubectl apply --filename display_resources.yaml

我們檢視一下display_resources.yaml的內容:

# Channel for testing events.

apiVersion: messaging.knative.dev/v1alpha1
kind: InMemoryChannel
metadata:
  name: camel-test

---

# Subscription from the CamelSource's output Channel to the Knative Service below.

apiVersion: messaging.knative.dev/v1alpha1
kind: Subscription
metadata:
  name: camel-source-display
spec:
  channel:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
    name: camel-test
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: camel-event-display

---

# This is a very simple Knative Service that prints the input event to its log.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: camel-event-display
spec:
  template:
    spec:
      containers:
        - image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display@sha256:f4628e97a836c77ed38bd3b6fd3d0b06de4d5e7db6704772fe674d48b20bd477

總結

本文簡單介紹了一下Camel K以及Camel K 如何與Knative的整合。

後續待對Camel K 有了一些實戰經驗,再詳細些一些。

相關文章