简体   繁体   English

如何在Jenkinsfile中使用kubectl sh在Kubernetes上进行部署

[英]How to deploy on Kubernetes using kubectl sh in Jenkinsfile

I'm trying to create a pipeline that is triggered after a commit in git. 我正在尝试创建在git中提交后触发的管道。 I have installed minikube using kvm2 driver. 我用kvm2驱动程序安装了minikube。 However I am not sure if everything is setup as it should. 但是我不确定是否所有设置都应该如此。 Jenkins has been installed previously and it doesn't run on kubernetes. Jenkins先前已安装,并且无法在kubernetes上运行。

Below is my Jenkinsfile. 下面是我的Jenkins文件。

pipeline {

    environment {
        ....
    }

    agent any

    stages {

        stage ('Clone') {
            ....
        }

        stage('Build') {
            ....
        }

        stage(‘Containerize’) {
            .... build images with docker
        }

        stage('Deploy Image') {
            .... push images on Docker hub
        }

        stage('Deploy Application') {
            steps {
                // Create namespace if it doesn't exist
                sh("kubectl get ns development || kubectl create ns development")
               .... 
            }
        }
    }
}

I also configured Kubernetes as Cloud. 我还将Kubernetes配置为Cloud。 I've added kubernetes URL as the one show in ./kube/config kubernetes server certificate as the content of /.minikube/ca.crt credentials created with the 我添加了kubernetes URL作为./kube/config kubernetes服务器证书中的一个显示为/.minikube/ca.crt凭据创建的内容

sudo openssl pkcs12 -export -out kubernetes.pfx -inkey apiserver.key -in apiserver.crt -certfile ca.crt -passout pass:jenkins

The problem I have is this: 我遇到的问题是:

[Pipeline] { (Deploy Application)
[Pipeline] sh
+ kubectl get ns development
The connection to the server localhost:8080 was refused - did you specify the right host or port?

Is this something related to RBAC? 这与RBAC有关吗? I tried to use the Secret Token using kubectl create serviceaccount jenkins and copy the token but when I tested the connection it didn't work. 我尝试通过kubectl create serviceaccount jenkins使用Secret Token并复制令牌,但是当我测试连接时不起作用。

I'm not sure if this is the right way to deploy on Kubernetes using a pipeline. 我不确定这是否是使用管道在Kubernetes上部署的正确方法。 Please suggest if there's a better way to deploy my microservices. 请建议是否有更好的方法来部署我的微服务。

  • There are alot of many ways to deploy to kubernetes , use the one that works for you better 有很多方法可以部署到kubernetes,使用一个更适合你的方法

  • the above error is related to the address/API endpoint of the cluster , so it could not found your cluster , you need to provide the kubeconfig in some suitable location or use some other way of connecting to cluster 上述错误与集群的地址/ API端点有关,因此无法找到您的集群,您需要在某个合适的位置提供kubeconfig或使用其他一些方式连接到集群

Something like: 就像是:

kubectl --kubeconfig=/path/to/kubeconfig-file get ns development || kubectl --kubeconfig=/path/to/kubeconfig-file  create ns development")

As always , please try and run those commands manually and make sure they work before putting them in automation/pipeline 与往常一样,请尝试手动运行这些命令,并确保它们在将它们放入自动化/管道之前工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM