Kubernetes 如何提交kubectl作业并以运行方式传递给用户

Kubernetes 如何提交kubectl作业并以运行方式传递给用户,kubernetes,kubectl,Kubernetes,Kubectl,我想在Kubernetes上运行一个容器,比如说image1 当我运行kubectl apply-f somePod.yml(它运行image1)时,如何与运行kubectl命令的用户一起启动映像 我仍然不确定我是否理解你的问题 但是,kubectl在创建作业时没有传递用户或服务帐户的选项: kubectl创建作业--帮助 Create a job with the specified name. Examples: # Create a job kubectl create job

我想在Kubernetes上运行一个容器,比如说image1


当我运行kubectl apply-f somePod.yml(它运行image1)时,如何与运行kubectl命令的用户一起启动映像

我仍然不确定我是否理解你的问题

但是,
kubectl
在创建作业时没有传递用户或服务帐户的选项:

kubectl创建作业--帮助

Create a job with the specified name.

Examples:
  # Create a job
  kubectl create job my-job --image=busybox

  # Create a job with command
  kubectl create job my-job --image=busybox -- date

  # Create a job from a CronJob named "a-cronjob"
  kubectl create job test-job --from=cronjob/a-cronjob

Options:
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or
map key is missing in the template. Only applies to golang and jsonpath output formats.
      --dry-run=false: If true, only print the object that would be sent, without sending it.
      --from='': The name of the resource to create a Job from (only cronjob is supported).
      --image='': Image name to run.
  -o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
      --save-config=false: If true, the configuration of current object will be saved in its
annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to
perform kubectl apply on this object in the future.
      --template='': Template string or path to template file to use when -o=go-template,
-o=go-template-file. The template format is golang templates
[http://golang.org/pkg/text/template/#pkg-overview].
      --validate=true: If true, use a schema to validate the input before sending it

Usage:
  kubectl create job NAME --image=image [--from=cronjob/name] -- [COMMAND] [args...] [flags]
[options]

Use "kubectl options" for a list of global command-line options (applies to all commands).
您可以在YAML定义中指定许多因素。例如,您可以在
pod
配置中创建
servicecomport
或指定
runAsUser
。但是,这需要一个定义文件,而不是运行级别为
kubectl

您可以找到如何使用
runAsUser
参数执行此操作

<>你也可以考虑使用。你有一篇文章可能对你有帮助。但是,您需要创建特定的
serviceCount
它看起来类似于:

apiVersion: v1
kind: Pod
metadata:
   name: pod-demo-sa
spec:
   serviceAccountName: demo-sa
   containers:
   — name: alpine
     image: alpine:3.9
     command:
     — "sleep"
     — "10000"
如果这将是一些实验室或实践,您也可以考虑使用创建定制的docker图像

不幸的是,以前的选项是硬编码的。其他解决方案将需要一个特定的scritp和许多管道

此外,正如您在标题中提到的,要将一些值传递给您可以使用的配置,这是不可能的。请参阅以下解释:

在大多数情况下,工作都会产生豆荚,因此我在解释中使用豆荚。
对于作业来说,这意味着YAML文件有点不同

$ kubectl explain job.spec.
$ kubectl explain job.spec.template.spec
用户使用

吊舱正在使用运行。无法从用户帐户运行pod

注意:在最新版本中,
spec.servicecomport
spec.servicecomportname

但是,您可以通过在Pod容器内运行
kubectl
或从Pod容器内向Kubernetes
api服务器发出
curl
请求来使用用户帐户凭据。
使用是最方便的方法

您还可以做什么来区分群集中的用户:

  • 为每个用户创建一个
  • 在该命名空间中创建默认服务帐户
这样,如果用户创建Pod时未指定
spec.ServiceAccountName
,默认情况下,它将使用Pod命名空间中的
default
服务帐户

您甚至可以为
默认服务帐户
设置与
用户帐户
相同的权限。唯一的区别是
服务帐户
存在于特定的命名空间中

如果需要为所有用户使用相同的命名空间,可以使用以下命令行参数为每个用户设置正确的
serviceAccountName
(假设您的服务帐户与用户同名):

$ cat testchart/templates/job.yaml 
...
   serviceAccountName: {{ .Values.saname }}
...

$ export SANAME=$(kubectl config view --minify -o jsonpath='{.users[0].name}')
$ helm template ./testchart --set saname=$SANAME
---
# Source: testchart/templates/job.yaml
...
   serviceAccountName: kubernetes-admin
...

您还可以用同样的方式为每个用户指定
名称空间

您能详细说明一下吗?您希望运行能够执行
kubectl
的容器(您可以使用Dockerfile使用kubectl创建自己的映像)。那么您想作为运行作业的人员在该容器内执行命令吗?或者您只想将用户名作为变量传递给容器?你使用的是纯金属还是Prem?什么是“用户”?(每个pod中的每个容器都有自己的
/etc/passwd
文件,可以在具有不同主机passwd文件的不同节点上运行;pod以这种方式共享文件也很少见。)用户是一个在laptopsHi上安装了kubectl的人,感谢您的回复,实际上,每个容器都在提交kubectl命令的用户下运行。这和hadoop的模拟功能是一样的。(doAs())不幸的是,Kubernetes没有内置的那种功能。这是可能的,但您需要更改
serviceAccountName
(可能使用一些使用部署的脚本)。将此标记为正确的响应是不可能的,这是对我最好的解释