2023Jenkins连接k8s

58 篇文章 3 订阅
首先配置k8s config文件
1.方式获取k8s密钥
cat .kube/config 
2.导出方式或者密钥
kubectl config view --raw > k8s-config-admin 

请添加图片描述

pipeline {
    agent {
        kubernetes {
            yaml '''
                apiVersion: v1
                kind: Pod
                metadata:
                  labels:
                    some-label: devops
                spec:
                  containers:
                  - name: docker
                    image: docker:19.03-dind
                    securityContext:
                      privileged: true
                    tty: true
                    hostPID: true
                  - name: kubectl
                    image: registry.cn-shenzhen.aliyuncs.com/jbjb/dbs:kubectl-128
                    command:
                    - cat
                    tty: true
            '''
        }
    }
    environment {
        image = "registry.cn-shenzhen.aliyuncs.com/jbjb/dockers:$JOB_NAME-$BUILD_ID"
    }
    stages {
        stage('Get Code') {
            steps {
                
            checkout scmGit(branches: [[name: '*/lld']], extensions: [], userRemoteConfigs: [[credentialsId: 'll', url: 'http://git.xxx.com/new-test.git']])
            }
        }
        stage ("Docker Build") {
            steps {
                container('docker') {
                    script {
                        withDockerRegistry(credentialsId: 'mydocker', url: 'https://registry.cn-shenzhen.aliyuncs.com') {
                            docker.build("${image}").push()
                            
                        }
                    }
                }
            }
        }
        stage('Deploy to Kubernetes') {
            steps {
                container('kubectl') {
                withKubeCredentials(kubectlCredentials: [[credentialsId: 'kjbs', serverUrl: 'https://192.168.0.192:6443']]) {
                    sh 'kubectl get pods'
                }
            }
        }
    }
}
}

最后一个stps也可以换成这样

        stage('Deploy to Kubernetes') {
            steps {
                container('kubectl') {
                withKubeConfig(credentialsId: 'kjbs', serverUrl: 'https://192.168.0.192:6443') {
                    sh 'kubectl get pods'
                }
            }
        }
    }
withKubeCredentials 和 withKubeConfig 都是 Jenkins Pipeline 中用于指定 Kubernetes 凭据和配置的代码块,但它们之间有一些区别。
1 withKubeCredentials:
  • withKubeCredentials 用于指定 Kubernetes 的凭据信息,包括凭据 ID、服务器 URL 等。
  • 适用于使用单个凭据来执行与 Kubernetes 相关的操作。
  • 指定的凭据信息仅适用于该代码块内的步骤。
2 withKubeConfig:
  • withKubeConfig 用于指定完整的 Kubernetes 配置文件(kubeconfig)和凭据。
  • 可以使用自定义的 kubeconfig 文件,其中包含集群、用户、上下文等详细信息。
  • 适用于需要在多个步骤中重复使用相同的配置文件和凭据的情况。
  • withKubeConfig 代码块内的步骤都可以访问指定的 kubeconfig 文件和相关凭据。

根据你可以选择适合场景的代码块。如果只需要在单个步骤中使用简单的凭据信息,那么使用 withKubeCredentials 就足够了。而如果需要在多个步骤中使用相同的配置文件和凭据,使用 withKubeConfig 更为方便。

温馨提示debian12部署的k8s使用containerd运行是,jenkins 动态salve构建时,内核docker in docker时会错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值