资源说明
kubectl本身带有认证信息 认证信息文件存放在用户家目录下的/root/.kube/config
kubectl 可以远程访问 只需要把配置文件拷贝过去 k8s的输入输出都是以json格式来传递的
[root@k8s-master ~]# kubectl proxy --port=8080
Starting to serve on 127.0.0.1:8080
[root@k8s-master .kube]# curl http://localhost:8080/api/v1/namespaces
k8s中每个资源都有一个唯一对应的资源路径url 可以通过此url对资源进行操作
创建资源的时候需要填充一个body参数 其它操作直接调用url指定action即可
快速生成资源清单文件模板
1.只要支持create命令的资源都可以使用此方式
kubectl create serviceaccount mysa -o yaml --dry-run > s.yaml
用户管理
k8s的用户认证管理的设计方式可以是同时保存多个用户对应管理多个不同的k8s集群 使用了上下文概念来实现不同环境的用户认证管理 use-context yxh@kubernetes
1.创建私钥 [pki]# (umask 077; openssl genrsa -out yxh.key 2048) [pki]# ls apiserver.key front-proxy-ca.crt yxh.key 2.生成证书签署请求 CN就是用户账号名称 [pki]# openssl req -new -key yxh.key -out yxh.csr -subj "/CN=yxh" 3.签署证书 [root@k8s-master pki]# openssl x509 -req -in yxh.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out yxh.crt -days 365 Signature ok subject=/CN=yxh Getting CA Private Key 4.查看证书信息 [root@k8s-master pki]# openssl x509 -in yxh.crt -text -noout Certificate: Data: Version: 1 (0x0) Serial Number: 8a:59:4a:8d:64:e9:3b:1c Signature Algorithm: sha256WithRSAEncryption Issuer: CN=kubernetes Validity Not Before: Jun 22 10:30:26 2019 GMT Not After : Jun 21 10:30:26 2020 GMT Subject: CN=yxh 5.添加用户到k8s集群 [root@k8s-master pki]# kubectl config set-credentials yxh --client-certificate=./yxh.crt --client-key=./yxh.key --embed-certs=true User "yxh" set. [root@k8s-master pki]# kubectl config set-context yxh@kubernetes --cluster=kubernetes --user=yxh Context "yxh@kubernetes" created. [root@k8s-master pki]# kubectl config use-context yxh@kubernetes Switched to context "yxh@kubernetes". [root@k8s-master pki]# kubectl get pods No resources found. Error from server (Forbidden): pods is forbidden: User "yxh" cannot list pods in the namespace "default"