Cloud-Native Devops Best Practices(2) - GitOps + OpenKruise CloneSet
What is GitOps?
GitOps is an approach to continuous delivery. Its core idea is to store the declarative infrastructure and applications of an application in a Git repository.
With Git at the core of the delivery pipeline, every developer can submit Pull Requests and use Git to accelerate and simplify application deployment and maintenance tasks for Kubernetes. By using a simple tool like Git, developers can more efficiently focus on creating new features rather than operations-related tasks (e.g., application installation, configuration, migration, etc.).

GitOps + OpenKruise CloneSet Practice
Requirements
- Install Kubernetes Cluster, Since v1.0.0 (alpha/beta), OpenKruise requires Kubernetes version >= 1.16.
- Install Tekton, Reference Official Documents, Tekton is a Google open source Kubernetes native framework for creating continuous integration and continuous deployment/delivery (CI/CD) systems.
- Install Argo-cd, Reference Official Documents, Argo-cd is a declarative GitOps continuous delivery tool for Kubernetes.
Install OpenKruise(Enable: TemplateNoDefaults)
Openkruise installed by default will inject the default value of pod / PVC template, which will conflict with the sync judgment logic of Argo CD. Therefore, when installing openkruise, you need to open gates TemplateNoDefaults, as follows:
# Firstly add openkruise charts repository if you haven't do this.$ helm repo add openkruise https://openkruise.github.io/charts/# [Optional]$ helm repo update# Install the latest version.$ helm install kruise openkruise/kruise --set featureGates="TemplateNoDefaults=true"# Those that have been installed need to be upgraded$ helm upgrade kruise openkruise/kruise --set featureGates="TemplateNoDefaults=true"
CloneSet Deploy Stateless Application
CloneSet is the ability provided by OpenKruise to efficiently manage stateless applications, it is similar to the official workload: Deployment, but offers many enhancements such as InPlace Update, Batch Release, etc. Please refer to the documentation CloneSet. This article provides a helloworld http service demo, It contains Helm Charts, and the cloneSet configuration is shown below:
apiVersion: apps.kruise.io/v1alpha1kind: CloneSetmetadata:name: helloworld-serverlabels:app: helloworld-serverspec:updateStrategy:# CloneSet will try to in-place update Pod instead of recreating them if possibletype: InPlaceIfPossible# Batch release, currently updating only one Podpartition: 1replicas: 2selector:matchLabels:app: helloworld-servertemplate:metadata:labels:app: helloworld-serverspec:containers:- name: helloworldimage: "openkruise/kruise:hello_world-d92ae174b"
Argo-cd CloneSet Health Check
Configure CloneSet Argo-cd Custom CRD Health Checks, With this configuration argo-cd is able to perform a healthy check of the CloneSet, such as whether the CloneSet is published and whether the Pods are ready, as follows:
apiVersion: v1kind: ConfigMapmetadata:labels:app.kubernetes.io/name: argocd-cmapp.kubernetes.io/part-of: argocdname: argocd-cmnamespace: argocddata:resource.customizations.health.apps.kruise.io_CloneSet: |hs = {}-- if pausedif obj.spec.updateStrategy.paused thenhs.status = "Suspended"hs.message = "CloneSet is Suspended"return hsend-- check cloneSet statusif obj.status ~= nil thenif obj.status.observedGeneration < obj.metadata.generation thenhs.status = "Progressing"hs.message = "Waiting for rollout to finish: observed cloneSet generation less then desired generation"return hsendif obj.status.updatedReplicas < obj.spec.replicas thenhs.status = "Progressing"hs.message = "Waiting for rollout to finish: replicas hasn't finished updating..."return hsendif obj.status.updatedReadyReplicas < obj.status.updatedReplicas thenhs.status = "Progressing"hs.message = "Waiting for rollout to finish: replicas hasn't finished updating..."return hsendhs.status = "Healthy"return hsend-- if status == nilhs.status = "Progressing"hs.message = "Waiting for cloneSet"return hs
OpenKruise internal CRD resources in addition to CloneSet, others such as: Advanced StatefulSet, SidecarSet, etc. can be similar to the above way to achieve Custom Resource Health.
Tekton Pipeline + Argo-cd
Argo-CD together with Tekton Pipeline is a popular DevOps practice and integrate well with CI process. Such practice requires storing the Argo-cd admin secret in K8S Secret CRD (method of obtaining secret), which in turn can be used in Tekton Pipeline, as follows:
apiVersion: v1data:# argo-cd admin secretusername: xxxxxpassword: xxxxxserver: xxxxxkind: Secretmetadata:name: argosecret---apiVersion: tekton.dev/v1beta1kind: Taskmetadata:labels:app: helloworldname: helloworld-argocdspec:params:- name: gitrepositoryurltype: string- name: branchtype: string- name: short_shatype: string- name: docker_repotype: string- name: app_nametype: string- name: app_nstype: string- name: k8s_servertype: stringsteps:- name: argocd-deployimage: argoproj/argocd:latestcommand:- shargs:- '-ce'- >set -eecho "upgrade app $(params.app_name)"; username=`cat /var/secret/username`; password=`cat /var/secret/password`; server=`cat /var/secret/server`;argocd login ${server} --insecure --username ${username} --password ${password}argocd app create $(params.app_name) --upsert --repo $(params.gitrepositoryurl) --path $(params.app_name)/charts --dest-namespace $(params.app_ns) --dest-server $(params.k8s_server) --revision $(params.branch) --helm-set image.repository=$(params.docker_repo) --helm-set image.tag=$(params.branch)-$(params.short_sha) --helm-set installation.namespace=$(params.app_ns)argocd app list; argocd app sync $(params.app_name)argocd app wait $(params.app_name) --healthvolumeMounts:- name: argocd-secretmountPath: "/var/secret"volumes:- name: argocd-secretsecret:secretName: argosecret---apiVersion: tekton.dev/v1beta1kind: Pipelinemetadata:name: helloworld-pipelinespec:params:- name: gitrepositoryurltype: string- name: branchtype: string- name: short_shatype: string- name: docker_repotype: string- name: app_nametype: string- name: app_nstype: string- name: k8s_servertype: string# Here you can connect with CI process to realize CI/CD Pipelinetasks:- name: helloworld-argocdtaskRef:name: helloworld-argocdparams:- name: gitrepositoryurlvalue: $(params.gitrepositoryurl)- name: short_shavalue: $(params.short_sha)- name: branchvalue: $(params.branch)- name: docker_repovalue: $(params.docker_repo)- name: app_namevalue: $(params.app_name)- name: app_nsvalue: $(params.app_ns)- name: k8s_servervalue: $(params.k8s_server)
Run Tekton Pipeline
Configure PipelineRun CRD, and kubectl apply -f in k8s cluster to run Pipeline, as follows:
apiVersion: tekton.dev/v1beta1kind: PipelineRunmetadata:name: helloworld-pipeline-run-1spec:pipelineRef:name: helloworld-pipelineparams:- name: gitrepositoryurlvalue: https://github.com/zmberg/samples.git- name: branchvalue: hello_world- name: short_shavalue: d92ae174b- name: docker_repovalue: zhaomingshan/kruise- name: app_namevalue: helloworld- name: app_nsvalue: helloworld- name: k8s_servervalue: https://kubernetes.default.svc
The results can be viewed via the argo-cd cli, as follows:

Summary
OpenKruise is more of a Kubernetes level extended capability, such as in-place upgrade, preImageDownload, etc. So many community users using OpenKruise in production environments have some additional costs, need to integrate or self-research container PaaS. The main purpose of this article is to combine some of the best Paas solutions in the community with OpenKruise, so that as many people as possible get to enjoy the dividends of cloud-native at a lower cost.
Argo-cd is a very good product for the community, and it integrate well with OpenKruise’s CRD resources, we hope that this article can intrigue more ideas from the community about “How to use OpenKruise easily”. We will try to integrate OpenKruise with other CI/CD pipelines later to better implement DevOps practice.
