gpt4 book ai didi

kubernetes - 在 kubernetes cronjob 中指定角色

转载 作者:行者123 更新时间:2023-12-02 12:13:58 27 4
gpt4 key购买 nike

我们有一个运行 istio 代理的 kubernetes 集群。
起初我创建了一个 cronjob,它从数据库中读取并在找到时更新一个值。它工作得很好。

然后发现我们已经有了一个更新数据库的服务,所以我将数据库代码更改为服务调用。

conn := dial.Service("service:3550", grpc.WithInsecure())
client := protobuf.NewServiceClient(conn)

client.Update(ctx)

但是 istio 拒绝了带有 RBAC 错误的调用。它只是拒绝,并没有说明原因。

是否可以向 cronjob 添加角色?我们怎么做?

mTLS 网状策略是 PERMISSIVE。

Kubernetes 版本是 1.17,istio 版本是 1.3
API Version:  authentication.istio.io/v1alpha1
Kind: MeshPolicy
Metadata:
Creation Timestamp: 2019-12-05T16:06:08Z
Generation: 1
Resource Version: 6578
Self Link: /apis/authentication.istio.io/v1alpha1/meshpolicies/default
UID: 25f36b0f-1779-11ea-be8c-42010a84006d
Spec:
Peers:
Mtls:
Mode: PERMISSIVE

cronjob yaml
Name:                          cronjob
Namespace: serve
Labels: <none>
Annotations: <none>
Schedule: */30 * * * *
Concurrency Policy: Allow
Suspend: False
Successful Job History Limit: 1
Failed Job History Limit: 3
Pod Template:
Labels: <none>
Containers:
service:
Image: service:latest
Port: <none>
Host Port: <none>
Environment:
JOB_NAME: (v1:metadata.name)
Mounts: <none>
Volumes: <none>
Last Schedule Time: Tue, 17 Dec 2019 09:00:00 +0100
Active Jobs: <none>
Events:

编辑
我已经在 ClusterRBACConfig 中为我的命名空间关闭了 RBA,现在它可以工作了。所以cronjobs受角色影响是我的结论,应该可以添加角色并调用其他服务。

最佳答案

cronjob如果启用了 RBAC,则需要适当的权限才能运行。

这种情况下的解决方案之一是添加 ServiceAccountcronjob配置文件有足够的权限来执行它需要的操作。

由于您已经在命名空间中拥有现有服务您可以检查您是否已经存在 ServiceAccount对于特定 NameSpace通过使用:

$ kubectl get serviceaccounts -n serve

如果存在 ServiceAccount您可以将其添加到您的 cronjob list yaml 文件中。

就像在这个例子中:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: adwords-api-scale-up-cron-job
spec:
schedule: "*/2 * * * *"
jobTemplate:
spec:
activeDeadlineSeconds: 100
template:
spec:
serviceAccountName: scheduled-autoscaler-service-account
containers:
- name: adwords-api-scale-up-container
image: bitnami/kubectl:1.15-debian-9
command:
- bash
args:
- "-xc"
- |
kubectl scale --replicas=2 --v=7 deployment/adwords-api-deployment
volumeMounts:
- name: kubectl-config
mountPath: /.kube/
readOnly: true
volumes:
- name: kubectl-config
hostPath:
path: $HOME/.kube # Replace $HOME with an evident path location
restartPolicy: OnFailure

然后在 Pod 模板下应该有服务帐户可见:
$ kubectl describe cronjob adwords-api-scale-up-cron-job
Name: adwords-api-scale-up-cron-job
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"batch/v1beta1","kind":"CronJob","metadata":{"annotations":{},"name":"adwords-api-scale-up-cron-job","namespace":"default"},...
Schedule: */2 * * * *
Concurrency Policy: Allow
Suspend: False
Successful Job History Limit: 3
Failed Job History Limit: 1
Starting Deadline Seconds: <unset>
Selector: <unset>
Parallelism: <unset>
Completions: <unset>
Active Deadline Seconds: 100s
Pod Template:
Labels: <none>
Service Account: scheduled-autoscaler-service-account
Containers:
adwords-api-scale-up-container:
Image: bitnami/kubectl:1.15-debian-9
Port: <none>
Host Port: <none>
Command:
bash
Args:
-xc
kubectl scale --replicas=2 --v=7 deployment/adwords-api-deployment

Environment: <none>
Mounts:
/.kube/ from kubectl-config (ro)
Volumes:
kubectl-config:
Type: HostPath (bare host directory volume)
Path: $HOME/.kube
HostPathType:
Last Schedule Time: <unset>
Active Jobs: <none>
Events: <none>

如果是自定义 RBAC 配置,我建议引用 kubernetes文档。

希望这可以帮助。

关于kubernetes - 在 kubernetes cronjob 中指定角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59324922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com