当前位置:网站首页>First use of kubernetes cronjob
First use of kubernetes cronjob
2022-06-23 17:41:00 【I have nothing to do with you】
background
Although it has been used for several years kubernetes Yes . But the types of service applications are generally deployments statefuset daemonset Several types , as for job cronjob I haven't used it much . Now there's just one php The service of the application needs to be executed every five minutes , Just can go to know a CronJob Use !
First, review it kubernetes Of workloads
reference :https://kubernetes.io/zh/docs/concepts/workloads/
- Deployment and ReplicaSet ( Replace the original resource ReplicationController). Deployment Ideal for managing stateless applications on your cluster ,Deployment All in Pod They are all equivalent to each other , And be replaced when needed .
- StatefulSet Allows you to run one or more of the Pods. for example , If your load will persist the data , You can run a StatefulSet, Each one Pod With someone PersistentVolume correspond . you are here StatefulSet In each Pod Code running in can copy data to the same StatefulSet Others in Pod In order to improve the overall service reliability .
- DaemonSet Define... That provides local support facilities for nodes Pods. these Pods Maybe the operation and maintenance of your cluster is Very important , For example, as an auxiliary tool for network links or as a network plug-in unit And so on . Every time you add a new node to the cluster , If the node is associated with DaemonSet Protocol matching of , Then the control surface will be the DaemonSet Dispatch a Pod Run on the new node .
- Job and CronJob. Define tasks that run to the end and stop .Job It's a one-time task , and CronJob It will run repeatedly according to its time plan .
- Third party workload resources , adopt Custom resource definition (CRD) Add third-party workload resources
Pods
Of course, the above workloads are ultimately managed by Pod, therefore Pod Where should I put it ?Pod Yes, you can. Kubernetes Created and managed in 、 The smallest deployable cell .Pod Similar to a set of shared namespace and file system volumes Docker Containers .
reference :https://kubernetes.io/zh/docs/concepts/workloads/pods/
Cronjob For the first time
Define the needs
1. Mirror image php7.4 above Need to install gd mysql rely on
2. Run every five minutes
Basic image construction
Because of me php Mirrors are nginx Packed together . And this application is a pure php application . I decided to rebuild an image , reference dockerhub:https://registry.hub.docker.com/_/php
Dockerfile
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd pdo_mysql mysqliTo overcome inertia , The working environment is switched to linux If the screenshot is not convenient, the figure above will not be displayed . The image warehouse uses the personal version of Tencent cloud tcr( It seems that they were merged some time ago , It used to be a personal warehouse )
Build a mirror image , And push the basic image to the basic warehouse :
docker build -t ccr.ccs.tencentyun.com/laya-master/php:7.4-fpm . docker push ccr.ccs.tencentyun.com/laya-master/php:7.4-fpm
jenkins pipeline Pipeline construction
Pipeline reference :https://duiniwukenaihe.blog.csdn.net/article/details/116661391
reference :stage Single choice multiple choice problem
pipeline do when Judge
when {
environment name: 'XXXX', value: 'true'
}Add... Under the sub project folder Dockerfile
FROM ccr.ccs.tencentyun.com/xxxx/php:7.4-fpm ADD html /var/www/html WORKDIR /var/www/html[
notes : Because this is git A new sub file directory is added under the project. If it is not a new project, it will not be written completely pipeline Just add clips !
1. Build and upload images to the image warehouse
stage('docker build worldmap-job') {
agent { label "build" }
when {
environment name: 'worldmap-job', value: 'true'
}
steps {
sh " cd worldmap-job&&docker build -t ccr.ccs.tencentyun.com/xxxx/worldmap-job:$data ."
withCredentials([usernamePassword(credentialsId: 'xxxx', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUser')]) {
sh "docker login -u ${dockerUser} -p ${dockerPassword} ccr.ccs.tencentyun.com"
sh "docker push ccr.ccs.tencentyun.com/xxxx/worldmap-job:$data"
}
}
} 2. Deploy the application
stage("develop worldmap-job") {
when {
environment name: 'worldmap-job', value: 'true'
}
steps {
sh "sed -e 's/{data}/$data/g' /home/jenkins/workspace/yaml/develop/worldmap-job.tpl > /home/jenkins/workspace/yaml/develop/worldmap-job.yaml"
sh "sed -e 's/{data}/$data/g' /home/jenkins/workspace/yaml/develop/worldmap-job.tpl > /home/jenkins/workspace/yaml/develop/worldmap-job.yaml"
sh "sudo kubectl apply -f /home/jenkins/workspace/yaml/develop/worldmap-job.yaml --namespace=develop"
sh "sudo kubectl apply -f /home/jenkins/workspace/yaml/develop/worldmap-job.yaml --namespace=develop"
}
}Note that the format here may be different from that of ordinary users , Because the deployment here uses parallelism parallel . reference :https://duiniwukenaihe.blog.csdn.net/article/details/116661391
tpl Template file
worldmap-job.tpl
apiVersion: batch/v1
kind: CronJob
metadata:
name: worldmap-job
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: worldmap-job
image: ccr.ccs.tencentyun.com/xxxx/worldmap-job:{data}
imagePullPolicy: IfNotPresent
args:
- /usr/local/bin/php
- /var/www/html/DrawLandsMap.php
- run_job
- "6"
envFrom:
- configMapRef:
name: deploy
env:
- name: PHP_MEM_LIMIT
value: "256M"
resources:
requests:
memory: "256M"
cpu: "250m"
limits:
memory: "1024M"
cpu: "2000m"
imagePullSecrets:
- name: tencent
restartPolicy: OnFailureThere are several points to pay attention to :
schedule schedule: "/5 * * *" ###5 Once per minute
imagePullPolicy imagePullPolicy: IfNotPresent ###imagePullPolicy Self judgment seems to be three necessities , I copy deployments The configuration of the has been changed. At the beginning, it seems that there is a mistake
args Executing scripts is still Numbers should be emphasized with quotation marks
envFrom I quote configmap The variables in the
env Limit php limit
sesources Made resource restrictions
imagePullSecrets Mirror warehouse secret
restartPolicy Container restart strategy
now jenkins Trigger a build . land kubernetes Cluster validation :
[[email protected] develop]# kubectl get cronjob -n develop NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE worldmap-job */5 * * * * False 0 2m36s 28h [[email protected] develop]# kubectl get pods -n develop|grep world worldmap-job-27357635-274hp 0/1 Completed 0 12m worldmap-job-27357640-wb459 0/1 Completed 0 7m53s worldmap-job-27357645-cndb4 0/1 Completed 0 2m53s [[email protected] develop]# kubectl get jobs --watch -n develop NAME COMPLETIONS DURATION AGE worldmap-job-27357635 1/1 11s 13m worldmap-job-27357640 1/1 11s 8m43s worldmap-job-27357645 1/1 12s 3m43s
It seems like this , Only the latest reality 3 individual job Of course, those who are interested can study how to change the number of this ?(successfulJobsHistoryLimit: 3) k It can be modified . Checked it again pod Logs are normal . also job5 The minute trigger time is 0 5 10 In this order
Some of my own thoughts :
- cronjob Resource restriction
- cronjob You can also mount configmap
- Task type applications can try to apply job or cronjob
- The image still needs to be rebuilt , Different applications . This reduces the size of the image , Reduce vulnerability possibilities
边栏推荐
- Digital twin excavator of Tupu software realizes remote control
- Google Play Academy 组队 PK 赛,火热进行中!
- QT layout manager [qvboxlayout, qhboxlayout, qgridlayout]
- What does the timestamp 90K mean?
- 浅谈5类过零检测电路
- Date to localdatetime
- [30. concatenate substrings of all words]
- How important is 5g dual card dual access?
- Innovative technology leader! Huawei cloud gaussdb won the 2022 authoritative award in the field of cloud native database
- 使用Jmeter进行性能测试及性能监控平台搭建
猜你喜欢

ctfshow php的特性

Easyplayer mobile terminal plays webrtc protocol for a long time. Pressing the play page cannot close the "about us" page

Another breakthrough! Alibaba cloud enters the Gartner cloud AI developer service Challenger quadrant

qYKVEtqdDg

The company recruited a tester with five years' experience and saw the real test ceiling

Network remote access raspberry pie (VNC viewer)

Database Experiment 2 query

Jetpack compose and material you FAQs

官方零基础入门 Jetpack Compose 的中文课程来啦!

Intel arc A380 graphics card message summary: the entry-level price products of running point and bright driving need to be optimized
随机推荐
[network communication -- webrtc] analysis of webrtc source code -- supplement of pacingcontroller related knowledge points
Date selection of hotel check-in time and check-out time
Google Play Academy 组队 PK 赛,火热进行中!
《MPLS和VP体系结构》
华为手机通过adb安装APK提示“签名不一致,该应用可能已被修改”
浅谈5类过零检测电路
EasyPlayer移动端播放webrtc协议时长按播放页面无法关闭“关于我们”页面
MySQL的 安装、配置、卸载
I successfully joined the company with 27K ByteDance. This interview notes on software testing has benefited me for life
如何设计一个秒杀系统?
JSON - learning notes (message converter, etc.)
Practice sharing of chaos engineering in stability management of cloud native Middleware
Talk about the difference between redis cache penetration and cache breakdown, and the avalanche effect caused by them
bypassuac提权
使用Jmeter进行性能测试及性能监控平台搭建
浅析3种电池容量监测方案
Self supervised learning (SSL)
如何通过线上股票开户?在线开户安全么?
How important is 5g dual card dual access?
B. Integers Shop-Hello 2022