为特定项目和集群运行单个kubectl命令? [英] Run a single kubectl command for a specific project and cluster?

查看:69
本文介绍了为特定项目和集群运行单个kubectl命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我们正在使用Jenkins将新版本的Kubernetes(k8s)复制控制器部署到我们的测试或产品集群中.测试和生产(k8s)群集位于不同的(谷歌云平台)项目下.我们在Jenkins上为gcloud SDK配置了两个配置文件,一个配置文件用于测试(测试配置文件),另一个配置文件用于prod(产品配置文件).我们在Jenkins中定义了一个托管脚本,该脚本为我们的复制控制器执行滚动更新.问题是我找不到控制kubectl rolling-update命令目标的项目的方法(您可以指定哪个集群,但不能指定哪个项目).因此,现在,用于对测试服务器进行滚动更新的脚本如下所示:

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

尽管这可行,但是如果两个作业在不同的环境中同时运行,则可能会非常危险.假设作业1针对测试环境,而作业2针对产品.如果在作业1执行其rolling-update命令之前,作业2将活动配置文件切换为产品配置文件",则作业1将以错误的项目为目标,并且在更糟的情况下,请更新错误的复制控制器(如果群集具有相同的名称).

问题

有没有一种方法可以指定kubectl命令针对的项目(例如在滚动更新期间)可以安全地同时运行?

解决方案

您可以将--cluster=--context=标志传递给kubectl以设置单个运行.例如,如果我在〜/.kube/config中有两个集群"foo"和"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

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?

解决方案

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

这篇关于为特定项目和集群运行单个kubectl命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆