
作者 | 周正喜 阿里云技术专家 爱好云原生,深度参与 OAM 社区
大家都知道,应用开放模型 Open Application Model(OAM) 将应用的工作负载(Workload)分为三种 —— 核心型、标准型和扩展型,这三者的主要区别在于一个 OAM 平台对于具体某一类工作负载进行实现的自由度不同。
其中,OAM 社区中目前唯一一个核心工作负载是Containerized Workload,它用来描述一个基于容器的工作负载,可以理解为是 Kubernetes Deployment 的简化版(去掉了 PodSecurityPolicy 等大量与业务研发无关的字段)。
不过,很多读者可能会有疑问:对于 Kubernetes 内置的工作负载 OAM 是否还能直接支持呢?
答案当然是肯定的,而且这是 OAM 作为 Kubernetes 原生的应用定义模型的默认能力。
下面,本文就以 Deployment 为例,介绍如何使用 OAM 基于 Kubernetes 的内置工作负载来定义和管理云原生应用。

示例准备
基于 GitHubFoodTrucks(旧金山美味街边小吃地图应用)项目,构建镜像 zzxwill/foodtrucks-web:0.1.1,加上依赖的 Elasticsearch 镜像,在默认情况下,它的 Deployment 描述文件food-truck-deployment.yaml 如下所示:
apiVersion: apps/v1
kind: Deployment
metadata:
name: food-trucks-deployment
labels:
app: food-trucks
spec:
selector:
matchLabels:
app: food-trucks
template:
metadata:
labels:
app: food-trucks
spec:
containers:
- name: food-trucks-web
image: zzxwill/foodtrucks-web:0.1.1
env:
- name: discovery.type
value: single-node
ports:
- containerPort: 5000
- name: es
image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
ports:
- containerPort: 9200
- containerPort: 9300
如果将上述 yaml 文件提交到 Kubernetes 集群,通过 port-forward 可以通过浏览器查看效果:


定义 Component 与 Workload



部署这个应用
apiVersion: core.oam.dev/v1alpha2
kind: ApplicationConfiguration
metadata:
name: example-deployment-appconfig
spec:
components:
- componentName: example-deployment
✗ kubectl apply -f sample-deployment-component.yaml
component.core.oam.dev/example-deployment created
✗ kubectl apply -f sample-applicationconfiguration.yaml
applicationconfiguration.core.oam.dev/example-deployment-appconfig created
✗ kubectl describe applicationconfiguration example-deployment-appconfig
Name: example-deployment-appconfig
...
Status:
Conditions:
Message: cannot apply components: cannot apply workload "food-trucks-deployment": cannot get object: deployments.apps "food-trucks-deployment" is forbidden: User "system:serviceaccount:crossplane-system:crossplane" cannot get resource "deployments" in API group "apps" in the namespace "default"
Reason: Encountered an error during resource reconciliation
...
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: deployment-clusterrole-poc
rules:
- apiGroups:
- apps
resources:
- deployments
verbs:
- "*"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: oam-food-trucks
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: deployment-clusterrole-poc
subjects:
- kind: ServiceAccount
namespace: crossplane-system
name: crossplane
✗ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
food-trucks-deployment 1/1 1 1 2m20s
✗ kubectl port-forward deployment/food-trucks-deployment 5000:5000
Forwarding from 127.0.0.1:5000 -> 5000
Forwarding from [::1]:5000 -> 5000
Handling connection for 5000
Handling connection for 5000


什么时候使用 Deployment ?

为什么使用 OAM Component 来定义应用?

了解更多和贡献
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69975341/viewspace-2699804/ ,如需转载,请注明出处,否则将追究法律责任。