基于 GitHub FoodTrucks (旧金山美味街边小吃地图应用)项目,构建镜像 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 可以通过浏览器查看效果:
在 OAM 中, 一个应用是由多个 Component(组件)构成的,而一个 Component 里的核心字段,就是 Workload(工作负载)。
所以说,像 Kubernetes Deployment、StatefulSet 等内置的工作负载,其实天生就可以被定义为 OAM Component 中的 Workload。比如下面这个 sample-deployment-component.yaml 文件,可以看到,.spec.workload 的内容,就是一个 Deployment,也就是 food-truck-deployment.yaml 里定义的 Deployment。
接下来,我们就将上述 OAM Component 提交到 Kubernetes 集群验证一下。
在 OAM 中,我们需要编写一个应用配置 ApplicationConfiguration 来组织所有的 OAM Component。由于只有一个 Component,本例中的 sample-applicationconfiguration.yaml 非常简单,如下所示:
apiVersion: core.oam.dev/v1alpha2
kind: ApplicationConfiguration
metadata:
name: example-deployment-appconfig
spec:
components:
- componentName: example-deployment
提交 OAM Component 和 ApplicationConfiguration YAML 文件给 Kubernetes:
✗ 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
不过,如果这个时候你查看 example-deployment-appconfig 的执行情况,会发现如下报错:
✗ 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
...
这是因为 OAM 的 Kubernetes 插件权限不足导致的,所以不要忘记设置合理的 ClusterRole 和 ClusterRoleBinding。
提交如下的授权文件 rbac.yaml,ApplicationConfiguration 可以执行成功。
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
继续查看 deployments,并设置端口转发:
✗ 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
本文系作者在时代Java发表,未经许可,不得转载。
如有侵权,请联系nowjava@qq.com删除。