【问题标题】:kubectl create using inline options in bash script instead of yaml filekubectl 在 bash 脚本中使用内联选项而不是 yaml 文件创建
【发布时间】:2019-06-18 13:42:46
【问题描述】:

我想将下面的行转换为作为 bash 脚本运行,以便我可以使用 jenkins 作业调用它。

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

【问题讨论】:

    标签: bash kubernetes jenkins-pipeline kubectl kubernetes-helm


    【解决方案1】:

    想出了如何在命令行上使用带有参数的 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
    

    【讨论】:

      【解决方案2】:

      你也可以使用 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
      

      【讨论】:

        猜你喜欢
        • 2021-11-04
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-01
        • 2019-08-05
        • 1970-01-01
        • 2021-12-30
        相关资源
        最近更新 更多