k8s自动部署教程
文章目录k8s自动部署教程k8s自动部署教程启动jenkinssystemctl start | stop | restart | status jenkinspipeline {agent anyoptions {// 默认15分钟 timeout (15)timeout(time: 3, unit: 'MINUTES')}environment {build_dir="/root/build-
·
缺陷记录
- 部署成功的外界无法访问到
参考文章
实战命令
kubectl get pods -o wide 节点名
netstat -tlunp
kubectl get pods,svc
curl -I localhost:19999
k8s自动部署教程
- 启动jenkins
systemctl start | stop | restart | status jenkins
pipeline {
agent any
options {
// 默认15分钟 timeout (15)
timeout(time: 3, unit: 'MINUTES')
}
environment {
build_dir="/root/build-workspace"
host="k8s-web.muoo.com"
}
stages {
stage('build') {
steps{
// git 'https://gitee.com/goodshred/k8s-hello-world.git'
// sh "mvn -Dmaven.test.failure.ignore=true clean package"
echo "curUser=${USER}"
script {
sh "chmod +x /root/script/build.sh"
sh "chmod +x /root/script/deploy.sh"
}
sh "/root/script/start.sh"
sh "/root/script/deploy.sh"
}
}
}
post {
success {
script {
echo "post==success"
}
}
failure {
script {
echo "post==failure"
}
}
//构建不稳定时
//随着项目的开发和持续扩展,使用Jenkins构建(build)项目越来越多,Jenkins服务器的磁盘可能被大日志文件占满造成无法继续构建项目的异常(一般会出现构建按钮为黄色)
unstable {
script {
echo "post==unstable "
}
}
}
}
代码分析
先运行jenkins脚本代码(1. 拉代码,2. mvn将代码打成jar包,3. 执行脚本build.sh–将jar包打成镜像,4. 执行deploy.sh将镜像丢到k8s中运行)
拉代码,代码变jar包,jar变镜像,k8s命令启动镜像。
打包成docker镜像
vi Dockerfile
没有jdk:8-jdk-alpine镜像的话首次运行会自动进行下载拷贝到要制作的镜像里面
FROM openjdk:8-jdk-alpine
COPY ${jenkins_dir}target/hello-world-0.0.1-SNAPSHOT.jar /hello-world-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar","/hello-world-0.0.1-SNAPSHOT.jar"]
k8s+jenkins+git
jenkins的pipeline如下:
pipeline {
//说明流水线哪一个节点运行
agent any
options {
// 默认15分钟 timeout (15)
timeout(time: 13, unit: 'MINUTES')
}
environment {
build_dir="/opt/k8s-cicd/build-workspace"
project_name="hello-world"
host="k8s-web.muoo.com"
}
stages {
stage('build') {
steps{
git 'https://gitee.com/goodshred/k8s-hello-world.git'
sh "mvn -Dmaven.test.failure.ignore=true clean package"
sh 'echo "curUser=${USER}"'
//script {
//本质还是不应该写在这里,碰到无权限问题应该手动去操作linux机器,比如配置两台机器的免密登陆,使用root权限给某个文件赋予允许jenkins执行的权限
//chmod: changing permissions of ‘/opt/k8s-cicd/script/build.sh’: Operation not permitted
//我是用的root创建的脚本文件,因此jenkins用户无法更改其权限
//sh "chmod +x /opt/k8s-cicd/script/build.sh"
//sh "chmod +x /opt/k8s-cicd/script/deploy.sh"
// }
sh "${build_dir}/script/build.sh"
sh "${build_dir}/script/deploy.sh"
}
}
}
post {
success {
script {
echo "post==success"
}
}
failure {
script {
echo "post==failure"
}
}
//构建不稳定时
//随着项目的开发和持续扩展,使用Jenkins构建(build)项目越来越多,Jenkins服务器的磁盘可能被大日志文件占满造成无法继续构建项目的异常(一般会出现构建按钮为黄色)
unstable {
script {
echo "post==unstable "
}
}
}
}
创建目录结构如下:
Dockerfile文件
FROM openjdk:8-jdk-alpine
COPY hello-world-0.0.1-SNAPSHOT.jar /hello-world-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar","/hello-world-0.0.1-SNAPSHOT.jar"]
build.sh文件:
#!/bin/bash
if [ "${build_dir}" == "" ];then
echo "env 'build_dir' is not set"
exit 1
fi
# 构建时候的工作名,比如Jenkins自动构建个项目,取名叫k8s-cicd,那么${JOB_NAME}=k8s-cicd,它是Jenkins自带的全局变量
# /opt/k8s-cicd/build-workspace/k8s-web-demo
docker_dir=${build_dir}/${JOB_NAME}
if [ ! -d ${docker_dir} ];then
mkdir -p ${docker_dir}
fi
echo "docker workspace: ${docker_dir}"
# ${WORKSPACE}/${MODULE}=/var/lib/jenkins/workspace/k8s-web-demo/
jenkins_dir=${WORKSPACE}/${MODULE}
export jenkins_dir=${jenkins_dir}
echo "jenkins_dir is ${jenkins_dir}"
if [ ! -f ${jenkins_dir}target/*jar ];then
echo "target file not found ${jenkins_dir}target/*jar"
exit 1
fi
\cp ${jenkins_dir}target/*jar ${docker_dir}
cd ${docker_dir}
# VERSION=$(date + %y%m%d%H%M%S%)
VERSION=$(date "+%y-%m-%d_%H-%M-%S")
IMAGE_NAME=goodshred/${project_name}:${VERSION}
echo ${IMAGE_NAME}
echo "${IMAGE_NAME}" > ${WORKSPACE}/IMAGE
echo "workspave is : ${WORKSPACE}======================"
cat ${WORKSPACE}/IMAGE
echo "build image ${IMAGE_NAME}"
docker build -t ${IMAGE_NAME} .
docker push ${IMAGE_NAME}
deploy.sh文件如下:
#!/bin/bash
name=${JOB_NAME}
image=$(cat ${WORKSPACE}/IMAGE)
\cp ${build_dir}/template/web.yaml ${build_dir}/script/web.yaml
sed -i "s,{{name}},${name},g" ${build_dir}/script/web.yaml
sed -i "s,{{image}},${image},g" ${build_dir}/script/web.yaml
kubectl apply -f ${build_dir}/script/web.yaml
cat ${build_dir}/script/web.yaml
web.yaml如下:
apiVersion: v1
kind: Service
metadata:
name: {{name}}
namespace: default
labels:
app: {{name}}
spec:
type: NodePort
ports:
- port: 30009
targetPort: 19999
# type: NodePort # 在主机上暴露端口
nodePort: 19999 #service对外开放端口
selector:
app: {{name}}
---
apiVersion: extensions/v1beta1
#apps/v1,extensions/v1beta1需要根据自己k8s的版本选择
kind: Deployment #对象类型
metadata:
name: {{name}} #名称
labels:
app: {{name}} #标注
spec:
replicas: 1 #运行容器的副本数,修改这里可以快速修改分布式节点数量
selector:
matchLabels:
app: {{name}}
template:
metadata:
labels:
app: {{name}}
spec:
containers: #docker容器的配置
# {{name}}
- name: {{name}}
#image: goodshred/hello-world:latest
## 192.168.1.253:8081/pasq/dockertest:0.0.1 # pull镜像的地址 ip:prot/dir/images:tag
# {{image}}
image: {{image}}
imagePullPolicy: IfNotPresent #pull镜像时机,
ports:
- containerPort: 19999 #容器对外开放端口
更多推荐
已为社区贡献6条内容
所有评论(0)