一丶准备工作
1.安装好jenkins
2.安装好k8s
3.熟悉Docker,K8S,Jenkins基本使用 了解代码版本仓库(Git),容器镜像仓库(Harbor)了解Java项目发布流程
二丶Harbor镜像仓库
1、下载离线安装包
https://github.com/goharbor/harbor/releases
2、解压并配置访问地址
# tar zxvf harbor-offline-installer-v1.6.1.tgz
# cd harbor
# vi harbor.cfg
hostname = 10.206.240.188
harbor_admin_password = 123456
3、准备配置
# ./prepare
4、导入镜像并启动
# ./install.sh
5、查看容器状态
# docker-compose ps
三丶Git代码版本仓库
1.一种是自己找台服务器安装(自行安装)
2.使用阿里云code,或gitlub等代码管理库
四丶Jenkins安装相关插件
• Git Parameter 主要拉去Jenkinsfile 具体后面讲解
• Kubernetes 主要用于jenkins与k8s连接
• Pipeline 流水线
• Kubernetes Continuous Deploy 用于将制作好的镜像部署到k8s集群中
五丶Jenkins配置
1.全局安全配置 开放50000端口
2.系统配置拉到最下面 k8s云
kubernetes地址
[root]# kubectl get svc
六丶构建Jenkins Slave镜像
链接:https://pan.baidu.com/s/1c6-wElnafuItF7D0-s2oPw
提取码:1h90
vim Dockerfile 输入如下内容
FROM centos:7
LABEL maintainer salter
COPY jdk-8u221-linux-x64.tar.gz /usr/local/jdk-8u221-linux-x64.tar.gz
RUN ls .
RUN cd /usr/local && tar -zxvf jdk-8u221-linux-x64.tar.gz
RUN yum install -y curl git libtool-ltdl-devel && \
yum clean all && \
rm -rf /var/cache/yum/* && \
mkdir -p /usr/share/jenkins
RUN yum -y install wget
RUN wget https://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
RUN tar -zxvf apache-maven-3.6.3-bin.tar.gz
RUN mv apache-maven-3.6.3 /usr/local/maven3
ENV M3_HOME=/usr/local/maven3
ENV JAVA_HOME=/usr/local/jdk1.8.0_221
ENV PATH=${PATH}:${JAVA_HOME}/bin:${M3_HOME}/bin
RUN mvn -v
COPY slave.jar /usr/share/jenkins/slave.jar
COPY jenkins-slave /usr/bin/jenkins-slave
COPY settings.xml /usr/local/maven3/conf/settings.xml
RUN chmod +x /usr/bin/jenkins-slave
ENTRYPOINT ["jenkins-slave"]
2.制作镜像
[root]# docker build -t 172.17.137.168:90/library/jenkins-slave-maven3.6-jdk8-git .
3.上传镜像到Harbor
docker login 172.17.137.168:90
docker push 172.17.137.168:90/library/jenkins-slave-maven3.6-jdk8-git
七丶根据项目编写Jenkinsfile及deploy.yaml文件
1.Jenkinsfile 内容如下
def image_name = "172.17.137.168:90/library/live-scrm:${BUILD_NUMBER}" def k8s_auth = "9914859b-3bf5-454a-ab89-bac3f1bd5864" podTemplate(label: 'jenkins-slave', cloud: 'kubernetes', containers: [ containerTemplate( name: 'jnlp', image: "172.17.137.168:90/library/jenkins-slave-maven3.6-jdk8-git" ), ], volumes: [ hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'), hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker'), hostPathVolume(mountPath: '/root/.m2', hostPath: '/root/.m2') ], ) { node("jenkins-slave"){ // 第一步 stage('拉取代码'){ git 'https://code.aliyun.com/personal-dev/live-scrm.git' } // 第二步 stage('代码编译'){ sh "mvn clean package -Dmaven.test.skip=true" } // 第三步 stage('构建镜像'){ sh """ echo ' FROM java:8 MAINTAINER live-scrm ADD target/*.jar live-scrm.jar EXPOSE 8080 ENTRYPOINT ["java","-jar","live-scrm.jar"] ' > Dockerfile docker build -t ${image_name} . docker login -u admin -p '123456' 172.17.137.168:90 docker push ${image_name} """ } // 第四步 stage('部署到K8S平台'){ sh """ sed -i 's#\$IMAGE_NAME#${image_name}#' live-scrm.yaml """ kubernetesDeploy configs: 'live-scrm.yaml', kubeconfigId: "${k8s_auth}" } } }
2.deploy.yaml文件内容如下 我这里是取名为live-scrm.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: live-scrm spec: replicas: 1 selector: matchLabels: app: live-scrm template: metadata: labels: app: live-scrm spec: containers: - name: live-scrm image: $IMAGE_NAME imagePullPolicy: IfNotPresent ports: - containerPort: 8082 resources: requests: cpu: 500m volumeMounts: - name: timezone mountPath: /etc/localtime imagePullSecrets: - name: dockerregsecret volumes: - name: timezone hostPath: path: /usr/share/zoneinfo/Asia/Shanghai --- apiVersion: v1 kind: Service metadata: name: live-scrm spec: type: NodePort #这里代表是NodePort类型的 ports: - port: 8082 #这里的端口和clusterIP(10.97.114.36)对应,即10.97.114.36:80,供内部访问。 targetPort: 8082 #端口一定要和container暴露出来的端口对应,nodejs暴露出来的端口是8081,所以这里也应是8081 protocol: TCP nodePort: 32143 # 所有的节点都会开放此端口,此端口供外部调用。 selector: app: live-scrm
八丶Jenkins Pipeline构建流水线发布
九丶点击构建,见证奇迹吧~