K8S安装(五): 在K8S集群中部署Springboot 微服务应用示例 ,通过Ingress-Nginx对外暴露应用服务
本文主要模拟Springboot 程序的微服务部署,及CI&CD的Jenkins设计. 当前的K8S集群位一个master节点和三个workers 节点。
单机部署准备编译jar包的image,此处由于未注册镜像到docker-hub,所以只在服务器本地每个worker节点中build,有自己的docker-hub仓库的可用push上去。
首先编写构建jar包镜像所需的脚本Dockerfile文件
FROM openjdk:8-jre-alpine MAINTAINER Jack Lee USER root RUN mkdir -p /home/SpiderWiki WORKDIR /home/SpiderWiki COPY SpiderWiki-1.0-SNAPSHOT.jar SpiderWiki-1.0-SNAPSHOT.jar EXPOSE 8888 ENTRYPOINT ["java","-jar", "SpiderWiki-1.0-SNAPSHOT.jar"]
单机上build镜像和部署请用命令:
docker build -t test/spiderwiki:v1.0 .
echo "SpiderWiki image id :" $(docker images test/spiderwiki:v1.0 -q)
echo "Creating SpiderWiki container......"
docker run --name spiderwiki-web -p 8001:8888 -v /root/spiderwiki:/home/SpiderWiki -d test/spiderwiki:v1.0
echo "SpiderWiki container is running......"
K8S集群部署(启动3个副本pod):
则编写K8S 脚本,Ingress-SpiderWiki.yml :
--- apiVersion: apps/v1 kind: Deployment metadata: name: app-spiderwiki labels: app: app-spiderwiki spec: replicas: 3 selector: matchLabels: app: app-spiderwiki template: metadata: labels: app: app-spiderwiki spec: containers: - name: spiderwiki image: voith/spiderwiki:v1.0 imagePullPolicy: IfNotPresent ports: - containerPort: 8888 --- apiVersion: v1 kind: Service metadata: name: spiderwiki-service spec: type: ClusterIP selector: app: app-spiderwiki ports: - protocol: TCP port: 80 targetPort: 8888 --- apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress-web-spiderwiki namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: defaultBackend: service: name: spiderwiki-service port: number: 80 rules: - host: spiderwiki.com http: paths: - path: "/" pathType: Prefix backend: service: name: spiderwiki-service port: number: 80
脚本编写完成后,run起来:
deployment.sh
#!/bin/bash
set -e
echo "Starting to build SpiderWiki docker image......"
#docker build -t voith/spiderwiki:v1.0 .
#echo "SpiderWiki image id :" $(docker images voith/spiderwiki:v1.0 -q)
#echo "Creating SpiderWiki container......"
#docker run --name spiderwiki-web -p 8001:8888 -v /root/spiderwiki:/home/SpiderWiki -d voith/spiderwiki:v1.0
#echo "SpiderWiki container is running......"
kubectl apply -f Ingress-SpiderWiki.yml
kubectl get pods --all-namespaces -o wide
kubectl get svc --all-namespaces -o wide
kubectl get ingress --all-namespaces -o wide
使用CI&CD部署:
编写Jenkinsfile:
pipeline { agent any options { buildDiscarder(logRotator(numToKeepStr: '720')) } environment { GIT_REPO="https://github.com/xxxx/SpiderWiki.git" BRANCH="main" GIT_AUTH="xxxxxxx" DOCKER_REGISTRY_AUTH="xxxx" IMG_NAME="test/spiderwiki:v1.0" } stages { stage('Pulling from Git') { checkout([$class: 'GitSCM', branches: [[name: '${BRANCH}']], userRemoteConfigs: [[credentialsId: "${GIT_AUTH}", url: "${GIT_REPO}"]]]) } stage('Compiling SpiderWiki'){ sh 'sudo su root -c "mvn clean package -Dmaven.test.skip=true"' } stage('Building SpiderWiki Image'){ withCredentials([usernamePassword(credentialsId: "${DOCKER_REGISTRY_AUTH}", passwordVariable: 'PASSWORD', usernameVariable: 'USER_NAME')]) { sh """ echo ' FROM openjdk:8-jre-alpine MAINTAINER ming.lu USER root RUN mkdir -p /home/SpiderWiki WORKDIR /home/SpiderWiki COPY target/SpiderWiki-1.0-SNAPSHOT.jar SpiderWiki-1.0-SNAPSHOT.jar EXPOSE 8888 ENTRYPOINT ["java","-jar", "SpiderWiki-1.0-SNAPSHOT.jar"] '> Dockerfile docker build -t ${IMG_NAME} . docker login -u ${USER_NAME} -p '${PASSWORD}' docker push ${IMG_NAME} """ } } stage('Deplying to K8S'){ sh """ #!/bin/bash sudo su root -c "kubectl apply -f Ingress-SpiderWiki.yml" """ } stage('Verifying') { sh """ echo "checking if it's running" """ } } post { success { echo 'All activities have been performed successfully!' } failure { echo 'At least one activity has failed!' echo "Build time: ${new Date()}" echo "Build ID: ${env.BUILD_ID}" echo "Build URL: ${env.BUILD_URL}" emailext body: "" } unstable { echo 'The pipeline has been marked as unstable!' } changed { echo 'The state of the Pipeline has changed!' } } } }