从零开始Kubernetes CronJob实现任务调度
安装 kubectl
下载
获取最新版本
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
指定版本
curl -LO https://dl.k8s.io/release/v1.23.0/bin/linux/amd64/kubectl
二进制包版本验证
[root@ydt local]# curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 154 100 154 0 0 152 0 0:00:01 0:00:01 --:--:-- 152
100 64 100 64 0 0 33 0 0:00:01 0:00:01 --:--:-- 75
[root@ydt local]# echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
kubectl: 确定
一般来说要么指定版本,要么取最新的版本校验文档,都不会有问题
安装及查看版本
[root@ydt local]# sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
[root@ydt local]# kubectl version --client --output=yaml
clientVersion:
buildDate: "2022-04-14T08:49:13Z"
compiler: gc
gitCommit: ad3338546da947756e8a88aa6822e9c11e7eac22
gitTreeState: clean
gitVersion: v1.23.6
goVersion: go1.17.9
major: "1"
minor: "23"
platform: linux/amd64
安装minikube
Minikube是一个快速搭建单节点Kubenetes集群的工具,它对硬件资源没有太高的要求,方便开发人员学习使用,或者进行日常的开发。
#安装须知:
2 CPUs or more
2GB of free memory
20GB of free disk space
Internet connection
Container or virtual machine manager, such as: Docker, Hyperkit, Hyper-V, KVM, Parallels, Podman, VirtualBox, or VMware Fusion/Workstation #一般都是Docker容器
#安装命令
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-latest.x86_64.rpm
sudo rpm -Uvh minikube-latest.x86_64.rpm
#启动Docker
service docker start
# 创建用户,如果你使用的是root用户的话会无法启动并提示如下信息,那是因为Minikube不允许使用root权限启动,需要创建一个非root账号再启动
useradd -u 1024 -g docker macro
# 设置用户密码,这个密码需要复杂点,比如aaa@123
passwd macro
# 切换用户
su macro
#启动集群
minikube start --driver=docker
#遇到的报错:
#1、Sorry, Kubernetes 1.23.3 requires conntrack to be installed in root’s path
#安装conntrack解决,安装不了换个yum源,比如阿里云
yum install conntrack
#2、/proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
#执行:echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
#3、/proc/sys/net/ipv4/ip_forward contents are not set to 1
#执行:echo 1 > /proc/sys/net/ipv4/ip_forward
#4、hostname "***" could not be reached
#将 127.0.0.1 映射本机的hostname
#看到如下启动结果,表示启动成功
[macro@ydt local]$ minikube start --driver=docker
* Centos 7.7.1908 上的 minikube v1.25.2
* 根据现有的配置文件使用 docker 驱动程序
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
* Updating the running docker "minikube" container ...
! This container is having trouble accessing https://k8s.gcr.io
* To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
* 正在 Docker 20.10.12 中准备 Kubernetes v1.23.3…
- kubelet.housekeeping-interval=5m
* Verifying Kubernetes components...
- Using image gcr.io/k8s-minikube/storage-provisioner:v5
* Enabled addons: storage-provisioner, default-storageclass
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
#查看集群节点(单节点集群)
[macro@ydt local]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 90m v1.23.3
CronJob任务
架构图
配置任务实例
这里先来一个demo,一分钟打印一句话:hello cron-job
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-test
spec:
schedule: "*/1 * * * *" #表明每一分钟都会执行这个任务
jobTemplate: #job控制器模板,用于为cronjob控制器生成job对象,下面其实就是job的定义
spec:
template: #模板,当副本数量不足时,会根据下面的模板创建pod副本
spec:
containers:
- name: busybox-container
image: busybox
args:
- /bin/sh
- -c
- date; echo "hello cron-job"
restartPolicy: OnFailure #重启策略只能设置为Never或者OnFailure
创建任务实例
[macro@ydt local]$ kubectl create -f cronjob.yaml
Warning: batch/v1beta1 CronJob is deprecated in v1.21+, unavailable in v1.25+; use batch/v1 CronJob
cronjob.batch/cronjob-test created
查看执行结果
[macro@ydt local]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
cronjob-test-27516061-k4vwv 0/1 Completed 0 2m10s
cronjob-test-27516062-xlpbj 0/1 Completed 0 70s
cronjob-test-27516063-n9pc9 0/1 Completed 0 10s
[macro@ydt local]$ kubectl logs cronjob-test-27516061-k4vwv
Tue Apr 26 09:01:06 UTC 2022
hello cron-job
[macro@ydt root]$ kubectl get cj -n default -w
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob-test */1 * * * * False 0 52s 2m34s
cronjob-test */1 * * * * False 1 0s 2m42s
cronjob-test */1 * * * * False 0 10s 2m52s
cronjob-test */1 * * * * False 0 10s 2m52s
cronjob-test */1 * * * * False 1 0s 3m42s
cronjob-test */1 * * * * False 0 9s 3m51s
cronjob-test */1 * * * * False 0 9s 3m51s
cronjob-test */1 * * * * False 1 0s 4m42s
cronjob-test */1 * * * * False 0 8s 4m50s
cronjob-test */1 * * * * False 0 8s 4m50s
发现每隔一分钟,cronjob就会生成一个pod容器执行job
如果pod建立失败,可以查看具体原因:
kubectl describe pod cronjob-test-27517357-qfznq
删除任务实例
[macro@ydt local]$ kubectl delete cronjob cronjob-test
cronjob.batch "cronjob-test" deleted
Kubernetes可视化插件
#开启插件
[macro@ydt local]$ minikube addons enable dashboard
- Using image kubernetesui/dashboard:v2.3.1
- Using image kubernetesui/metrics-scraper:v1.0.7
* Some dashboard features require the metrics-server addon. To enable all features please run:
minikube addons enable metrics-server
* 启动 'dashboard' 插件
#暴露外部访问ip
[macro@ydt local]$ kubectl proxy --port=8100 --address=192.168.137.128 --accept-hosts='^.*' &
[1] 343326
[macro@ydt local]$ Starting to serve on 192.168.137.128:8100
#获取访问路径
[macro@ydt local]$ minikube dashboard --url
* 正在验证 dashboard 运行情况 ...
* Launching proxy ...
* 正在验证 proxy 运行状况 ...
http://127.0.0.1:36290/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
注意,每次minikube单节点集群重启后都需要重新执行,得到新的访问路径!
接口执行
可以通过Shell脚本执行远程服务接口
apiVersion: batch/v1beta1
kind: CronJob
metadata:
namespace: kube-system #不要跟服务同一个namespace,否则successfulJobsHistoryLimit,failedJobsHistoryLimit不生效
name: cronjob-test
spec:
schedule: "0 0/1 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: busybox-container
image: busybox
command:
- wget
- "http://服务名.服务所在namespace/infoVisitLog"
restartPolicy: OnFailure
successfulJobsHistoryLimit: 2
failedJobsHistoryLimit: 2