Run a single kubectl command for a specific project and cluster?

孤街浪徒 提交于 2021-01-07 06:22:15

问题


Background

We're using Jenkins to deploy a new version of a Kubernetes (k8s) replication controller to our test or prod cluster. The test and prod (k8s) clusters are located under different (google cloud platform) projects. We have configured two profiles for our gcloud SDK on Jenkins, one for test (test-profile) and one for prod (prod-profile). We have defined a managed script in Jenkins that performs the rolling update for our replication controller. The problems is that I cannot find a way to control to which project I want to target the kubectl rolling-update command (you can specify which cluster but not which project afict). So right now our script that does the rolling update to our test server looks something like this:

gcloud config configurations activate test-profile && kubectl rolling-update ...

While this works it could be extremely dangerous if two jobs run concurrently for different environments. Say that job 1 targets the test environment and job 2 targets prod. If job2 switches the active profile to "prod-profile" before job 1 has executed its rolling-update command job 1 will target to wrong project and in worse case update the wrong replication controller (if the clusters have the same name).

Question

Is there a way to specify which project that a kubectl command is targeting (for example during a rolling update) that is safe to run concurrently?


回答1:


You can pass the --cluster= or --context= flags to kubectl to set a single run. For example, if I have two clusters in my ~/.kube/config "foo" and "bar":

$ kubectl --cluster=foo get pods
NAME              READY     STATUS    RESTARTS   AGE
foo-ht1qh   1/1       Running   0          3h
foo-wf8f4   1/1       Running   0          3h
foo-yvgpd   1/1       Running   0          3h

vs

$ kubectl --cluster=bar get pods
NAME              READY     STATUS    RESTARTS   AGE
bar-de4h7   1/1       Running   0          9h
bar-c4g03   1/1       Running   0          9h
bar-2sprd   1/1       Running   0          9h



回答2:


You may use gcloud config set project yourProject to set the project property. See https://cloud.google.com/sdk/gcloud/reference/config/set



来源:https://stackoverflow.com/questions/33942709/run-a-single-kubectl-command-for-a-specific-project-and-cluster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!