kubernetes部署后如何访问应用程序

时间:2020-01-16 13:21:09

标签: jenkins kubernetes

我是kubernetes工具的新手,我正尝试使用docker + kubernetes部署Angular应用程序,这里是下面的Jenkins脚本。

stage('Deploy') {
       container('kubectl') {        
            withCredentials([kubeconfigFile(credentialsId: 'KUBERNETES_CLUSTER_CONFIG', variable: 'KUBECONFIG')]) {
            def kubectl
               kubectl = "kubectl --kubeconfig=${KUBECONFIG} --context=demo"
               echo 'deployment to PRERELEASE!'
               sh "kubectl config get-contexts"
               sh "kubectl -n demo get pods"
               sh  "${kubectl} apply -f ./environment/pre-release -n=pre-release"
               } 
            }
            }
    }

请找到以下詹金斯输出

/home/jenkins/agent/workspace/DevOps-CI_future-master-fix
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] container
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of $KUBECONFIG
[Pipeline] {
[Pipeline] echo
deploy to deployment!!
[Pipeline] echo
deploy to PRERELEASE!
[Pipeline] sh
+ kubectl config get-contexts
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
*         demo                          kubernetes   kubernetes-admin   demo
          kubernetes-admin@kubernetes   kubernetes   kubernetes-admin   
[Pipeline] sh
+ kubectl -n demo get pods
NAME                                                      READY   STATUS    RESTARTS   AGE
worker-f99adee3-dedd-46ca-bc0d-6b24391e5865-qkd47-mwl3v   5/5     Running   0          26s
[Pipeline] sh
+ kubectl '--kubeconfig=****' '--context=demo' apply -f ./environment/pre-release '-n=pre-release'
deployment.apps/frontend-deploy unchanged
service/frontend unchanged
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

现在的问题是,在部署之后,我无法使用以下命令在两台计算机的主计算机中看到吊舱和部署,请您帮我成功部署后如何访问应用程序。

kubectl get pods
kubectl get services
kubectl get deployments

2 个答案:

答案 0 :(得分:2)

您正在运行pre-release时将名称空间设置为"${kubectl} apply -f ./environment/pre-release -n=pre-release"

要在此命名空间中获取pod,请使用:kubectl get pods -n pre-release

命名空间是一种在单个物理Kubernetes集群中分离不同虚拟集群的方法。有关更多详细信息,请参见https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/

答案 1 :(得分:2)

运行以下命令时,您正在使用pre-release选项在名为-n的命名空间中创建资源。

kubectl '--kubeconfig=****' '--context=demo' apply -f ./environment/pre-release '-n=pre-release'
deployment.apps/frontend-deploy unchanged

您需要列出相同命名空间中的资源。

kubectl get pods -n pre-release
kubectl get services -n pre-release
kubectl get deployments -n pre-release

默认情况下,kubectl将在default名称空间中执行请求的操作。如果要将当前名称空间设置为pre-release,而不必在每个kubectl命令后附加-n pre-release,则可以运行以下命令:

kubectl config set-context --current --namespace=pre-release