Cloud-Native Devops Best Practices(1) - Continuous Integration (CI) + OpenKruise Image Pre-download
- Continuous Integration(CI) is a hands-on way to bring integration forward to the early stages of the development cycle, allowing builds, tests and integration of code to happen more often and repeatedly.
- is provided by OpenKruise to pull application images to specific Node nodes in advance of application deployment, which in turn can greatly improve the efficiency of application deployment.
Architecture
- Long-term pre-download common sidecar images, base images, such as: istio envoy, log collection containers.
- In large-scale scenarios, pre-download business app images to a specific K8s Node to reduce the pressure on the image repository during deployment, mainly for Deployment, StatefulSet and other k8s native resources.
- OpenKruise CloneSet & Advanced StatefulSet InPlace Update with built-in image pre-download capability, refer to CloneSet documentation.
Note: The OpenKruise image pre-download capability is only available for regular kubelet nodes, and not for virtual kubelet.
Requirements
- Install Kubernetes Cluster, Since v1.0.0 (alpha/beta), OpenKruise requires Kubernetes version >= 1.16.
- Install Tekton, Reference 。 Tekton is a Google open source Kubernetes native framework for creating continuous integration and continuous deployment/delivery (CI/CD) systems.
- Helm installation of OpenKruise, Since v0.9.0, Reference Install OpenKruise。
1. Git Repo: This article provides a helloworld http service demo, It contains Code, Dockerfile, and Unit Test, as follows:
Image Pre-download
Kruise CloneSet & Advanced StatefulSet InPlace Update Built-in Image Pre-download
Note: This scenario no longer requires to deploy ImagePullJob CRD
If you have enabled the feature-gate during , CloneSet & Advanced StatefulSet controller will automatically pre-download the image you want to update to the nodes of all old Pods. It is quite useful to accelerate the progress of applications upgrade.
# 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="PreDownloadImageForInPlaceUpdate=true"
# Those that have been installed need to be upgraded
$ helm upgrade kruise openkruise/kruise --set featureGates="PreDownloadImageForInPlaceUpdate=true"
The parallelism of each new image pre-downloading by CloneSet & Advanced StatefulSet is 1
, which means the image is downloaded on nodes one by one. You can change the parallelism using the annotation on CloneSet according to the capability of image registry, for registries with more bandwidth and P2P image downloading ability, a larger parallelism can speed up the pre-download process.
Kubernetes Native Workload, e.g. Deployment, StatefulSet, DaemonSet, Job etc.
1. Configure ImagePullJob CRD in k8s configmap, as follows:
apiVersion: v1
kind: ConfigMap
name: imagePullJob
data:
imagepulljob.yaml: |
apiVersion: apps.kruise.io/v1alpha1
kind: ImagePullJob
metadata:
name: APP_NAME
spec:
# pre-download image
image: APP_IMAGE
parallelism: 10
# You can write the names or label selector in the selector field to assign Nodes (only one of them can be set).
# If no selector is set, the image will be pulled on all Nodes in the cluster.
names:
- node-1
- node-2
matchLabels:
node-type: xxx
completionPolicy:
type: Always
activeDeadlineSeconds: 1200
ttlSecondsAfterFinished: 300
pullPolicy:
backoffLimit: 3
timeoutSeconds: 300
1. configure tekton pileline, first executing the Build-Test-DockerPush Task, and second Image Pre-download Task, as follows:
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: helloworld-pipeline
spec:
params:
- name: gitrepositoryurl
type: string
type: string
- name: short_sha
type: string
- name: docker_repo
type: string
- name: app_name
type: string
- name: helloworld-build-push
taskRef:
name: helloworld-build-push
params:
- name: gitrepositoryurl
value: $(params.gitrepositoryurl)
- name: short_sha
value: $(params.short_sha)
- name: branch
value: $(params.branch)
- name: docker_repo
value: $(params.docker_repo)
- name: app_name
value: $(params.app_name)
- name: helloworld-image-predownload
taskRef:
name: helloworld-image-predownload
params:
- name: short_sha
value: $(params.short_sha)
- name: branch
value: $(params.branch)
- name: docker_repo
value: $(params.docker_repo)
- name: app_name
value: $(params.app_name)
- helloworld-build-push
2. Configure PipelineRun CRD, and kubectl apply -f in k8s cluster to run Pipeline, as follows:
3. The execution results can be viewed via the tekton command line tool tkn, as follows: