我想将下面的行转换为bash脚本运行,以便可以使用jenkins job调用它。
kubectl create -f tiller-sa-crb.yaml
tiller-sa-crb.yaml在下面。我如何在bash脚本中转换上述命令。这样我的詹金斯工作调用./tiller-sa-crb.sh,并且在下面完成了所有工作。基本上,我的最终目标是拥有一个纯shell脚本并在jenkins作业上调用它。
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
答案 0 :(得分:1)
您还可以使用stdin来kubectl create命令,如下所示:
#!/usr/bin/env bash
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
EOF
答案 1 :(得分:0)
弄清楚了如何在命令行上使用带有参数的kubectl创建服务帐户,以及如何使用服务帐户创建角色绑定。
#!/bin/bash
#create SA account in one line in namespace kube-system
kubectl create serviceaccount tiller -n kube-system
#create cluster role binding in one line using above serviceaccount and in kube-system namespace
kubectl create clusterrolebinding crb-cluster-admin-test2 --
clusterrole=cluster-admin --serviceaccount=kube-system:tiller