2023Jenkins连接k8s

编程入门 行业动态 更新时间:2024-10-10 01:17:04

2023<a href=https://www.elefans.com/category/jswz/34/1770690.html style=Jenkins连接k8s"/>

2023Jenkins连接k8s

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

pipeline {agent {kubernetes {yaml '''apiVersion: v1kind: Podmetadata:labels:some-label: devopsspec:containers:- name: dockerimage: docker:19.03-dindsecurityContext:privileged: truetty: truehostPID: true- name: kubectlimage: registry-shenzhen.aliyuncs/jbjb/dbs:kubectl-128command:- cattty: true'''}}environment {image = "registry-shenzhen.aliyuncs/jbjb/dockers:$JOB_NAME-$BUILD_ID"}stages {stage('Get Code') {steps {checkout scmGit(branches: [[name: '*/lld']], extensions: [], userRemoteConfigs: [[credentialsId: 'll', url: '.git']])}}stage ("Docker Build") {steps {container('docker') {script {withDockerRegistry(credentialsId: 'mydocker', url: '') {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时会错

更多推荐

2023Jenkins连接k8s

本文发布于:2023-12-05 03:10:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1662883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Jenkins   k8s

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!