如何使用 jenkins 部署有状态应用程序

how to deploy stateful applications using jenkins

长话短说;博士 有谁知道允许部署有状态应用程序的 Jenkins 插件

我正在使用 Jenkins 通过多分支 CI/CD 管道自动执行开发过程。 我正在尝试部署有状态应用程序。 我正在使用“Kubernetes Continuous Deploy Version1.0.0”插件进行部署 管道按预期正确运行,除了一部分。

状态为绿色,但是,有状态集未在 k8s 集群上启动或更新。 当我检查状态时,我的插件似乎跳过了我提供的 YAML。 “将应用程序部署到 Kubernetes”阶段的状态:

            Starting Kubernetes deployment
        Loading configuration: /var/lib/jenkins/workspace/algo-1_master/Preprod/Trader/statefulset_algo_1.yaml
        Skipped unsupported resource: StatefulSet(apiVersion=apps/v1, kind=StatefulSet,
     metadata=ObjectMeta(annotations={}, clusterName=null, creationTimestamp=null, 
    deletionGracePeriodSeconds=null, deletionTimestamp=null, finalizers=[], 
    generateName=null, generation=null, initializers=null, labels={},
     name=algo-1, namespace=1-algo-prod, ownerReferences=[],
     resourceVersion=null, selfLink=null, uid=null, additionalProperties={}),
     spec=StatefulSetSpec(podManagementPolicy=null, replicas=4, revisionHistoryLimit=null, 
    selector=LabelSelector(matchExpressions=[], 
    matchLabels={app=algo-1}, additionalProperties={}), serviceName=algo-1
    , template=PodTemplateSpec(metadata=ObjectMeta(annotations={},
         clusterName=null, creationTimestamp=null, deletionGracePeriodSeconds=null, deletionTimestamp=null, finalizers=[], generateName=null, generation=null,
         initializers=null, labels={app=algo-1}, name=null, namespace=null, ownerReferences=[],
 resourceVersion=null, selfLink=null,
     uid=null, additionalProperties={}), spec=PodSpec(activeDeadlineSeconds=null, affinity=null, automountServiceAccountToken=null, containers=[Container(args=[], command=[], env=[EnvVar(name=PODNAME, value=null,
         valueFrom=EnvVarSource(configMapKeyRef=null, fieldRef=ObjectFieldSelector(apiVersion=null, 
        fieldPath=metadata.name, additionalProperties={}), resourceFieldRef=null,
 secretKeyRef=null, additionalProperties={}), additionalProperties={}), EnvVar(name=API_ID, value=null, 
        valueFrom=EnvVarSource(configMapKeyRef=null, fieldRef=null, resourceFieldRef=null,
         secretKeyRef=SecretKeySelector(key=api-id, name=alpaca-api, optional=null,
 additionalProperties={}), additionalProperties={}), additionalProperties={}),
         EnvVar(name=API_KEY, value=null, valueFrom=EnvVarSource(configMapKeyRef=null, fieldRef=null, resourceFieldRef=null, 
secretKeyRef=SecretKeySelector(key=secret-key, name=alpaca-api, optional=null, additionalProperties={}), 
        additionalProperties={}), additionalProperties={})], envFrom=[], image=arieltar/algo-1,
 imagePullPolicy=Always, lifecycle=null, livenessProbe=null, name=algo-1, ports=[], readinessProbe=null, resources=null, securityContext=null,
         stdin=null, stdinOnce=null, terminationMessagePath=null, terminationMessagePolicy=null,
 tty=null, volumeDevices=[], volumeMounts=[], workingDir=null, additionalProperties={})], dnsConfig=null, 
        dnsPolicy=null, hostAliases=[], hostIPC=null, hostNetwork=null, hostPID=null,
 hostname=null, imagePullSecrets=[], initContainers=[], nodeName=null, nodeSelector={},
 priority=null, priorityClassName=null, restartPolicy=null, schedulerName=null, securityContext=null, 
        serviceAccount=null, serviceAccountName=null, subdomain=null, terminationGracePeriodSeconds=null, tolerations=[], volumes=[], additionalProperties={}),
 additionalProperties={}), updateStrategy=null, volumeClaimTemplates=[], additionalProperties={}), status=null, additionalProperties={})
            Finished Kubernetes deployment

我的流水线如下

pipeline {
    environment {
      dockerimagename = "arieltar/algo-1"
      dockerImage = ""
    }

    agent any

    stages {

      stage('Checkout Source') {
        steps {
          git 'https://github.com/finance-dataspider/spiderdoc.git'
        }
      }

      stage('Build image') {
        steps{
          script {
            dockerImage = docker.build(dockerimagename,"./Preprod/Trader/")
          }
        }
      }

      stage('Pushing Image') {
        environment {
                registryCredential = 'dockerhub-reg'
            }
        steps{
          script {
            docker.withRegistry( 'https://registry.hub.docker.com', registryCredential ) {
              dockerImage.push("latest")
            }
          }
        }
      }

      stage('Deploying App to Kubernetes') {
        steps {
          script {
            kubernetesDeploy(configs: "Preprod/Trader/statefulset_algo_1.yaml", kubeconfigId: "kubernetes")
          }
        }
      }

    }

  }

简单的解决方案,如果插件不支持有状态集并且您想部署它,请不要使用它。

只需将 kubectl 安装到 bash 中并直接使用它来应用 YAML

stage('Deploy Image') {
      steps{
        script {
          docker.withRegistry( '', registryCredential ) {
            dockerImage.push("$BUILD_NUMBER")
             dockerImage.push('latest')

          }
        }
      }
    }
    stage('Deploy to K8s') {
      steps{
        script {
          sh "sed -i 's,TEST_IMAGE_NAME,harshmanvar/node-web-app:$BUILD_NUMBER,' deployment.yaml"
          sh "cat deployment.yaml"
          sh "kubectl --kubeconfig=/home/ec2-user/config get pods"
          sh "kubectl --kubeconfig=/home/ec2-user/config apply -f deployment.yaml"
        }
      }
    }