跟我一起学Knative(9)--集成Camel-K

在一般的大型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的基本概念。

跟我一起学Knative(9)--集成Camel-K_第1张图片

实际情况是该脚本被包装到“集成”自定义资源中并添加到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格式并与其他使用者交换。

跟我一起学Knative(9)--集成Camel-K_第2张图片

部署

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: ""

    # 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 有了一些实战经验,再详细些一些。

你可能感兴趣的:(serverless,kubernetes,k8s)