当前位置:网站首页>Produce kubeconfig with permission control
Produce kubeconfig with permission control
2022-06-24 06:28:00 【Xiezhengwei】
scene
In the development test scenario , We opened k8s colony , The cluster resources need to be allocated to users , But hopefully they can only use resources in their own namespaces , Not affecting others .
The following procedure shows how to use k8s Native capabilities do this .
Implementation steps
establish namespace
First create a namespace for the user name
kubectl create ns well
establish ServiceAccount
Create... Under the user namespace SA
apiVersion: v1 kind: ServiceAccount metadata: name: well-sa namespace: well
Create a Role
Create... Under the user namespace Role, Here, put the resources and permissions you want to give users .
kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: well-role namespace: well rules: - apiGroups: [""] resources: - pods - deployments - configmaps - services verbs: - get - list - watch - create - update - delete
establish RoleBinding
Will just create SA and Role Tied together .
kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: well-binding namespace: well subjects: - kind: ServiceAccount name: well-sa roleRef: kind: Role name: well-role apiGroup: rbac.authorization.k8s.io
Now? well-sa This ServiceAccount You can access well Namespace . Next we need to put SA The corresponding key is given to the user .
production kubeconfig
kubeconfig The template is as follows :
apiVersion: v1
kind: Config
users:
- name: well
user:
token: <token>
clusters:
- cluster:
certificate-authority-data: <certificate-authority-data>
server: <api-server>
name: well-cluster
contexts:
- context:
cluster: well-cluster
namespace: well
user: well
name: well-cluster
current-context: well-clusterNow you just need to replace the corresponding content above with the actual content .
The path to obtain these parameters is as follows :
- Through the command
kubectl config view --flatten --minifyCan get certificate-authority-data and api-server Information . - Through the command
kubectl describe sa well-sa -n wellGet secret Of key. - Through the command
kubectl describe secret <key> -n wellGet token Information .
When the replacement is complete kubeconfig It can be saved as a document and distributed to users .
automation
The above process can be completed automatically , Here is the complete implementation of this process Shell Script .
First of all, you need to have a permission sufficient kubeconfig In your kubectl Current context .
Copy this script and name the file create-key.sh, Give Execution Authority .
#!/bin/bash
echo " Welcome to use kubeconfig generator , This script can generate a key with limited permissions ."
echo " Executing this script requires that you first have the default key with the maximum permissions of the cluster ."
echo
echo " Usage method :"
echo "./create-key.sh"
echo " perhaps "
echo "./create-key.sh <yourname>"
echo
# Check ns
function userExists() {
checkUser=`kubectl get ns | grep -w $1`
if [ -z "$checkUser" ]
then
echo 0
else
echo 1
fi
}
USER=$1
if [ -z "$USER" ];then
while true; do
read -p " Please enter the user id :" USER
if [ -z "$USER" ];then # Input nothing
echo " You have to enter something , perhaps ctrl + c sign out , Please re-enter ."
echo
else
checkUser=`userExists $USER`
if [ "$checkUser" == "0" ];
then
break
else
echo "$USER occupied , Please re-enter or ctrl + c sign out ."
echo
fi
fi
done
else
checkUser=`userExists $USER`
if [ "$checkUser" = "1" ];then
echo "$USER occupied ." >>/dev/stderr
exit
fi
fi
kubectl create ns $USER
# establish SA
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: $USER-sa
namespace: $USER
EOF
# Create a character , And control resources , Adjust this section to assign the resource permissions you need
cat <<EOF | kubectl apply -f -
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: $USER-role
namespace: $USER
rules:
- apiGroups: [""]
resources:
- pods
- deployments
- configmaps
- services
verbs:
- get
- list
- watch
- create
- update
- delete
EOF
# establish Role Binding
cat <<EOF | kubectl apply -f -
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: $USER-binding
namespace: $USER
subjects:
- kind: ServiceAccount
name: $USER-sa
roleRef:
kind: Role
name: $USER-role
apiGroup: rbac.authorization.k8s.io
EOF
KUBE_APISERVER=`kubectl config view --minify -o=jsonpath="{.clusters[*].cluster.server}"`
TOKEN_KEY=`kubectl get sa $USER-sa -n $USER -o=jsonpath="{.secrets[0].name}"`
TOKEN=`kubectl get secrets $TOKEN_KEY -n $USER -o=jsonpath="{.data.token}"`
CLUSTER_AUTH=`kubectl config view --flatten --minify -o=jsonpath="{.clusters[0].cluster.certificate-authority-data}"`
TOKEN_DECODE=`echo $TOKEN | base64 --decode`
# production kubeconfig file
cat > $USER.config <<EOF
apiVersion: v1
kind: Config
users:
- name: $USER
user:
token: $TOKEN_DECODE
clusters:
- cluster:
certificate-authority-data: $CLUSTER_AUTH
server: $KUBE_APISERVER
name: $USER-cluster
contexts:
- context:
cluster: $USER-cluster
namespace: $USER
user: $USER
name: $USER-cluster
current-context: $USER-cluster
EOF
cat $USER.config | pbcopy
echo
echo " succeed !!!!"
echo
echo
echo "kubeconfig The file has been saved as ./$USER.config, And has been copied to your clipboard ."
echo " At present kubeconfig Only namespaces are allowed to be accessed $USER Specific resources under ."
echo
echo " Try the following command :"
echo "kubectl get po --kubeconfig=./$USER.config"
echo "kubectl get secret --kubeconfig=./$USER.config"
echo "kubectl get po --kubeconfig=./$USER.config -n default"- perform ./create-key.sh perhaps ./create-key.sh well Fine .
- When the execution is completed, a... Will be saved in the current directory well.config The file of , This is kubeconfig file , Send to use, this is good . Or paste the contents of the clipboard to the user .
- This script gives test cases , among ,kubectl get po Have permission ,kubectl get secret No authority ,kubectl get po -n default No authority .
- modify Role Part of , You can fine control permissions , You can also create multiple Role and Binding, Control different resources by permissions .
- Need to release resources , Delete namespace directly , Convenient and quick .
kubectl delete ns well
This script is in Mac Pass the next test .
边栏推荐
- Technology is a double-edged sword, which needs to be well kept
- WordPress pill applet build applet from zero to one [pagoda panel installation configuration]
- Small programs import Excel data in batches, and cloud development database exports CVS garbled code solution
- Analysis and treatment of easydss flash back caused by system time
- Tencent Security jointly established a data security committee
- How to solve the enterprise network security problem in the mixed and multi cloud era?
- What is the difference between a white box test and a black box test
- Easyscreen live streaming component pushes RTSP streams to easydarwin for operation process sharing
- Project deployment for learning 3D visualization from scratch
- Discussion on NFT Technology
猜你喜欢
![[fault announcement] one stored procedure brings down the entire database](/img/7c/e5adda73a077fe4b8f04b59d1e0e1e.jpg)
[fault announcement] one stored procedure brings down the entire database
Oracle case: ohasd crash on AIX

The product layout is strengthened, the transformation of digital intelligence is accelerated, and FAW Toyota has hit 2022million annual sales

One line of keyboard

ServiceStack. Source code analysis of redis (connection and connection pool)

Enter the software test pit!!! Software testing tools commonly used by software testers software recommendations
Fault analysis | using --force to batch import data leads to partial data loss

Solution to the 39th weekly game of acwing

Technology is a double-edged sword, which needs to be well kept

创客教育给教师发展带来的挑战
随机推荐
Apache enables gzip compressed web page transmission method
SAP hum unbinds Hu from delivery order
Double non students, self-taught programming, counter attack Baidu one year after graduation!
Analysis of official template of wechat personnel recruitment management system (III)
How to solve the problem that after Tencent cloud sets static DNS, restarting the machine becomes dynamic DNS acquisition
Why the computer can't start
Get the short video! Batch download of Kwai video (with source code)
Basic concepts of complex networks
Neighbor vote: use proximity voting to optimize monocular 3D target detection (ACM mm2021)
[in depth sharing] Devops evolution path -- Realizing R & D digital transformation based on four vertical and four horizontal Devops system
How to recover data by splicing database fragments
Tencent security release data security compliance capability map
How to bind the most formal domain name? What are the precautions for binding domain names?
Tencent Security jointly established a data security committee
Just now, we received a letter of thanks from Bohai University.
12. Tencent cloud IOT device side learning -- NTP function and Implementation
You don't have to spend a penny to build a wechat official website in a minute
EEG microstate as a continuous phenomenon
Little transparent apprentice's way to go ashore
WordPress pill applet build applet from zero to one [applet registration configuration]