K8S原來如此簡單(八)ServiceAccount+RBAC
ServiceAccount
ServiceAccount是給運行在Pod的程序使用的身份認證,Pod容器的進程需要訪問API Server時用的就是ServiceAccount賬戶。
ServiceAccount僅局限它所在的namespace,每個namespace創建時都會自動創建一個default service account。
創建Pod時,如果沒有指定Service Account,Pod則會使用default Service Account。
通過以下命令可以查看我們前面創建chesterns這個namespace下的serviceaccount與對應的secret
kubectl describe pod -n chesterns kubectl describe sa -n chesterns kubectl describe secrets -n chesterns
通過以下命令查看serviceaccount掛載進容器內部的文件
kubectl exec -it chesterdeployment-cb855fb4b-5ksgd -n chesterns -- ls /var/run/secrets/kubernetes.io/serviceaccount/
- ca.crt:根證書,用於Client端驗證API Server發送的證書
- namespace:標識這個service-account-token的作用域空間
- token:使用API Server私鑰簽名的JWT,用於訪問API Server時,Server端的驗證
自定義ServiceAccount
kubectl create sa chestersa -n chesterns
kubectl describe sa chestersa -n chesterns
通過指定serviceAccountName,讓pod使用自定義的sa
apiVersion: apps/v1 kind: Deployment metadata: name: chesterdeployment namespace: chesterns labels: app: chesterapi spec: replicas: 1 selector: matchLabels: app: chesterapi template: metadata: labels: app: chesterapi spec: serviceAccountName: chestersa containers: - name: oneapi image: registry.cn-beijing.aliyuncs.com/chester-k8s/oneapi:latest ports: - containerPort: 5000 livenessProbe: httpGet: path: /test port: 5000 - name: twoapi image: registry.cn-beijing.aliyuncs.com/chester-k8s/twoapi:latest ports: - containerPort: 5001 livenessProbe: httpGet: path: /test/calloneapi port: 5001
我們可以配置serviceaccount中的ImagePullSecret,拉取私有鏡像。
創建secret
kubectl create secret docker-registry aliregistry --docker-server=registry.cn-beijing.aliyuncs.com --docker-username=陳xx --docker-password=xxxxx -n chesterns
kubectl edit sa chestersa -n chesterns
imagePullSecrets: #添加此字段
- name: aliregistry
通過我們自定義的sa拉取私有鏡像
apiVersion: apps/v1 kind: Deployment metadata: name: chesterdeployment namespace: chesterns labels: app: chesterapi spec: replicas: 1 selector: matchLabels: app: chesterapi template: metadata: labels: app: chesterapi spec: serviceAccountName: chestersa containers: - name: oneapi image: registry.cn-beijing.aliyuncs.com/chester-k8s/privateoneapi:latest ports: - containerPort: 5000 livenessProbe: httpGet: path: /test port: 5000
通過以下命令驗證私有鏡像拉取狀態
kubectl delete -f deployment.yaml kubectl apply -f deployment.yaml kubectl describe pod -n chesterns
RBAC
在Kubernetes中,所有資源對象都是通過API對象進行操作,他們保存在etcd里。
而對etcd的操作我們需要通過訪問 kube-apiserver 來實現,上面的Service Account其實就是APIServer的認證過程,而授權的機制是通過RBAC這個基於角色的訪問控制實現。
Role與ClusterRole
在RBAC中,Role表示一組規則權限,權限只會增加(累加權限)。
-
Role 是定義在一個 namespace 中
-
ClusterRole 是集群級別的
定義Role
定義一個Role,限定在在名字為 chesterns namespace 中,對Pods有get,watch,list的權限
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: chesterrole namespace: chesterns rules: - apiGroups: [""] resources: ["pods"] verbs: ["get","watch","list"]
kubectl create -f role.yaml kubectl get role -n chesterns
修改初始化集群時,應用kubeconfig文件的模式
vi /etc/profile export KUBECONFIG=/etc/kubernetes/admin.conf #刪除 source /etc/profile mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
創建用戶
useradd chester su - chester #嘗試訪問集群 kubectl get pod
下載cfssl
cd /usr/bin wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 ## 改名,給執行權限 mv cfssl_linux-amd64 cfssl mv cfssljson_linux-amd64 cfssljson mv cfssl-certinfo_linux-amd64 cfssl-certinfo chmod +x * ll -h
生成證書
mkdir /usr/local/chestercert cd /usr/local/chestercert vi chester-csr.json
{ "CN": "chester", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "BeiJing", "O": "Ctyun", "ST": "BeiJing", "OU": "System" } ] }
cd /etc/kubernetes/pki/ cfssl gencert -ca=ca.crt -ca-key=ca.key -profile=kubernetes /usr/local/chestercert/chester-csr.json | cfssljson -bare chesteruser ls
為chester用戶生成集群配置文件
kubectl config set-cluster kubernetes --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true --server=https://192.168.43.111:6443 --kubeconfig=chester.kubeconfig ls
綁定用戶信息至kubeconfig中
kubectl config set-credentials chesteruser \ --client-certificate=/etc/kubernetes/pki/chesteruser.pem \ --client-key=/etc/kubernetes/pki/chesteruser-key.pem \ --embed-certs=true \ --kubeconfig=chester.kubeconfig
設置上下文參數
kubectl config set-context kubernetes \ --cluster=kubernetes \ --user=chesteruser \ --namespace=chesterns \ --kubeconfig=chester.kubeconfig
把kubeconfig文件複製到chester用戶的目錄的.kube下
mkdir -p /home/chester/.kube cp chester.kubeconfig /home/chester/.kube/config cd /home/chester/.kube/ ls config ## 修改文件所有者 cd /home/chester/ chown -R chester:chester .kube/
RoleBinding與ClusterRoleBinding
RoleBinding可以將角色中定義的權限授予用戶或用戶組。
RoleBinding包含一組權限列表(Subjects),權限列表中包含有不同形式的待授予權限資源類型(users,groups, or Service Account),Rolebinding 同樣包含對被 Bind的Role
-
RoleBinding 適用於某個命名空間內授權
-
ClusterRoleBinding適用於集群範圍內的授權。
定義RoleBinding
定義一個名稱為chesterrolebinding,將chesterrole權限資源賦予名為chester的用戶,僅作用於chesterns namespace。
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: chesterrolebinding namespace: chesterns subjects: - kind: User name: chester apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: chesterrole apiGroup: rbac.authorization.k8s.io
kubectl apply -f rolebinding.yaml
kubectl describe rolebinding -n chesterns
驗證chester用戶能否正常訪問
su chester cd /home/chester/.kube kubectl config use-context kubernetes --kubeconfig=config kubectl get pod -n chesterns


