1.自建背景:因为这个项目需要指定jdk1.8与tomcat8.5而且依赖maven打包的依赖有个本地目录,所以就自建一个包含了jdk-tomcat-maven+依赖包的基础镜像,然后通过云效流水线拉取代码在镜像内部打包构建,通过ack发布镜像。

2.基础镜像jdk1.8-tomcat8.5-maven3.6-撼地依赖包

先将所有需要用到的软件包放到Dockerfile平级目录

apache-maven-3.8.6-bin.tar.gz  Dockerfile handibigdata jdk1.8.0_201  jindi-client  settings.xml
apache-tomcat-8.5.81.tar.gz  Dockerfile.back  handibigdata.zip  jdk-8u201-linux-x64.tar.gz  maven tomcat

vi settings.xml

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
<localRepository>/root/.m2/repository</localRepository>
<mirrors>
<mirror>
  <id>alimaven</id>
  <mirrorOf>central</mirrorOf>
  <url>https://maven.aliyun.com/repository/public/</url>
</mirror>
</mirrors>

docker build -t gem-acr-p-a01-registry.cn-shenzhen.cr.aliyuncs.com/devops/centos7-jdk1.8-tomcat8.5:1 .
docker push gem-acr-p-a01-registry.cn-shenzhen.cr.aliyuncs.com/devops/centos7-jdk1.8-tomcat8.5:1
FROM centos:centos7
MAINTAINER ajia
RUN yum -y install openssl gcc gcc-c++ wget apr-devel openssl-devel
RUN yum -y install kde-l10n-Chinese
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
ENV LC_ALL zh_CN.utf8
#添加当前目录到镜像目录,镜像中没有会自动生成一个目录
ADD ./jdk-8u201-linux-x64.tar.gz /data/
RUN mv /data/jdk1.8.0_201 /data/jdk

ENV JAVA_HOME=/data/jdk
ENV CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$PATH:$JAVA_HOME/bin

COPY ./tomcat /home/tomcat
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#安装mvn
COPY ./maven /usr/local/
ENV MAVEN_HOME=/usr/local/maven
ENV PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
ADD ./handibigdata /root/.m2/repository/com/handibigdata
ADD ./jindi-client /home/jindi-client
COPY ./settings.xml /usr/local/maven/conf/
RUN cd /home/jindi-client && mvn -B clean package -Dautoconfig.skip -U -Dmaven.test.skip=true -P test
RUN rm -rf /home/jindi-client
RUN cd /home/tomcat/bin  && mkdir -p /usr/local/apr \
&& tar -zxvf tomcat-native.tar.gz -C /usr/local/apr \
&& cd /usr/local/apr/tomcat-native-1.2.33-src/native \
&& ./configure --prefix=/usr/local/apr \
&& make && make install

3.云效流水线利用基础镜像构建项目镜像

FROM gem-acr-p-a01-registry.cn-shenzhen.cr.aliyuncs.com/devops/centos7-jdk1.8-tomcat8.5:1
#ARG APP_PATH=/home
#ARG APP_VERSION=1.0
#ARG JAR_FILE=@artifactId@-@version@.jar
#ENV ENV_APP_PATH=/home
WORKDIR /home
ADD ./APP-NAME /home/APP-NAME
RUN cd /home/APP-NAME && mvn -B clean package -Dautoconfig.skip -U -Dmaven.test.skip=true -P test
RUN cp -r /home/APP-NAME/target/*.war /home/tomcat/webapps && mv /home/tomcat/webapps/*.war /home/tomcat/webapps/APP-NAME.war
EXPOSE 8080 443
CMD ["/home/tomcat/bin/catalina.sh","run"]
pwd 
cp -r ../jindi-client dockerfile-jdk1.8/
#cp -r ../jindi-client ./
ls dockerfile-jdk1.8
sed -i 's/APP-NAME/jindi-client/g' dockerfile-jdk1.8/Dockerfile
sed -i 's/jindi-client\.war/jindi_client\.war/g' dockerfile-jdk1.8/Dockerfile
cat dockerfile-jdk1.8/Dockerfile

 

 

 4.制作yaml文件,发布镜像到k8s

echo "K8S arguments ***************"
echo "APP NAME: ${K8S_APP_NAME}-${MAVEN_PROFILE}"
echo "PORT TYPE:${K8S_PORT_TYPE}"
echo "NODE PORT:${K8S_NODE_PORT}"
echo "REPLICAS: ${K8S_REPLICAS}"
echo "SERVICE PORT:${K8S_SERVICE_PORT}"
echo "NODE LABEL:${NODE_LABEL}"

#cd springboot
mkdir k8syaml
pwd
cp ./${K8S_SPRINGBOOT_PATH}/k8s_template-node1.yaml k8syaml/k8s_template.yaml
cp ./${K8S_BASE_SHELL}/yaml-generate.sh k8syaml/

cat k8syaml/k8s_template.yaml
cd k8syaml
sed -i 's/\/gemdale\/logs/\/home\/tomcat\/logs/g' k8s_template.yaml
chmod +x yaml-generate.sh
./yaml-generate.sh -f k8s_template.yaml -n "${K8S_NODE_PORT}" -p "${K8S_SERVICE_PORT}" -a "${K8S_APP_NAME}-${MAVEN_PROFILE}" -r "${K8S_REPLICAS}" -t "${K8S_PORT_TYPE}" -w "${LIMIT_CPU}" -x "${LIMIT_MEMORY}" -y "${REQUEST_CPU}" -z "${REQUEST_MEMORY}"

cat k8s_template.yaml

 yaml-generate.sh

#!/bin/sh

# 命令行输入参数: kubernetes yaml文件名  端口号
echo "Get replace holders args:"
PORT_TYPE='ClusterIP'
while getopts f:n:p:a:v:t:r: opt
do
  case "$opt" in
  f) echo " -Generate from yaml file: $OPTARG"
    YAML_FILE=$OPTARG
    ;;
  n) echo " -nodePort:$OPTARG"
    NODE_PORT=$OPTARG;;
  p) echo " -PORT&TARGET_PORT:$OPTARG"
    port=$OPTARG
    targetPort=$OPTARG
    ;;
  a) echo " -APP_NAME:$OPTARG"
    APP_NAME=$OPTARG
    ;;
  r) echo " -REPLICAS:$OPTARG"
    REPLICAS=$OPTARG
    ;;
  v) echo " -APP_VERSION:$OPTARG"
    APP_VERSION=$OPTARG
    ;;
  t) echo " -PORT_TYPE:$OPTARG"
    PORT_TYPE=$OPTARG
   ;;
  *) echo "Unknown option: $opt"
     echo "-f yaml file name
     -n nodePort
     -p port/targetPort
     -a APP_NAME
     -v APP_VERSION
     -t PORT_TYPE
     -n NODE_PORT
     -r REPLICAS
     ";;
  esac
done


sed -i s/@APP_NAME@/${APP_NAME}/g ${YAML_FILE}
sed -i s/@targetPort@/${targetPort}/g ${YAML_FILE}
sed -i s/@APP_VERSION@/${APP_VERSION}/g ${YAML_FILE}
sed -i s/@port@/${port}/g ${YAML_FILE}

if [[ $PORT_TYPE == "NodePort" ]]
then
  NODE_PORT="nodePort: ${NODE_PORT}";
else
  NODE_PORT=""
fi
sed -i s/@NodePort@/"${NODE_PORT}"/g ${YAML_FILE}

sed -i s/@PORT_TYPE@/${PORT_TYPE}/g ${YAML_FILE}
sed -i s:@APP_IMAGE@:${APP_IMAGE}:g ${YAML_FILE}
sed -i s:@REPLICAS@:${REPLICAS}:g ${YAML_FILE}

echo "
File after replacing:
"
cat ${YAML_FILE}

k8s_template-node1.yaml

apiVersion: apps/v1
kind: Deployment  
metadata:  
  name: @APP_NAME@
  labels:  
    app: @APP_NAME@
spec:  
  replicas: @REPLICAS@
  revisionHistoryLimit: 10
  selector:  
    matchLabels:  
      app: @APP_NAME@
  template:  
    metadata:  
      labels:  
        app: @APP_NAME@
        armsPilotAutoEnable: "on"
        armsPilotCreateAppName: @APP_NAME@
        one-agent.jdk.version: "OpenJDK11"
    spec:
      imagePullSecrets:  
      - name: osale-secret    
      containers:  
      - name: @APP_NAME@
        image: ${IMAGE}
        ports:  
        - containerPort: @targetPort@
          protocol: TCP  
        imagePullPolicy: IfNotPresent
        volumeMounts:
          - name: logs
            mountPath: /gemdale/logs
      volumes:
        - name: logs
          hostPath:
            type: DirectoryOrCreate 
            path: /data/test-logs/@APP_NAME@ 
      nodeSelector:
        alibabacloud.com/is-edge-worker: 'false'
        beta.kubernetes.io/arch: amd64
        beta.kubernetes.io/os: linux
      tolerations:
        - effect: NoSchedule
          key: node-role.alibabacloud.com/addon
          operator: Exists
 
---  
apiVersion: v1  
kind: Service  
metadata:  
  name: @APP_NAME@
  labels:  
    app: @APP_NAME@
spec:  
  ports:  
    - port: @port@
      targetPort: @targetPort@
      @NodePort@
  selector:  
    app: @APP_NAME@
  type: @PORT_TYPE@  

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐