由于项目需要,这段时间在研究使用K8S,已基本了解使用和操作;这里就截取我记录的文档讲下创建的第一个Pipeline任务: 如何发布一个demo的JAVA程序到K8S中。
首先大致介绍下环境:
- K8S群集:使用Kubeasz的Ansible项目搭建的环境(不限定,任务中使用kubectl进行发布)
- Jenkins主机:部署于一台物理机,装有 Docker,docker-build-step,Pipeline相关插件。
- 构建镜像机: 使用K8S群集内一台主机,因Jenkins主机上未安装配置Docker命令,故使用另外一台主机;也可复用Jenkins主机
接下来就讲下主要过程
创建基础Docker镜像
在镜像构建主机上进行操作,这里我使用了CentOS作为底镜像:
- 拉取官方镜像:
docker pull centos docker images
- 准备要添加进镜像的文件,及创建Dockerfile:
mkdir docker-baseimg cd docker-baseimg vim java.sh
export JAVA_HOME=/usr/local/jdk1.8.0_271 export PATH=$JAVA_HOME/bin:$PATH
vim start-jar.sh
#!/bin/bash # Author:Chris __On 2021/01/18 # =====================Description===================== # [Features]: This is for Start a jar Package in Docker Container. # ===================================================== # <----------------------------Configure Start---------------------------> Base=$(cd `dirname ${BASH_SOURCE}` ; pwd) App_Name="app.jar" App_Execute="${Base}/${App_Name}" Log_Path="${Base}/logs" App_Log="${Log_Path}/app-stdout.log" Xms=${JVM_XMS:-"256m"} Xmx=${JVM_XMX:-"512m"} Start_Opts="-Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -XX:+UseCMSInitiatingOccupancyOnly -XX:-UseLargePages -XX:+UseFastAccessorMethods -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${Log_Path}/java_heapdump.hprof" [ ! -z "${PROFILE_ACTIVE}" ] && S_Profile="--spring.profiles.active=${PROFILE_ACTIVE}" Ext_Args="$@" # <----------------------------Configure End----------------------------> if [ ! -f "${App_Execute}" ];then echo "ERROR: Can not find execute file: ${App_Execute}." exit 1 fi if [ -z "${JAVA_HOME}" ];then source /etc/profile.d/java.sh fi [ ! -d "${Log_Path}" ] && mkdir -p ${Log_Path} # 使用exec command, 会用 command 进程替换当前shell进程, 并且保持 PID 不变。 # 为保证Docker容器不会终止, 脚本需一直处于运行状态, # 故不要在最后添加 & 让java在后台另起进程运行, 而是直接替代当前脚本运行。 # Tips: docker logs显示容器内/dev/stdout, /dev/stderr的内容, 阿里云k8s也是收集此日志; # 故运行时就不进行日志重定向($App_Log)及 2>&1 了 exec java -server -Xms${Xms} -Xmx${Xmx} -jar ${App_Execute} ${Start_Opts} ${S_Profile} ${Ext_Args}
chmod +x start-jar.sh vim Dockerfile
FROM centos as build RUN sed -i "/^alias mv/a\alias ll='ls -lh --time-style=\"+%Y/%m/%d %H:%M\" --color'" ~/.bashrc RUN mkdir -p /app/logs ADD jdk-8u271-linux-x64.tar.gz /usr/local/ ADD java.sh /etc/profile.d/ ADD start-jar.sh /app/ RUN cd /usr/local/jdk1.8.0_271 && rm -rf src.zip \ lib/visualvm jre/lib/plugin.jar jre/bin/javaws \ jre/lib/desktop jre/plugin jre/lib/deploy* \ jre/lib/amd64/libglass.so \ jre/lib/amd64/libgstreamer-lite.so \ jre/lib/amd64/libjavafx*.so \ jre/lib/amd64/libjfx*.so FROM scratch COPY --from=build / . LABEL maintainer="Chris" WORKDIR /app
- 制作镜像:
docker build -t baseimg-jdk8:c8 . docker images
- 试运行基础镜像(可选测试):
docker run -it --rm baseimg-jdk8:c8 /bin/bash
- 上传基础镜像到Harbor:
vim /etc/docker/daemon.json
... "insecure-registries": ["127.0.0.1/8","nw-harbor.xxxx.cc"], ...
systemctl restart docker docker info
Tips:若要从自建Harbor上拉取镜像,此配置需要在群集内所有worker上都操作。若Harbor配置了HTTPS访问则可不用。docker login http://nw-harbor.xxxx.cc/ -u admin -p Nw-Harbor123
docker tag baseimg-jdk8:c8 nw-harbor.xxxx.cc/library/baseimg-jdk8:c8 docker images
docker push nw-harbor.xxxx.cc/library/baseimg-jdk8:c8
登陆到Harbor,即可看到已上传:
页码: 1 2
hello there. What can i do for you hehes thanks
i registered 2 weeks ago but i cant find where can i search any words.
thanks for help 🙂
hi, what do you wanna search for?
the search bar is on the top-right.