Initialize Env Infra Resources
This section will introduce how to initialize and destroy infrastructure of environment with KubeVela easily.
An Application development team usually needs to initialize some shared environment for users. An environment is a logical concept that represents a set of common infrastructure resources for Applications.
For example, a team usually wants two environments: one for development, and one for production.
In general, the infra resource types that can be initialized include the following types:
One or more Kubernetes clusters. Different environments may need different sizes and versions of Kubernetes clusters. Environment initialization can also manage multiple clusters .
Any type of Kubernetes custom resources (CRDs) and system plug-ins can be set up in environment initialization.
All kinds of shared resources and services. For example. shared resources in microservices. These shared resources can be a microservice component, cloud database, cache, load balancer, API gateway, and so on.
Various management policies and processes. An environment may have different global policies. The policy can be chaos test, security scan, SLO and son on; the process can be initializing a database table, registering an automatic discovery configuration, and so on.
KubeVela allows you to use different resources to initialize the environment.
You can use the Policy and Workflow in your Application. Note that there may be dependencies between initializations, we can use depends-on-app in workflow to do it.
The initialization of different environments has dependencies. Common resources can be separated as dependencies. In this way, reusable initialization modules can be formed.
For example, if both the test and develop environments rely on the same controllers, these controllers can be pulled out and initialized as separate environments, specifying dependency initialization in both the development and test environments.
If we want to use some CRD controller like OpenKruise in cluster, we can use Helm to initialize kruise.
We can directly use Application to initialize a kruise environment. The application below will deploy a kruise controller in cluster.
We have to enable FluxCD addon in cluster since we use Helm to deploy kruise. We can use depends-on-app to make sure addon-fluxcd is deployed before kruise, we also use apply-once policy here to make sure we’ll only apply the application once for initialization.
depends-on-appwill check if the cluster has the application withnameandnamespacedefines inproperties. If the application exists, the next step will be executed after the application is running. If the application do not exists, KubeVela will check the ConfigMap with the same name, and read the config of the Application and apply to cluster. For more information, please refer to depends-on-app.
cat <<EOF | vela up -f -apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: kruisenamespace: vela-systemspec:components:- name: kruisetype: helmproperties:repoType: helmurl: https://openkruise.github.io/charts/chart: kruiseversion: 1.2.0git:branch: mastervalues:featureGates: PreDownloadImageForInPlaceUpdate=truepolicies:- name: apply-oncetype: apply-onceproperties:enable: trueworkflow:steps:- name: check-fluxtype: depends-on-appproperties:name: addon-fluxcdnamespace: vela-system- name: apply-kruisetype: apply-componentproperties:component: kruiseEOF
Check the application in cluster:
vela status kruise -n vela-system
Expected Outcome
About:Name: kruiseNamespace: vela-systemCreated at: 2022-10-31 18:10:27 +0800 CSTStatus: runningWorkflow:mode: StepByStep-DAGfinished: trueSuspend: falseTerminated: falseSteps- id: l6apfsi5c2name: check-fluxtype: depends-on-appphase: succeeded- id: p2nqell47wname: apply-kruisetype: apply-componentphase: succeededServices:- Name: kruiseCluster: local Namespace: vela-systemType: helmHealthy Fetch repository successfully, Create helm release successfullyNo trait applied
Kruise is running successfully! Then you can use kruise in your cluster. If you need to set up a new environment, the only thing you need to do is to apply the files like above.
Some Kubernetes native resources like ConfigMap/PVC are commonly used in the environment.
If you want to apply those resources before deploying your application, you can add an initialization workflow to your application.
KubeVela provides a built-in workflow step apply-object to fill in native Kubernetes resources. In this way, by filling in Kubernetes native resources, we can avoid writing redundant component definitions.
Apply the following application, it will initialize an environment with ConfigMap/PVC. There is two components in this application, the first one will write data to PVC, the second on will read the data from PVC:
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: server-with-pvc-and-cmnamespace: defaultspec:components:- name: log-gen-workertype: workerproperties:image: busyboxcmd:- /bin/sh- -c- >i=0;while true;doecho "$i: $(date)" >> /test-pvc/date.log;i=$((i+1));sleep 1;donevolumes:- name: "my-pvc"type: "pvc"mountPath: "/test-pvc"claimName: "my-claim"- name: "my-configmap"type: "configMap"mountPath: "/test-cm"cmName: "my-cm"items:- key: test-keypath: test-key- name: log-read-workertype: workerproperties:name: count-logimage: busyboxcmd:- /bin/sh- -c- 'tail -n+1 -f /test-pvc/date.log'volumes:- name: "my-pvc"type: "pvc"mountPath: "/test-pvc"claimName: "my-claim"- name: "my-configmap"type: "configMap"mountPath: "/test-cm"cmName: "my-cm"items:- key: test-keypath: test-keypolicies:- name: my-policyproperties:clusters:- localtype: topology- name: apply-oncetype: apply-onceproperties:enable: trueworkflow:steps:- name: apply-pvctype: apply-objectproperties:value:apiVersion: v1kind: PersistentVolumeClaimmetadata:name: my-claimnamespace: defaultspec:accessModes:- ReadWriteOnceresources:requests:storage: 8GistorageClassName: standard- name: apply-cmtype: apply-objectproperties:value:apiVersion: v1kind: ConfigMapmetadata:name: my-cmnamespace: defaultdata:test-key: test-value- name: deploy-compproperties:policies:- my-policytype: deploy
Check the PVC and ConfigMap in cluster:
vela status server-with-pvc-and-cm --detail --tree
Expected Outcome
CLUSTER NAMESPACE RESOURCE STATUS APPLY_TIME DETAILlocal ─── default ─┬─ ConfigMap/my-cm updated 2022-10-31 18:00:52 Data: 1 Age: 57s├─ PersistentVolumeClaim/my-claim updated 2022-10-31 10:00:52 Status: Bound Volume: pvc-b6f88ada-af98-468d-8cdd-31ca110c5e1a Capacity: 8Gi Access Modes: RWO StorageClass: standard Age: 57s├─ Deployment/log-gen-worker updated 2022-10-31 10:00:52 Ready: 1/1 Up-to-date: 1 Available: 1 Age: 57s└─ Deployment/log-read-worker updated 2022-10-31 10:00:52 Ready: 1/1 Up-to-date: 1 Available: 1 Age: 57s
Check the application in cluster:
vela status server-with-pvc-and-cm
Expected Outcome
$ vela status server-with-pvc-and-cmAbout:Name: server-with-pvc-and-cmNamespace: defaultCreated at: 2022-10-31 18:00:51 +0800 CSTStatus: runningWorkflow:mode: StepByStep-DAGfinished: trueSuspend: falseTerminated: falseSteps- id: xboizfjo28name: apply-pvctype: apply-objectphase: succeeded- id: 4ngx25mrx8name: apply-cmtype: apply-objectphase: succeeded- id: 1gzzt3mfw1name: deploy-comptype: deployphase: succeededServices:- Name: log-gen-workerCluster: local Namespace: defaultType: workerHealthy Ready:1/1No trait applied- Name: log-read-workerCluster: local Namespace: defaultType: workerHealthy Ready:1/1No trait applied
Check the logs of the second component:
vela logs server-with-pvc-and-cm --component log-read-worker
Expected Outcome
+ log-read-worker-7f4bc9d9b5-kb5l6 › log-read-workerlog-read-worker 2022-10-31T10:01:15.606903716Z 0: Mon Oct 31 10:01:13 UTC 2022log-read-worker 2022-10-31T10:01:15.606939383Z 1: Mon Oct 31 10:01:14 UTC 2022log-read-worker 2022-10-31T10:01:15.606941883Z 2: Mon Oct 31 10:01:15 UTC 2022log-read-worker 2022-10-31T10:01:16.607006425Z 3: Mon Oct 31 10:01:16 UTC 2022log-read-worker 2022-10-31T10:01:17.607184925Z 4: Mon Oct 31 10:01:17 UTC 2022log-read-worker 2022-10-31T10:01:18.607304426Z 5: Mon Oct 31 10:01:18 UTC 2022...
We can see that both components is running. The two components share the same PVC and use the same ConfigMap.
As we have already modeled the environment as a KubeVela Application, we can destroy the environment easily by deleting the application.
vela delete server-with-pvc-and-cm
Then the KubeVela controller will clean up all these resources.
Last updated on Feb 9, 2023 by dependabot[bot]