当前位置:网站首页>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 .
边栏推荐
- Oracle case: ohasd crash on AIX
- One line of keyboard
- How to solve the enterprise network security problem in the mixed and multi cloud era?
- What transmission modes does the IOT data gateway support
- How to register a domain name? What are the benefits of building a website?
- SQL server memory management on cloud
- Neighbor vote: use proximity voting to optimize monocular 3D target detection (ACM mm2021)
- 10 year old drivers who have been engaged in software testing tell you what type of software is suitable for automation
- Risk management - Asset Discovery series - public web asset discovery
- Spirit information development log (3)
猜你喜欢

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

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
Oracle case: ohasd crash on AIX

A cigarette of time to talk with you about how novices transform from functional testing to advanced automated testing

Manual for automatic testing and learning of anti stepping pits, one for each tester

One line of keyboard

Solution to the 39th weekly game of acwing
Fault analysis | using --force to batch import data leads to partial data loss

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]
Operation and maintenance dry goods | how to improve the business stability and continuity through fault recovery?
How does easyplayer RTSP configure sending heartbeat information to the server?
Project deployment for learning 3D visualization from scratch
25 classic selenium automated interview questions, collect them quickly
How to build a website with a domain name? Is the domain name very cheap
Analysis of official template of wechat personnel recruitment management system (I)
DHCP server setup
The difference between ArrayList and LinkedList and the principle of using scene locality
Just now, we received a letter of thanks from Bohai University.
PMP | 8 abilities that excellent project managers focus on training
解读AI机器人产业发展的顶层设计
Raspberry PI (bullseye) replacement method of Alibaba cloud source
Tencent cloud won the "best customer value award for security hosting services in China" from Sullivan toubao Research Institute
The gadgets developed by freshmen are popular. Netizen: my food is good
Spirit information development log (3)
[in depth sharing] Devops evolution path -- Realizing R & D digital transformation based on four vertical and four horizontal Devops system
Tencent security apkpecker launched dex-vmp automatic shelling service
Configure PHP development environment in MAC environment: apache+php+mysql
Manual for automatic testing and learning of anti stepping pits, one for each tester