kubernetes - 在 Kubernetes cronjob 中指定角色
问题描述
我们有一个运行 istio 代理的 Kubernetes 集群。起初,我创建了一个 cronjob,它从数据库中读取并在找到时更新一个值。它工作得很好。
然后事实证明我们已经有一个执行数据库更新的服务,所以我将数据库代码更改为服务调用。
conn := dial.Service("service:3550", grpc.WithInsecure())
client := protobuf.NewServiceClient(conn)
client.Update(ctx)
但 istio 拒绝调用并出现 RBAC 错误。它只是拒绝并且没有说明原因。
是否可以向 cronjob 添加角色?我们怎么能做到这一点?
mTLS 网格策略是允许的。
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受角色影响是我的结论,应该可以添加角色并调用其他服务。
解决方案
如果启用了 RBAC ,cronjob
则需要适当的权限才能运行。
在这种情况下,解决方案之一是ServiceAccount
在cronjob
配置文件中添加一个具有足够权限来执行所需操作的配置文件。
由于您已经在命名空间中拥有现有服务,您可以使用以下方法检查您是否存在ServiceAccount
特定服务NameSpace
:
$ kubectl get serviceaccounts -n serve
如果存在ServiceAccount
您可以将其添加到您的 cronjob 清单 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 Template 下应该有 Service Account 可见:
$ 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文档。
希望这可以帮助。
推荐阅读
- python - ROS Melodic python版本冲突(--unmet dependencies)
- html - 有没有办法强制 HTML换行而不是全宽显示?
- php - PHPmailer 和 Google reCapcha V2 验证问题
- swift - youtube-dl 使用 --extract-audio --get-url 和 --audio-format m4a 总是返回 webm
- java - 无法在所需的“org.springframework.jdbc.core.NamedParameterJdbcTemplate”类型的 bean 中找到构造函数参数 0 的解决方案
- solidity - 错误编码参数:错误:类型/值长度不匹配
- python - 我的笔记本电脑的硬件配置会是我只得到斐波那契数列第 1000000 项的 13601 位的原因吗?
- python - 如何在我的 pygame 游戏中添加碰撞?
- r - tidyverse 中的 R 命令
- python - 如何将文本文件转换为列表列表