一起来学k8s 29.二进制k8s集群cronjob

二进制k8s集群cronjob

环境准备

##/etc/hosts
192.168.48.101 master01
192.168.48.102 master02
192.168.48.103 master03
192.168.48.201 node01
192.168.48.202 node02
192.168.48.54  nfs
## keepalived的vip
192.168.48.66
IPHostnameCPUMemory
192.168.48.101master0124G
192.168.48.102master0224G
192.168.48.103master0324G
192.168.48.201node0124G
192.168.48.202node0224G
192.168.48.54nfs24G
软件版本
kubernetes1.15.2
docker-ce19.03
calico3.8
etcd3.3.13
CNI0.8.1
coredns1.4.0
metrics-server0.3.3
ingress-controller0.25.0
dashboard1.10.1
Weave Scope1.11.4
nfsv4
helm2.14.3
harbor1.1.1

cronjob

制作一个java环境镜像

获取官方基础镜像centos7.6
[root@master01 ~]# docker pull centos:centos7.6.1810
准备jdk8,maven
[root@master01 ~]# mkdir /root/java
[root@master01 ~]# cd /root/java/
[root@master01 java]# ll
total 199448
-rw-r--r-- 1 root root   9136463 Jun 16 12:47 apache-maven-3.6.1-bin.tar.gz
-rw-r--r-- 1 root root 195094741 Aug 15 13:30 jdk-8u221-linux-x64.tar.gz
制作基础镜像
[root@master01 java]# pwd
/root/java
[root@master01 java]# ll
total 199448
-rw-r--r-- 1 root root   9136463 Jun 16 12:47 apache-maven-3.6.1-bin.tar.gz
-rw-r--r-- 1 root root 195094741 Aug 15 13:30 jdk-8u221-linux-x64.tar.gz
[root@master01 java]# vim Dockerfile

#CentOS with JDK8

#build a new image with basic centos7.6.1810
FROM centos:centos7.6.1810

#author
MAINTAINER tk8s

#create directory
RUN mkdir /usr/local/java

#copy archive to the image
ADD jdk-8u221-linux-x64.tar.gz /usr/local/java/
ADD apache-maven-3.6.1-bin.tar.gz /usr/local/java/

##make a symbol link
RUN ln -s /usr/local/java/jdk1.8.0_221  /usr/local/java/jdk 
RUN ln -s /usr/local/java/apache-maven-3.6.1  /usr/local/java/maven

##set ENV
ENV JAVA_HOME /usr/local/java/jdk
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
ENV MAVEN_HOME /usr/local/java/maven
ENV PATH $JAVA_HOME/bin:$JRE_HOME/bin:$MAVEN_HOME/bin:$PATH
[root@master01 java]# docker build -t registry.tk8s.com/library/centos7:jdk8_221 ./
[root@master01 java]# docker run -i -t --name=test registry.tk8s.com/library/centos7:jdk8_221  /bin/sh
sh-4.2# mvn -v
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-04T19:00:29Z)
Maven home: /usr/local/java/maven
Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /usr/local/java/jdk1.8.0_221/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "5.2.2-1.el7.elrepo.x86_64", arch: "amd64", family: "unix"
sh-4.2# java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
sh-4.2# echo $PATH
/usr/local/java/jdk/bin:/usr/local/java/jdk/jre/bin:/usr/local/java/maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

上传到harbor
[root@master01 java]# docker login registry.tk8s.com
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

[root@master01 java]# docker push registry.tk8s.com/library/centos7:jdk8_221
The push refers to repository [registry.tk8s.com/library/cent0s7]
c480f8fabd63: Pushed 
1bb63a8e4cb9: Pushed 
5380e90c66da: Pushed 
f2ee9360cf6b: Pushed 
bac1ef4bd29b: Pushed 
89169d87dbe2: Pushed 
jdk8_221: digest: sha256:8c5d7097c04718376a11a089d861f493fc2b8f37d29ee46a5b3abbae2844129b size: 1574

下载k8s-demo

[root@master01 ~]# git clone https://github.com/tangwei0928/k8s-demo.git
[root@master01 ~]# cd k8s-demo/
[root@master01 k8s-demo]# ll
total 12
drwxr-xr-x 2 root root   87 Aug 15 14:33 configs
drwxr-xr-x 3 root root   50 Aug 15 14:33 cronjob-demo
drwxr-xr-x 3 root root   50 Aug 15 14:33 dubbo-demo
drwxr-xr-x 3 root root   32 Aug 15 14:33 dubbo-demo-api
-rw-r--r-- 1 root root 5201 Aug 15 14:33 pom.xml
-rw-r--r-- 1 root root   62 Aug 15 14:33 README.md
drwxr-xr-x 3 root root   50 Aug 15 14:33 springboot-web-demo
drwxr-xr-x 4 root root   69 Aug 15 14:33 web-demo
[root@master01 k8s-demo]# cd /root/k8s-demo/cronjob-demo/
[root@master01 cronjob-demo]# ll
total 8
-rw-r--r-- 1 root root 190 Aug 15 14:33 Dockerfile
-rw-r--r-- 1 root root 425 Aug 15 14:33 pom.xml
drwxr-xr-x 3 root root  18 Aug 15 14:33 src

制作cronjob镜像

vim Dockerfile

FROM registry.tk8s.com/library/centos7:jdk8_221 as BUILD

COPY src /usr/app/src
COPY pom.xml /usr/app

RUN mvn -f /usr/app/pom.xml clean package -Dmaven.test.skip=true

FROM registry.tk8s.com/library/centos7:jdk8_221

MAINTAINER tk8s

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
ENV TZ=Asia/Shanghai

RUN mkdir /app

WORKDIR /app

COPY --from=BUILD /usr/app/target/cronjob-demo-1.0-SNAPSHOT.jar   /app/cronjob-demo.jar

ENTRYPOINT ["java", "-cp", "/app/cronjob-demo.jar", "com.mooc.demo.cronjob.Main"]

[root@master01 cronjob-demo]# docker build -t registry.tk8s.com/library/cronjob:demo ./
[root@master01 cronjob-demo]# docker run -it  registry.tk8s.com/library/cronjob:demo
I will working for 25 seconds!
All work is done! Bye!
[root@master01 cronjob-demo]# docker push registry.tk8s.com/library/cronjob:demo

编写cronjob yaml

vim cronjob-demo.yaml

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cronjob-demo
spec:
  schedule: "*/1 * * * *"
  successfulJobsHistoryLimit: 3
  suspend: false
  concurrencyPolicy: Forbid
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: cronjob-demo
        spec:
          restartPolicy: Never
          containers:
          - name: cronjob-demo
            image: registry.tk8s.com/library/cronjob:demo
          imagePullSecrets:
          - name: registry-secret 

加载yaml

[root@master01 ~]# kubectl apply  -f cronjob-demo.yaml 
cronjob.batch/cronjob-demo created

查看是否执行

[root@master01 ~]# kubectl get cronjob -o wide
NAME           SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE   CONTAINERS     IMAGES                                   SELECTOR
cronjob-demo   */1 * * * *   False     0        <none>          41s   cronjob-demo   registry.tk8s.com/library/cronjob:demo   <none>

[root@master01 ~]# kubectl get cronjob -o wide
NAME           SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE   CONTAINERS     IMAGES                                   SELECTOR
cronjob-demo   */1 * * * *   False     1        40s             81s   cronjob-demo   registry.tk8s.com/library/cronjob:demo   <none>

[root@master01 ~]# kubectl get pod -o wide
NAME                                    READY   STATUS      RESTARTS   AGE     IP              NODE     NOMINATED NODE   READINESS GATES
cronjob-demo-1565861340-46rhb           0/1     Completed   0          2m31s   10.244.140.72   node02   <none>           <none>
cronjob-demo-1565861400-9529r           0/1     Completed   0          101s    10.244.140.71   node02   <none>           <none>
cronjob-demo-1565861460-7tfnj           0/1     Completed   0          40s     10.244.140.76   node02   <none>           <none>
nfs-client-provisioner-74c85598-x6djg   1/1     Running     1          25h     10.244.140.96   node02   <none>           <none>

[root@master01 ~]# kubectl logs cronjob-demo-1565861400-9529r 
I will working for 16 seconds!
All work is done! Bye!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值