K8S发布系统-发布访问配置

创建任务-发布至k8s

在内网的一台Web服务器上创建K8S的部署yaml模板:

cd /data/www/
mkdir k8s
cd k8s
vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: CI_PROJECT_NAME
  namespace: NAMESPACE
spec:
  replicas: REPLICAS_NUM
  selector:
    matchLabels:
      app: CI_PROJECT_NAME
  template:
    metadata:
      labels:
        app: CI_PROJECT_NAME
    spec:
      containers:
      - env:
          - name: JVM_XMS
            value: JVM_XMS_VALUE
          - name: JVM_XMX
            value: JVM_XMX_VALUE
          - name: PROFILE_ACTIVE
            value: SPRING_PROFILE
        name: CI_PROJECT_NAME
        image: REPOSITORY_BASE/CI_PROJECT_NAME:BUILD_IMAGE_VERSION
        imagePullPolicy: Always
        resources:
          requests:
            cpu: REQUESTS_CPU
            memory: REQUESTS_MEM
          #limits:
            #cpu: LIMITS_CPU
            #memory: 20240Mi
      dnsPolicy: ClusterFirst
      restartPolicy: Always
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  minReadySeconds: MINREADSECONDS
vim svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: CI_SERVICE_NAME
  namespace: NAMESPACE
  labels:
    app: CI_SERVICE_NAME
spec:
  ports:
  - port: SVC_PORT
    protocol: TCP
    targetPort: TARGETPORT
  selector:
    app: CI_PROJECT_NAME

在Jenkins服务器上,为jenkins用户添加K8S群集内SSH密钥(若服务部署于k8s之内则可以不执行此操作):

mkdir /data/jenkins/.ssh
cd /data/jenkins/.ssh/
chown -R jenkins.jenkins /data/jenkins/.ssh
chmod 700 /data/jenkins/.ssh
chmod 600 /data/jenkins/.ssh/id_rsa

创建Pipeline任务:

node{
    // Pipeline方式, 提交参数, 将build的Docker镜像部署至K8S; 可用于外部调用
  
  String HarborUrl='nw-harbor.zongs365.cc'
    String HarborUser='admin'
    String HarborPasswd='Nw-Harbor123'
  String TemplateUrl='http://172.16.220.105/k8s'
  String Temp_Depoly='deployment.yaml'
  String Temp_SVC='svc.yaml'
  String KubernetHost='[email protected]'
  String Yaml_Path='/root/k8s_deploy'
  String SSH_KeyFile='/data/jenkins/.ssh/id_rsa'
  
  properties([
    parameters([
      string(name: 'Group', defaultValue: 'zongs-nw', description: 'Pod拉取用: 填写项目组名称,Harbor项目组;同构建任务', trim: false),
      string(name: 'Project_name', defaultValue: 'demo', description: '部署配置用,Pod拉取用: 项目名称;同构建任务', trim: false),
      string(name: 'Project_tag', defaultValue: 'nw-2', description: '部署配置用,Pod拉取用: 项目tag(镜像构建的版本);同构建任务', trim: false),
      string(name: 'NAMESPACE', defaultValue: 'default', description: '部署配置用: 所在命名空间', trim: false),
      string(name: 'REPLICAS_NUM', defaultValue: '1', description: '部署配置用: 运行的 Pod 数目', trim: false),
      string(name: 'JVM_XMS_VALUE', defaultValue: '256m', description: '部署配置用: JAVA 选项 Xms', trim: false),
      string(name: 'JVM_XMX_VALUE', defaultValue: '256m', description: '部署配置用: JAVA 选项 Xmx', trim: false),
      string(name: 'SPRING_PROFILE', defaultValue: 'dev', description: '部署配置用: spring.profiles.active', trim: false),
      string(name: 'REQUESTS_CPU', defaultValue: '50m', description: '部署配置用: 资源CPU需求', trim: false),
      string(name: 'REQUESTS_MEM', defaultValue: '256Mi', description: '部署配置用: 资源Memory需求,与 JVM_XMX_VALUE 一致即可', trim: false),
      string(name: 'MINREADSECONDS', defaultValue: '5', description: '部署配置用: 新创建的Pod状态为Ready持续此时间后认为Available', trim: false),
      string(name: 'CI_SERVICE_NAME', defaultValue: "${Project_name}-svc", description: '部署配置用: 服务名称', trim: false),
      choice(name: 'IS_SVC', choices: ['No', 'Yes'], description: '部署配置用: 是否创建服务(为 Yes 部署svc.yaml)'),
      string(name: 'SVC_PORT', defaultValue: '80', description: '部署配置用: 服务本身监听端口', trim: false),
      string(name: 'TARGETPORT', defaultValue: '8080', description: '部署配置用: 服务后端项目端口(即项目本身所监听的端口)', trim: false)
    ])
  ])
  
  if(SSH_KeyFile){
    if(!fileExists("${SSH_KeyFile}")){
      error "连接密钥文件 ${SSH_KeyFile} 不存在"
    }
    SSH_Command="ssh -i ${SSH_KeyFile} -p 22 -o StrictHostKeyChecking=no"
    SCP_Command="scp -i ${SSH_KeyFile} -P 22 -o StrictHostKeyChecking=no"
  }else{
    SSH_Command="ssh -p 22 -o StrictHostKeyChecking=no"
    SCP_Command="scp -P 22 -o StrictHostKeyChecking=no"
  }
  
  
  dir("${env.WORKSPACE}"){
    stage('群集主机SSH验证'){
      echo "验证k8s群集内主机 ${KubernetHost} 是否可连通,并确保YAML文件存放目录 ${Yaml_Path}"
      sh "${SSH_Command} ${KubernetHost} mkdir -p ${Yaml_Path}"
    }
    
    stage('K8S-Deployment 配置'){
      echo "从 ${TemplateUrl} 拉取 K8S-Deployment 模板文件"
      sh "wget ${TemplateUrl}/${Temp_Depoly} -O ${Project_name}-deploy.yaml"
      
      echo "按参数配置修改模板文件"
      sh "sed -i 's#CI_PROJECT_NAME#${Project_name}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#NAMESPACE#${NAMESPACE}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#REPLICAS_NUM#${REPLICAS_NUM}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#JVM_XMS_VALUE#${JVM_XMS_VALUE}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#JVM_XMX_VALUE#${JVM_XMX_VALUE}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#SPRING_PROFILE#${SPRING_PROFILE}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#REPOSITORY_BASE#${HarborUrl}/${Group}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#BUILD_IMAGE_VERSION#${Project_tag}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#REQUESTS_CPU#${REQUESTS_CPU}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#REQUESTS_MEM#${REQUESTS_MEM}#g' ${Project_name}-deploy.yaml"
      sh "sed -i 's#MINREADSECONDS#${MINREADSECONDS}#g' ${Project_name}-deploy.yaml"
      
      echo "将修改的文件 ${Project_name}-deploy.yaml 传送至 k8s 群集内部机器 ${KubernetHost}"
      sh "${SCP_Command} ${Project_name}-deploy.yaml ${KubernetHost}:${Yaml_Path}"
    }
    
    if(IS_SVC == 'Yes'){
      stage('K8S-SVC 配置(可选项)'){
        echo "从 ${TemplateUrl} 拉取 K8S-SVC 模板文件"
        sh "wget ${TemplateUrl}/${Temp_SVC} -O ${Project_name}-svc.yaml"
        
        echo "按参数配置修改模板文件"
        sh "sed -i 's#CI_SERVICE_NAME#${CI_SERVICE_NAME}#g' ${Project_name}-svc.yaml"
        sh "sed -i 's#CI_PROJECT_NAME#${Project_name}#g' ${Project_name}-svc.yaml"
        sh "sed -i 's#NAMESPACE#${NAMESPACE}#g' ${Project_name}-svc.yaml"
        sh "sed -i 's#SVC_PORT#${SVC_PORT}#g' ${Project_name}-svc.yaml"
        sh "sed -i 's#TARGETPORT#${TARGETPORT}#g' ${Project_name}-svc.yaml"
        
        echo "将修改的文件 ${Project_name}-svc.yaml 传送至 k8s 群集内部机器 ${KubernetHost}"
        sh "${SCP_Command} ${Project_name}-svc.yaml ${KubernetHost}:${Yaml_Path}"
      }
    }
    
    stage('部署至K8S'){
      echo "部署应用 ${Project_name}-deploy.yaml"
      sh "${SSH_Command} ${KubernetHost} kubectl apply -f ${Yaml_Path}/${Project_name}-deploy.yaml"
      if(IS_SVC == 'Yes'){
        echo "部署服务 ${Project_name}-svc.yaml"
        sh "${SSH_Command} ${KubernetHost} kubectl apply -f ${Yaml_Path}/${Project_name}-svc.yaml"
      }
    }
  }
}

执行:

部署成功,验证:

kubectl get deployments.apps
kubectl get svc

发表评论

error: Content is protected !!