zoukankan      html  css  js  c++  java
  • kubernetes 学习 创建cronjob

    POM.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>XXXXXXXXXX</groupId>
        <artifactId>XXXXXXX</artifactId>
        <version>1.0-SNAPSHOT</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
        <properties>
            <redis.version>2.9.0</redis.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${redis.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
        </dependencies>
        <profiles>
            <profile>
                <id>dist</id>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-jar-plugin</artifactId>
                            <configuration>
                                <archive>
                                    <manifest>
                                        <mainClass>含有main函数的类的全名</mainClass>
                                        <addClasspath>true</addClasspath>
                                        <classpathPrefix>lib/</classpathPrefix>
                                    </manifest>
                                    <!-- Add config folder into classpath of MANIFEST -->
                                    <manifestEntries>
                                        <Class-Path>src/main/resources</Class-Path>
                                    </manifestEntries>
                                </archive>
                                <classesDirectory></classesDirectory>
                                <!--<excludes> <exclude>*.conf</exclude> <exclude>*.xml</exclude>
                                    </excludes> -->
                            </configuration>
                        </plugin>
                        <!-- Copy dependency Jars to lib folder -->
                        <plugin>
                            <artifactId>maven-dependency-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>copy</id>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                    </goals>
                                    <configuration>
                                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                            <artifactId>maven-assembly-plugin</artifactId>
                            <configuration>
                                <descriptors>
                                    <descriptor>assembly.xml</descriptor>
                                </descriptors>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>make-assembly</id>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>single</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>

    assembly.xml

    <assembly>
        <id>dist</id>
        <formats>
            <format>zip</format>
        </formats>
        <!-- set to false the archive created will unzip its content to the current directory. -->
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <!-- copy file from target folder -->
            <fileSet>
                <directory>${basedir}/target</directory>
                <includes>
                    <include>*.jar</include>
                </includes>
                <outputDirectory></outputDirectory>
            </fileSet>
            <fileSet>
                <directory>${basedir}/target/lib</directory>
                <outputDirectory>lib</outputDirectory>
            </fileSet>
            <!-- copy file from source folder -->
            <fileSet>
                <directory>${basedir}/dist</directory>
                <outputDirectory></outputDirectory>
                <fileMode>744</fileMode>
                <lineEnding>unix</lineEnding>
                <includes>
                    <include>start</include>
                    <include>stop</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>

    logback.xml

    <!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
    <configuration>
        <property name="APP_NAME" value="your application name"/>
        <property name="SERVICE_NAME" value="your service name" />
        <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_PATH:-/var/log/${APP_NAME}.log}</file>
            <encoder>
                <pattern>[%date{ISO8601}] [${SERVICE_NAME}] [${HOSTNAME}] [%thread] [%-5level] [%logger{36}] :%msg%n
                </pattern>
            </encoder>
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <fileNamePattern>${LOG_PATH}/${APP_NAME}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
                <maxFileSize>100MB</maxFileSize>
                <maxHistory>60</maxHistory>
                <totalSizeCap>20GB</totalSizeCap>
            </rollingPolicy>
        </appender>
        <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
            <discardingThreshold>0</discardingThreshold>
            <queueSize>512</queueSize>
            <appender-ref ref="ROLLING"/>
        </appender>
        <logger name="ImagesDownLoad" additivity="false">
            <level value="INFO" />
            <appender-ref ref="STDOUT" />
        </logger>
    
        <root level="INFO">
            <appender-ref ref="ASYNC"/>
        </root>
    </configuration>

    dockerfile

    FROM openjdk:8-jre
    
    ARG PACKAGE="XXXXXXXXXXXXX-1.0-SNAPSHOT-dist.zip"
    
    COPY target/${PACKAGE} /${PACKAGE}
    COPY XXX.jks /var/lib/XXX.jks
    
    RUN mkdir /build && 
        unzip /${PACKAGE} -d /build
    
    EXPOSE 9000
    
    ENTRYPOINT ["java", "-cp", "/build/*", "含有main的类全名"]

    yaml文件:

    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: XXXXXX
    spec:
      schedule: "*/30 * * * *"   // 每隔30分钟执行一次
      jobTemplate:
        spec:
          template:
            spec:
              imagePullSecrets:
                - name: XXXXXXXXX-key    // pull image的时候,需要认证
              containers:
              - name: imagedownload
                image: 私有image仓库的地址/imagedownloadjob:259
                env:
                  - name: DB_HOST
                    value: "XXXXXXX"
              restartPolicy: OnFailure

    过程:

           1: 在工程目录下:   mvn clean pcage -P dist           // 打包,安装POM中定义的dist的方式进行打包

           2:  创建docker 镜像:

       docker build -t url/forecasting/imagedownloadjob:196 --compress --force-rm --build-arg VERSION=196 --build-arg                        MICROSERVICE=job.imagedownloadjob --build-arg REPOSITORY=url/forecasting/imagedownloadjob .

                (  -t, --tag list                Name and optionally a tag in the

                   --compress                Compress the build context using gzip

                   --force-rm                Always remove intermediate containers

                   --build-arg list          Set build-time variables)

           3:  推送docker镜像到私有仓库之前,先要登录一下docker仓库     docker login URL

                 把docker镜像推到私有仓库:

                        docker push url/forecasting/imagedownloadjob:196

                通过pull命令验证是否推送成功
                docker pull url/forecasting/imagedownloadjob:196

            4:  kubectl create -f cronjob.yaml //创建job 

                  kubectl get cronjob hello //查看job状态

                 kubectl get jobs --watch // 查看

                 kubectl describe pod fail-1036623984-hxoas // 查看pod失败的原因

                  kubectl delete cronjob hello //删除cronjob

                  使用kubectl delete jobs –all可以删除当前namespaces下所有的job

       

  • 相关阅读:
    CISCO一些基本配置
    每日学习
    每日学习(个人作业2)
    每日学习
    团队冲刺第十四天
    每日学习
    团队冲刺第十三天
    每日学习
    团队冲刺第十二天
    2021.5.26
  • 原文地址:https://www.cnblogs.com/liufei1983/p/9258436.html
Copyright © 2011-2022 走看看