11-kubernetes认证及serviceaccount
以插件的形式存在
认证-授权-准入控制
token-RBAC-
用户组成:
user: username,uid
group: 用户组
extra: 额外信息
客户端私钥
cat .kube/config
本地启动代理
kubectl proxy --port=8080
API:Request path
kubectl api-version http://localhost:8080/apis/apps/v1/namespaces/default/deployments/myapp-deploy/ /apis/apps/v1 /apis/apps/b1beta1 /apis/apps/b1beta2
http 请求动作
GET
POST
PUT
DELETE
list
create
update
patch
watch
poroxy
redirect
deletecollection
k8上需要连接API的有两类
1.集群外部
2.集群内部
创建一个service 帐号
#测试 kubectl create serviceaccount admin -o yaml --dry-run kubectl create serviceaccount admin #查看以创建的service帐号 kubectl get sa #查看详细信息 kubectl describe sa admin ##简洁输出 kubectl get pods myapp-asdxx -o yaml --export
POD使用 Service Account (SA)帐号例子
apiVersion: v1 kind: Pod metadata: name: pod-sa-demo namespace: default labels: app: myapp tier: frontend annotations: leiyan.com/reatedby: "cluster admin" spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 serviceAccountName: admin
授权逻辑
K8S上认证Token信息文件都保存在kubeconfig上。
kubectl config view ➜ ~ cd /etc/kubernetes/pki ➜ pki ll total 56K -rw-r--r-- 1 root root 1.3K Jan 23 11:26 apiserver.crt -rw-r--r-- 1 root root 1.1K Jan 23 11:26 apiserver-etcd-client.crt -rw------- 1 root root 1.7K Jan 23 11:26 apiserver-etcd-client.key -rw------- 1 root root 1.7K Jan 23 11:26 apiserver.key -rw-r--r-- 1 root root 1.1K Jan 23 11:26 apiserver-kubelet-client.crt -rw------- 1 root root 1.7K Jan 23 11:26 apiserver-kubelet-client.key -rw-r--r-- 1 root root 1.1K Jan 23 11:26 ca.crt -rw------- 1 root root 1.7K Jan 23 11:26 ca.key drwxr-xr-x 2 root root 162 Jan 23 11:26 etcd -rw-r--r-- 1 root root 1.1K Jan 23 11:26 front-proxy-ca.crt -rw------- 1 root root 1.7K Jan 23 11:26 front-proxy-ca.key -rw-r--r-- 1 root root 1.1K Jan 23 11:26 front-proxy-client.crt -rw------- 1 root root 1.7K Jan 23 11:26 front-proxy-client.key -rw------- 1 root root 1.7K Jan 23 11:26 sa.key
自制证书
1.生成私钥
(umask 077; openssl genrsa -out leiyan.key 2048)
2.生成证书
openssl req -new -key leiyan.key -out leiyan.csr -subj "/CN=leiyan"
3.由ca.key 签署
openssl x509 -req -in leiyan.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out leiyan.crt -days 365
4.查看
openssl x509 -in leiyan.crt -text -noout
添加为K8S集群认证
kubectl config view #c查看当前 kubectl config set-credentials --help kubectl config set-credentials leiyan --client-certificate=./leiyan.crt --client-key=./leiyan.key --embed-certs=true
授权leiyan 能访问 kubernets集群
kubectl config set-context leiyan@kubernetes --cluster=kubernetes --user=leiyan
定义集群地址
kubectl config set-cluster --help kubectl config set-cluster mycluter --kubeconfig=/tmp/test.conf --server="https://192.168.1.90:6443" --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
切换帐号
kubectl config use-context leiyan@kubernetes
请登录后查看评论内容