k8s 3主6从 配置 gitlab、gitlab-runner、harbor、部署java应用 到k8s
harbor
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
项目地址:https://gitcode.com/gh_mirrors/ha/harbor
免费下载资源
·
harbor
# 安装在 192.168.0.28 也就是 k8s ha2 节点
gitlab
# 安装在 192.168.0.54 也就是 k8s ha3 节点
gitlab 管理账号配置 gitlab-runner
gitlab-runner
# 安装在 192.168.0.54 也就是 k8s ha3 节点
# 工作目录:/home/gitlab-runner/builds/*
# 安装 docker
# 编写 /root/gitlab-runner-biuld-image/docker-build.sh
#!/bin/bash
MODULE=$1
APP_NAME=$2
VERSION=$3
TIME=`date "+%Y%m%d%H%M"`
GIT_REVISION=`git log -1 --pretty=format:"%h"`
IMAGE_NAME=harbor.bcwl.com/bcwl/${APP_NAME}:${VERSION}
cd ${MODULE}
echo "构建docker images `${APP_NAME}` time `${TIME}` git log `${GIT_REVISION}` "
docker build -t ${IMAGE_NAME} .
cd -
docker push ${IMAGE_NAME}
echo "删除镜像 ${IMAGE_NAME}"
docker rmi -f ${IMAGE_NAME}
k8s master 节点,部署java 应用到k8s 集群
# 安装在 192.168.0.41
# 编写 /root/auto-build-app/k8s-auto-build-deploy.sh
#!/bin/bash
file_name=$1
image=$2
metadata_name=$3
label_name=$4
port=$5
replicas=1
node_port=$7
if [ -n "$6" ] ;then
replicas=$6
fi
cd /root/auto-build-app
rm -rf ${file_name}
echo "1 = $1 2 = $2 3 = $3 4 = $4 5 = $5 6 = $6 7 = $7"
echo "file_name = ${file_name} port = ${port} image = ${image} metadata_name = ${metadata_name} label_name = ${label_name} node_port = ${node_port} "
cat >> ${file_name} << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${metadata_name}-deploy
spec:
revisionHistoryLimit: 3
minReadySeconds: 80
replicas: ${replicas}
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 50%
maxUnavailable: 30%
selector:
matchLabels:
app: ${label_name}
template:
metadata:
labels:
app: ${label_name}
spec:
terminationGracePeriodSeconds: 80
containers:
- lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 60"]
name: ${label_name}
image: ${image}
ports:
- containerPort: ${port}
resources:
limits:
memory: 2048Mi
requests:
cpu: 300m
memory: 1024Mi
EOF
if [ -n "$7" ] ;
then
cat >> ${file_name} << EOF
---
apiVersion: v1
kind: Service
metadata:
name: ${metadata_name}-service
spec:
type: NodePort
ports:
- port: ${port}
targetPort: ${port}
nodePort: ${node_port}
selector:
app: ${label_name}
EOF
else
cat >> ${file_name} << EOF
---
apiVersion: v1
kind: Service
metadata:
name: ${metadata_name}-service
spec:
ports:
- port: ${port}
targetPort: ${port}
selector:
app: ${label_name}
EOF
fi
kubectl apply -f ${file_name} --record
项目编写 Dockerfile
FROM harbor.bcwl.com/jre/jre:8
COPY bcwl-cache-provider/target/bcwl-cache-provider-1.0-SNAPSHOT.jar /bcwl-cache.jar
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=testing", "-Xms1024m","-Xmx1024m", "/bcwl-cache.jar"]
项目编写 .gitlab-ci.yml
stages:
- build-image
- send-app
build-image-job:
stage: build-image
script:
- mvn clean install
- VERSION=`date +%Y%m%d%H%M`
- echo "VERSION=$VERSION" > .version
- sh /root/gitlab-runner-biuld-image/docker-build.sh /home/gitlab-runner/builds/1bb4e598/0/development/rear-end/java/bcwl-cache bcwl-cache-test $VERSION
artifacts:
paths:
- .version
expire_in: 1 days
only:
- test
tags:
- k8s
send-app-job:
stage: send-app
script:
- source ./.version
- ssh root@master1 /root/auto-build-app/k8s-auto-build-deploy.sh bcwl-cache-test.yaml harbor.bcwl.com/bcwl/bcwl-cache-test:$VERSION bcwl-cache-test bcwl-cache-test 6377 2
only:
- test
tags:
- k8s
提交代码测试
k8s 查看部署的java
end ,谢谢观看,如果有写得不明白的地方欢迎指出,因为软件部署到不同的服务器,需要免密登录,目前是 sh 部署,在想优化配置方案。
GitHub 加速计划 / ha / harbor
23.24 K
4.68 K
下载
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
最近提交(Master分支:2 个月前 )
9e55afbb
pull image from registry.goharbor.io instead of dockerhub
Update testcase to support Docker Image Can Be Pulled With Credential
Change gitlab project name when user changed.
Update permissions count and permission count total
Change webhook_endpoint_ui
Signed-off-by: stonezdj <stone.zhang@broadcom.com>
Co-authored-by: Wang Yan <wangyan@vmware.com> 15 天前
3dbfd422
Signed-off-by: wang yan <wangyan@vmware.com> 15 天前
更多推荐
已为社区贡献1条内容
所有评论(0)