1. 下载jdk
下载tar包,上传到node https://pan.baidu.com/s/18IicPYf7W0j-sHBXvfKyyg
配置环境变量
2. 下载tomcat, 到node的/home目录下
wget https://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz
3. 下载jenkins 到/home
wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war
将jenkins的war放到tomcat的webaap目录下
mv jenkins.war /home/tomcat9/webapps
启动tomcat
4. 下载git, 直接在node执行命令
apt-get install get
5. 下载maven, 到/home目录
wget https://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
解压, mv maven... maven3
配置环境变量
然后, 在服务器找到初始密码, 填入
ssh/publish over ssh
git/git parameter
maven Integration plugin
手动添加maven插件
管理-->插件管理-->搜索maven Integration
进入系统配置-->全局工具配置
配置jdk和git
三个目标:
在node节点上, 获取registry镜像
docker pull registry
启动容器
docker run -p 5000:5000 -v /home/registry_images:/var/lib/registry -d registry
这里将registry挂载到了本地目录, 避免docker重启后, 镜像丢失
修改master机器上的registry容器为所在的宿主机
/etc/docker/daemon.json
{
"insecure-registries":["192.168.1.104:5000"], //修改为registry所在容器的宿主机
"registry-mirrors": ["https://w52p8twk.mirror.aliyuncs.com"]
}
我的registry在node节点上, node的ip是192.168.1.104
重启docker
systemctl daemon-reload
systemctl restart docker
下载一个nginx并上传到Registry仓库
领取nginx镜像
docker pull nginx
改名
docker tag nginx 192.168.1.104:5000/nginx:test
尝试把 192.168.1.104:5000/nginx:test上传到我们的Registry仓库
docker push 192.168.1.104:5000/nginx:test
可以成功push, 说明我们的仓库是创建成功了.
下面在node上做同样的操作. 修改/etc/docker/daemon.json文件
{
"insecure-registries":["192.168.1.104:5000"],
"registry-mirrors": ["https://w52p8twk.mirror.aliyuncs.com"]
}
重启docker
systemctl daemon-reload
systemctl restart docker
构建的整体流程:
1. 设置参数化构建
2. 设置代码库的地址
3. 设置maven的构建命令, 执行后会打包出一个jar包, 将jar打包成一个动态镜像, 并推到镜像仓库中
4. 将应用部署的yaml文件拷贝到k8s的master节点上, 然后执行命令, 让k8s根据yaml文件启动应用
目的是: 可以根据分支进行构建.或者标签进行构建
https://github.com/solochen84/SpringBootDemo
项目地址: https://github.com/solochen84/SpringBootDemo
git项目地址: https://github.com/solochen84/SpringBootDemo.git
这个项目是public的, 所以, 不需要配置Credentials
设置构建完之后执行的动作
脚本内容
#!/bin/sh
jarName=spring-boot-demo-0.0.1-SNAPSHOT.jar
jarFolder=ph
projectName=ph
docker_path=${WORKSPACE}
cp ${WORKSPACE}/target/${jarName} ${docker_path}
sh /root/docker_dir/deploy_docker.sh ${projectName} ${docker_path} ${jarName}
创建deploy_docker文件. 目录: /root/docker_dir/deploy_docker.sh. 文件内容如下
设置文件的可执行权限
chmod 775 deploy_docker.sh
deploy_docker.sh文件内容
#!/bin/sh
# maven01 $workspace $jarname
# ${projectName} ${docker_path} ${jarName}
set -e
projectName=$1
docker_path=$2
appName=$3
#user_name=
#password=
tag=$(date +%s)
server_path=192.168.1.104:5000
target_image=${projectName}:${tag}
#${BUILD_NUMBER}
echo ${target_image}
cd ${docker_path}
docker build --build-arg app=${appName} -t ${target_image} .
docker tag ${target_image} ${server_path}/${projectName}
echo The name of image is "${server_path}\/${target_image}"
设置了免密登录, jenkins就可以到k8s上运行脚本, 执行命令
生成秘钥
ssh-keygen -t rsa
拷贝公钥
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.106
ssh root@192.168.1.106
yaml文件的位置, 是在项目里面的.
set -e
echo ok
echo ${WORKSPACE}
docker_path=${WORKSPACE}
scp ${WORKSPACE}/*.yaml 192.168.1.106:/root/
ssh 192.168.1.106 '/usr/bin/kubectl apply -f /root/kube.yaml'
ssh 192.168.1.106 '/usr/bin/kubectl get svc|grep maven'
这里的ip地址填master的ip地址
构建的过程中, 会出现各种各样的问题
1. no matches for kind "Deployment" in version "extensions/v1beta1"
参考文章: https://www.cnblogs.com/nnylee/p/11779653.html
修改对应的shell脚本
set -e
echo ok
echo ${WORKSPACE}
docker_path=${WORKSPACE}
scp ${WORKSPACE}/*.yaml 192.168.1.106:/root/
ssh 192.168.1.106 'sed -i "s|extensions/v1beta1|apps/v1|" /root/kube.yaml'
ssh 192.168.1.106 'sed -i "s|192.168.0.108|192.168.1.104|" /root/kube.yaml'
ssh 192.168.1.106 '/usr/bin/kubectl apply -f /root/kube.yaml --validate=false'
ssh 192.168.1.106 '/usr/bin/kubectl get svc|grep maven'
2. spec.template.metadata.labels: Invalid value: map[string]string{"app":"maven"}: `selector` does not match template `labels`.
Build step 'Execute shell' marked build as failure
参考的是这篇文章: https://www.cnblogs.com/robinunix/p/11155860.html
我修改后的配置
apiVersion: v1
kind: Service
metadata:
name: maven-service
spec:
type: NodePort
ports:
- name: maven
port: 8080
nodePort: 31002
targetPort: 8080
protocol: TCP
selector:
app: maven
---
apiVersion: apps/v1
kind: Deployment
metadata:
#name: maven-deployment
name: maven
spec:
selector:
matchLabels:
app: maven
replicas: 1
template:
metadata:
labels:
app: maven
spec:
containers:
- name: maven
image: 192.168.1.104:5000/maven:latest
ports:
- containerPort: 8080
env:
- name: key
value: "value"
然后重新启动, 成功!
kubectl get deployments --all-namespaces
发现,name为maven的deployment的Ready状态是0个
查看pod
kubectl get pod --all-namespaces
发现有一个pending状态
查看pod的日志
kubectl describe pod maven-7589958577-5ms68 -n default