目录
1、下载 Kubernetes Continuous Deploy 插件
2、设置k8s凭证
3、在IDEA里配置文件
1、下载 Kubernetes Continuous Deploy 插件
2、设置k8s凭证
在服务器里找到config文件
在凭证里面添加
获取k8s的id号
编写脚本文件
def git_address = "http://192.168.100.128:82/kgc/tensquare_back.git"
def git_auth = "1162668b-deea-4c49-9a7d-eb7e6aec3ebe"
//构建版本的名称
def tag = "latest"
//Harbor私服地址
def harbor_url = "192.168.100.133:85"
//Harbor的项目名称
def harbor_project_name = "tensquare"
//Harbor的凭证
def harbor_auth = "4e2c8472-a8bf-4d51-912a-9a12ebdda722"
//k8s的凭证
def k8s_auth="9190e86a-b328-48ae-9d88-18bb2cb4644d"
//定义k8s-barbor的凭证
def secret_name="registry-auth-secret"
podTemplate(label: 'jenkins-slave', cloud: 'kubernetes', containers: [
containerTemplate(
name: 'jnlp',
image: "192.168.100.133:85/library/jenkins-slave-maven:latest"
),
containerTemplate(
name: 'docker',
image: "docker:stable",
ttyEnabled: true,
command: 'cat'
),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
nfsVolume(mountPath: '/usr/local/apache-maven/repo', serverAddress: '192.168.100.130' , serverPath: '/opt/nfs/maven'),
],
)
{
node("jenkins-slave"){
// 第一步
stage('pull code'){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
// 第二步
stage('make public sub project'){
//编译并安装公共工程
sh "mvn -f tensquare_common clean install"
}
// 第三步
stage('make image'){
//把选择的项目信息转为数组
def selectedProjects = "${project_name}".split(',')
for(int i=0;i<selectedProjects.size();i++){
//取出每个项目的名称和端口
def currentProject = selectedProjects[i];
//项目名称
def currentProjectName = currentProject.split('@')[0]
//项目启动端口
def currentProjectPort = currentProject.split('@')[1]
//定义镜像名称
def imageName = "${currentProjectName}:${tag}"
//编译,构建本地镜像
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
container('docker') {
//给镜像打标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_project_name}/${imageName}"
//登录Harbor,并上传镜像
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')])
{
//登录
sh "docker login -u ${username} -p ${password} ${harbor_url}"
//上传镜像
sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}"
}
//删除本地镜像
sh "docker rmi -f ${imageName}"
sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}"
}
def deploy_image_name = "${harbor_url}/${harbor_project_name}/${imageName}"
//部署到K8S
sh """
sed -i 's#\$IMAGE_NAME#${deploy_image_name}#' ${currentProjectName}/deploy.yml
sed -i 's#\$SECRET_NAME#${secret_name}#' ${currentProjectName}/deploy.yml
"""
kubernetesDeploy configs: "${currentProjectName}/deploy.yml", kubeconfigId: "${k8s_auth}"
}
}
}
}
3、在IDEA里配置文件
deploy.yml内容如下
---
apiVersion: v1
kind: Service
metadata:
name: eureka
labels:
app: eureka
spec:
type: NodePort
ports:
- port: 10086
name: eureka
targetPort: 10086
selector:
app: eureka
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: eureka
spec:
serviceName: "eureka"
replicas: 2
selector:
matchLabels:
app: eureka
template:
metadata:
labels:
app: eureka
spec:
imagePullSecrets:
- name: $SECRET_NAME
containers:
- name: eureka
image: $IMAGE_NAME
ports:
- containerPort: 10086
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: EUREKA_SERVER
value: "http://eureka-0.eureka:10086/eureka/,http://eureka- 1.eureka:10086/eureka/"
- name: EUREKA_INSTANCE_HOSTNAME
value: ${MY_POD_NAME}.eureka
podManagementPolicy: "Parallel"
在修改application.yml配置文件更改如下
server:
port: ${PORT:10086}
spring:
application:
name: eureka
eureka:
server:
# 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eviction-interval-timer-in-ms: 5000
enable-self-preservation: false
use-read-only-response-cache: false
client:
# eureka client间隔多久去拉取服务注册信息 默认30s
registry-fetch-interval-seconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVER:http://127.0.0.1:${server.port}/eureka/}
instance:
# 心跳间隔时间,即发送一次心跳之后,多久在发起下一次(缺省为30s)
lease-renewal-interval-in-seconds: 5
# 在收到一次心跳之后,等待下一次心跳的空档时间,大于心跳间隔即可,即服务续约到期时间(缺省为90s)
lease-expiration-duration-in-seconds: 10
instance-id: ${EUREKA_INSTANCE_HOSTNAME:${spring.application.name}}:${server.port}@${random.l ong(1000000,9999999)}
hostname: ${EUREKA_INSTANCE_HOSTNAME:${spring.application.name}}
修改完成后,提交项目
这个时候构建会显示报错!!k8s访问harbor需要密钥权限
需要在所有的k8s主机上操作
[root@localhost ~]# docker login -u zch -p Zch12345 192.168.100.133:85 #先登录harbor仓库
在master主机上操作
[root@k8s-master ~]# kubectl create secret docker-registry registry-auth-secret --docker-server=192.168.100.133:85 --docker-username=zch --docker-password=Zch12345 --docker-email=zch@qq.com
在master节点查看
设置完成后开始构建
查看节点状态
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
eureka-0 1/1 Running 0 7m28s
eureka-1 1/1 Running 0 7m28s
nfs-client-provisioner-6858cdf56-6ltmd 1/1 Running 0 169m
查看暴露的端口号
[root@k8s-master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
eureka NodePort 10.1.147.207 <none> 10086:30272/TCP 8m30s
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 5h48m
访问两个node节点的30272端口
部署服务网关
更改配置文件中的eureka集群地址
修改服务网关zuul的地址
在zuul目录下创建配置文件deploy.yml如下
---
apiVersion: v1
kind: Service
metadata:
name: zuul
labels:
app: zuul
spec:
type: NodePort
ports:
- port: 10020
name: zuul
targetPort: 10020
selector:
app: zuul
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: zuul
spec:
serviceName: "zuul"
replicas: 2
selector:
matchLabels:
app: zuul
template:
metadata:
labels:
app: zuul
spec:
imagePullSecrets:
- name: $SECRET_NAME
containers:
- name: zuul
image: $IMAGE_NAME
ports:
- containerPort: 10020
podManagementPolicy: "Parallel"
需要手动上传父工程依赖到NFS的Maven共享仓库目录中
提交项目
构建服务网关zuul
构建成功后查看
认证中心和活动微服务的构建方式与服务网关构建相同
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
admin-0 1/1 Running 0 6m34s
admin-1 1/1 Running 0 6m34s
eureka-0 1/1 Running 0 69m
eureka-1 1/1 Running 0 69m
gathering-0 1/1 Running 0 55s
gathering-1 1/1 Running 0 55s
nfs-client-provisioner-6858cdf56-6ltmd 1/1 Running 0 3h51m
zuul-0 1/1 Running 0 25m
zuul-1 1/1 Running 0 25m
|