kubectl create using inline options in bash script instead of yaml file

1/24/2019

I want to convert below line to run as a bash script so that i can call it using jenkins job.

kubectl create -f tiller-sa-crb.yaml

The tiller-sa-crb.yaml is below. How can i convert my above command in bash script. such that my jenkins job calls ./tiller-sa-crb.sh and it does all below. Basically, my end goal is to have a pure shell script and invoke that on jenkins job.

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
-- AhmFM
bash
jenkins-pipeline
kubectl
kubernetes
kubernetes-helm

2 Answers

1/25/2019

You can also make use of stdin to kubectl create command, like this:

#!/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
-- Nepomucen
Source: StackOverflow

1/24/2019

Figured out how to use kubectl with parameters on command line to create service account and also to create rolebinding using the service account.

#!/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
-- AhmFM
Source: StackOverflow