当前位置:   article > 正文

kubernetes集群安全——认证、鉴权、准入控制_认证鉴权 英文呢

认证鉴权 英文呢

机制说明

Kubernetes 作为一个分布式集群的管理工具,保证集群的安全性是其一个重要的任务。API Server 是集群内部各个组件通信的中介,也是外部控制的入口。所以 Kubernetes 的安全机制基本就是围绕保护 API Server 来设计的。Kubernetes 使用了认证(Authentication)、鉴权(Authorization)、准入控制(AdmissionControl)三步来保证API Server的安全。

认证(Authentication)

  • HTTP Token 认证:通过一个 Token 来识别合法用户
  • HTTP Base 认证:通过 用户名+密码 的方式认证
  • 最严格的 HTTPS 证书认证:基于 CA 根证书签名的客户端身份认证方式
HTTPS:双向认证(颁发证书)-集群组件
      ETCD
	      服务端:ETCD
		  客户端:ApiServer
      ApiServer
	      服务端:ApiServer
	      客户端:
		        需要加密:
				    集群颁发:kubelet
					手动颁发:kubectl、kube-proxy
			    非加密:都运行在master节点
				   Controller Manager、Scheduler
				   
       SA(ServiceAccount)-POD认证
	      ca.crt:用户Pod验证apiserver发来的证书
		  token:用户单点认证apiserer验证pod是否合法
		  namespace:标识作用域
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

鉴权(Authorization)

上面认证过程,只是确认通信的双方都确认了对方是可信的,可以相互通信。而鉴权是确定请求方有哪些资源的权限。API Server 目前支持以下几种授权策略 (通过 API Server 的启动参数 “–authorization-mode” 设置)

  • AlwaysDeny:表示拒绝所有的请求,一般用于测试
  • AlwaysAllow:允许接收所有请求,如果集群不需要授权流程,则可以采用该策略
  • ABAC(Attribute-Based Access Control):基于属性的访问控制,表示使用用户配置的授权规则对用户请求进行匹配控制
  • Webbook:通过调用外部 REST 服务对用户进行授权
  • RBAC(Role-Based Access Control):基于角色的访问控制,现行默认规则
RBAC 授权模式

RBAC(Role-Based Access Control)基于角色的访问控制,在 Kubernetes 1.5 中引入,现行版本成为默认标准。相对其它访问控制方式,拥有以下优势:

  • 对集群中的资源和非资源均拥有完整的覆盖

  • 整个 RBAC 完全由几个 API 对象完成,同其它 API 对象一样,可以用 kubectl 或 API 进行操作

  • 可以在运行时进行调整,无需重启 API Server

RBAC 的 API 资源对象说明

RBAC 引入了 4 个新的顶级资源对象:Role(角色)、ClusterRole(集群角色)、RoleBinding(角色绑定)、ClusterRoleBinding(集群角色绑定),4 种对象类型均可以通过 kubectl 与 API 操作

Role and ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-reader
rules:
  - apiGroups: [""] # "" indicates the core API group
  #对象是pod类型,可以通过/分隔符控制子资源的访问权限,例如: resources: ["pods","pods/logs"],
  #如果为resources:["pods/logs"]表明只能访问pod下的logs
    resources: ["pods"] 
    verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  # "namespace" omitted since ClusterRoles are not namespaced
  name: secret-reader
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "watch", "list"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
RoleBinding and ClusterRoleBinding

RoleBinding 包含一组权限列表(subjects),权限列表中包含有不同形式的待授予权限资源类型(User、Group、ServiceAcount)

RoleBinding 可以绑定Role也可以绑定ClusterRole,而 ClusterRoleBinding 只能绑定ClusterRole
RoleBinding绑定Role

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: default
subjects:
  - kind: User
    name: jane
    #Defaults to "" for ServiceAccount subjects. 
    #Defaults to "rbac.authorization.k8s.io" for User and Group subjects
    apiGroup: rbac.auorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

RoleBinding绑定ClusterRole

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: read-secrets
  namespace: development # This only grants permissions within the "development" namespace.
subjects:
- kind: User
  name: dave
  apiGroup: rbac.authization.k8s.io
roleRef:
  kind: ClusterRole
  name: secret-reader
  apiGroup: rbac.authorization.k8s.io
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

ClusterRoleBinding绑定ClusterRole

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: read-secrets-global
subjects:
  - kind: Group
    name: manager
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: secret-reader
  apiGroup: rbac.authorization.k8s.io
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
实例:创建用户作为某个名称空间下的管理员
#在opt目录下创建test.json文件
{
  #用户为test
  "CN": "test",
  #当前证书可以在任意节点被调用,即任意节点可以通过证书访问apiserver
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      #所属组为k8s自定义的组,系统组为system:
      "O": "k8s",
      "OU": "System"
    }
  ]
}

# 下载证书生成工具
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
mv cfssl_linux-amd64 /usr/local/bin/cfssl

wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson

wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo

#授予可执行权限
chmod a+x /usr/local/bin/cfssl
chmod a+x /usr/local/bin/cfssljson
chmod a+x /usr/local/bin/cfssl-certinfo

#签发证书
cfssl gencert -ca=/etc/kubernetes/pki/ca.crt -ca-key=/etc/kubernetes/pki/ca.key -profile=kubernetes /opt/test.json | cfssljson -bare test 
[root@master opt]# ll test*
-rw-r--r--. 1 root root  993 54 15:52 test.csr
-rw-r--r--. 1 root root  217 54 15:28 test.json
-rw-------. 1 root root 1675 54 15:52 test-key.pem
-rw-r--r--. 1 root root 1233 54 15:52 test.pem

# 设置集群参数(即服务端)
[root@master opt]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.116.128 master k8s-api registry
192.168.116.129 node1
#设置KUBE_APISERVER变量
export KUBE_APISERVER="https://k8s-api:6443"

kubectl config set-cluster kubernetes \
--certificate-authority=/etc/kubernetes/pki/ca.crt \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=/opt/test.kubeconfig

[root@master opt]# cat test.kubeconfig 
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1EVXdNekV6TURjd01sb1hEVE15TURRek1ERXpNRGN3TWxvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTEdsCklpUDAyWlRDc240S0dwY25TT1pQUnUzTlhPemhMREo3S3B2eXViSEt6MWxhQVN0cVFmZi96eVNrWjFHbS95UzcKbS9EVVBXcVBPYVVRT1BMcFMrT2I0OGNOMGVqZldNazNHUkl2b080NDNoTHVDUk1hRVdYam8yakd4bVlUNjR4QgpKbGRhS2hQMjZNblBSNWxTOEs5cGdxa2JHaVhCMU1xcENtbXMrdWhaUWVkZ1I5RkVjSjczSXNRNENySjgyRkNZClFzeGFWN1p5ek1qcVBLTzNLNWpxY0Z0QjJJZFI3UXJaWDljTWFXakJZRjFkL2J5WnZSVjRmUjhqOG94VDlIalIKY1ZpSWxORFhzbVRLcEU3THF2Mll3OUZoOEE0cXdIOWhISmpqbTN5RkdHeS9RL0VRNStRZXJ2aFEwSHBXSlpRNQpGcHJWTDhncFp2QTdnMTVkcHlVQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFBblZIR1lOa0tPS1ZzN2xFbkZBT0JUN05Lc0sKTVIvVnNuTUpnbU9iSjRqam1hMkk2cG0vUFd1OUJIeFNteTcwbFE1WnYyZ1Rudm9wYnorVFhDMmdteTlneFBsaQo5UUxxZW5ITEtXam1zandQWFhaM2xaanJjYVluZDBHQnI4YXVORTNwNzZLbWZ5MHpLMEUvVDN2WW8vckIzTHlRCkRhalRsb1R4MllJZWQxRXBIVnFCOXg1M0E4SXR2Mi81azlnSUVMcnJSWTZkZG9icE5NYUNiLzFWWmFzSTNYaWMKQ1hZMi9mM0FmYTZDRm55dmFJWFh1SnIyVmkwcXpBV3VsNE5zcDdvRWxZVGdGcDlsNUtTUjV3UWVFakF6QXVLaAo1K3NQUzBmK1JyRGlnMTJyM2wwRFJCd1dtKzJnOGVlUHlrclNmL3NZaldQM2xWeFlTbHpnNFZDUElrTT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://k8s-api:6443
  name: kubernetes
contexts: null
current-context: ""
kind: Config
preferences: {}
users: null

# 设置客户端认证参数
kubectl config set-credentials test \
--client-certificate=/opt/test.pem \
--client-key=/opt/test-key.pem \
--embed-certs=true \
--kubeconfig=/opt/test.kubeconfig

[root@master opt]# cat test.kubeconfig 
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQ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5NYUNiLzFWWmFzSTNYaWMKQ1hZMi9mM0FmYTZDRm55dmFJWFh1SnIyVmkwcXpBV3VsNE5zcDdvRWxZVGdGcDlsNUtTUjV3UWVFakF6QXVLaAo1K3NQUzBmK1JyRGlnMTJyM2wwRFJCd1dtKzJnOGVlUHlrclNmL3NZaldQM2xWeFlTbHpnNFZDUElrTT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://k8s-api:6443
  name: kubernetes
contexts: null
current-context: ""
kind: Config
preferences: {}
users:
- name: test
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURZRENDQWtpZ0F3SUJBZ0lVTURUb3ZweHVUajk0VUZrNDM3N1RRNDZFQmljd0RRWUpLb1pJaHZjTkFRRUwKQlFBd0ZURVRNQkVHQTFVRUF4TUthM1ZpWlhKdVpYUmxjekFlRncweU1qQTFNRFF3TnpRNE1EQmFGdzB5TXpBMQpNRFF3TnpRNE1EQmFNRjh4Q3pBSkJnTlZCQVlUQWtOT01SQXdEZ1lEVlFRSUV3ZENaV2xLYVc1bk1SQXdEZ1lEClZRUUhFd2RDWldsS2FXNW5NUXd3Q2dZRFZRUUtFd05yT0hNeER6QU5CZ05WQkFzVEJsTjVjM1JsYlRFTk1Bc0cKQTFVRUF4TUVkR1Z6ZERDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBT0o1RS9VRgo0TEVvMW5naUNrazJ4ZENTQVQ2cUpSMWo5ODVjNm0yZXFDdURleUIrVDREWXlCOXA3RmZxUnRVdTRyQ1hxWTJ4CmhVRXZyOFNDUUtPd1hXUDgxcXhURzZkRVB5ejMrV2h5dTJGcHJSZWhoL3NwRHZVdmJ2QUFPMmx6U3VJVFQ3WWYKUVdhRk95NzdldHR5bm95bjNZa1FMbE9xeGRmV2hMeUJFTHkvdElDSW14L0UyYmlzNGNRelVSeGIwQisva0kwegpoN0Z0YzZQNXQz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 设置上下文参数
kubectl config set-context kubernetes \
--cluster=kubernetes \
--user=test \
--namespace=testns \
--kubeconfig=/opt/test.kubeconfig

[root@master opt]# cat test.kubeconfig 
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1EVXdNekV6TURjd01sb1hEVE15TURRek1ERXpNRGN3TWxvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTEdsCklpUDAyWlRDc240S0dwY25TT1pQUnUzTlhPemhMREo3S3B2eXViSEt6MWxhQVN0cVFmZi96eVNrWjFHbS95UzcKbS9EVVBXcVBPYVVRT1BMcFMrT2I0OGNOMGVqZldNazNHUkl2b080NDNoTHVDUk1hRVdYam8yakd4bVlUNjR4QgpKbGRhS2hQMjZNblBSNWxTOEs5cGdxa2JHaVhCMU1xcENtbXMrdWhaUWVkZ1I5RkVjSjczSXNRNENySjgyRkNZClFzeGFWN1p5ek1qcVBLTzNLNWpxY0Z0QjJJZFI3UXJaWDljTWFXakJZRjFkL2J5WnZSVjRmUjhqOG94VDlIalIKY1ZpSWxORFhzbVRLcEU3THF2Mll3OUZoOEE0cXdIOWhISmpqbTN5RkdHeS9RL0VRNStRZXJ2aFEwSHBXSlpRNQpGcHJWTDhncFp2QTdnMTVkcHlVQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFBblZIR1lOa0tPS1ZzN2xFbkZBT0JUN05Lc0sKTVIvVnNuTUpnbU9iSjRqam1hMkk2cG0vUFd1OUJIeFNteTcwbFE1WnYyZ1Rudm9wYnorVFhDMmdteTlneFBsaQo5UUxxZW5ITEtXam1zandQWFhaM2xaanJjYVluZDBHQnI4YXVORTNwNzZLbWZ5MHpLMEUvVDN2WW8vckIzTHlRCkRhalRsb1R4MllJZWQxRXBIVnFCOXg1M0E4SXR2Mi81azlnSUVMcnJSWTZkZG9icE5NYUNiLzFWWmFzSTNYaWMKQ1hZMi9mM0FmYTZDRm55dmFJWFh1SnIyVmkwcXpBV3VsNE5zcDdvRWxZVGdGcDlsNUtTUjV3UWVFakF6QXVLaAo1K3NQUzBmK1JyRGlnMTJyM2wwRFJCd1dtKzJnOGVlUHlrclNmL3NZaldQM2xWeFlTbHpnNFZDUElrTT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://k8s-api:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    namespace: testns
    user: test
  name: kubernetes
current-context: ""
kind: Config
preferences: {}
users:
- name: test
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURZRENDQWtpZ0F3SUJBZ0lVTURUb3ZweHVUajk0VUZrNDM3N1RRNDZFQmljd0RRWUpLb1pJaHZjTkFRRUwKQlFBd0ZURVRNQkVHQTFVRUF4TUthM1ZpWlhKdVpYUmxjekFlRncweU1qQTFNRFF3TnpRNE1EQmFGdzB5TXpBMQpNRFF3TnpRNE1EQmFNRjh4Q3pBSkJnTlZCQVlUQWtOT01SQXdEZ1lEVlFRSUV3ZENaV2xLYVc1bk1SQXdEZ1lEClZRUUhFd2RDWldsS2FXNW5NUXd3Q2dZRFZRUUtFd05yT0hNeER6QU5CZ05WQkFzVEJsTjVjM1JsYlRFTk1Bc0cKQTFVRUF4TUVkR1Z6ZERDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBT0o1RS9VRgo0TEVvMW5naUNrazJ4ZENTQVQ2cUpSMWo5ODVjNm0y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BtSEcxOUNJZDB4RFZEVVUKMlJEL2RMOXQ4MmdzUTFjS1JQVTNjQ08walpxck9pTGg1aWQ5aEpCMUg2MmtGa3IrNEpOK0IvcHNJM3VPWFlFTgo0b0k1NVl6NFJlTEtOTWZQSWNPVFpTRXFoTGdtVVNHZGhuUU9NWGNVMnFnK1FidzRYQ3ZRCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg=

#切换上下文信息
kubectl config use-context kubernetes --kubeconfig=/opt/test.kubeconfig
[root@master .kube]# cat config 
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1EVXdOVEF4TXpJek5Wb1hEVE15TURVd01qQXhNekl6TlZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTER6CkRhVG9IRVdpNzVnR002ZTU2SngzQlRHN3I4VWNrS3lIR2xiV2EvUHFyd0dlc2pFU0JZU2VkV1I3SGtiVWNjRGYKVmxSNStvczBISHFaZm80OEZBOXpQd1kra2k2M21jd015SFpXVXgvbkVLYmswTWZ1V3JwRG5DNFBIb09GdEFZVQpmSEhDL1YzOFJxN3EzMWpSdnp0eFdudXBPb3ZVVDYzaElDL0NpdnhSbTVhTlUxbFJDaUwyRXFkYW1jRVNGamY0CmZ2bmkyS283NlNnL3FrTXpxWTlDc2UxSkxUdkVKbmw1OXNnNHlGR3BLdnVacGVSbGROSGx1K2x2ZGJIWkpNQVoKSXE4c0NEbUVZdGlvL1lncmFxZXR4S25LQTdPU2k5U0M2OGMvRDJ5cndiWmtaSkVOSGNDRGpwRWdqQ3lLNk1pSQpxbFVKcEpNaWxNVE01SmxWaUZrQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFFY3RMdDBES1MxNTBMQVlMQjd6Rmd0TmxmK1YKYmg5MjcvNEppbERzVFJBVkpBa3hpUDVsT1ZQTjRlUzZ0WlpBWlFvOFVqZTl2R282aUMrc1ZDUkJMcGo4QXBUYQovVFdOMUx4dG1rci9vK2pMS3BEelBob0ZrK3dITDJ4SXN3K1l0SWltTmFEb0R1Z0MwOS8xOENrSnNGdjBHNGxTClNZb1RaZkg4QXRlQWRLNTJYUmxrUXBMWTJ5SWtIc21WYzkzUVI1WmttcFBPcmROcFk1TXhHYnZ6Zm02cDVCcE0KZ1ZvakJMQUNURXlKcjlzbzlkTjRRb1FkVkd1WVQrYzIxUXFJMVNrTUc3cG1FY08xb3pOc1QzeFJadkZEb0FJVAorWVNXQ1YrWEt0Wks4blgyMDlKZi8xb2pxbVFWY0tFdTNBblpLVEZtL0cxZTdWbmZBTUw2R014WWhaVT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://192.168.234.137:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    namespace: testns
    user: test
  name: kubernetes
#原先为空字符串,切换后变为kubernetes
#特别注意,必须先切换上下文后才能将文件拷贝到$HOME/.kube文件夹下,否则会报无法连接apiserver
current-context: kubernetes
kind: Config
preferences: {}
users:
- name: test
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURZRENDQWtpZ0F3SUJBZ0lVTHpBZXZmQXBhd0xyNzFKN1Brb084MmxUTlRJd0RRWUpLb1pJaHZjTkFRRUwKQlFBd0ZURVRNQkVHQTFVRUF4TUthM1ZpWlhKdVpYUmxjekFlRncweU1qQTFNRFV3TmpRMU1EQmFGdzB5TXpBMQpNRFV3TmpRMU1EQmFNRjh4Q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5QTE0cS9DNXoxcUoKdHdta3hMUEJYL2h5QzVLSUN1M3ZiUFc1eWlQYmtKRWE0Tnd0dzR5L0RSSHJhWTk5cXEwUjh0UGlQV01MbUg0UwpqdGwyUVByUFg0ekt0V1BLaXppT0xoWkRIZno3THM4UXVKRjZkTmc5TVZTSHA5YThZOFdkWTE3SXgzV3htVGx0CmJOaWhMNGtDZ1lCN1NXRFp5VTlIcnBqTG5vblRUdWlnNEhaZ05UcFBaa3hpTVp6aUdjMWZ1TFBqaW8zTnVja2kKM3V6cUF2UEVsVFAxYndEcEZVMjZ1THNIVUdra3J4YVFHMm52WGFaL0JIU1h1MkdtcUVMZERjdmtkL2pYNm5MWApEbUhnVENtL3h0M3haeFMyN1IzbWFOYXRCbUN2RGlPREFxR0JpSndUaU1ldURLNEhBR1JBeWc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
    
#创建testns的名称空间
kubectl create ns testns
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
#限制名称空间资源
apiVersion: v1
kind: ResourceQuota
metadata:
  name: limit-resources
  namespace: testns
spec:
  hard:
    requests.cpu: "20"
    requests.memory: 100Gi
    limits.cpu: "40"
    limits.memory: 200Gi

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
#给test用户绑定管理员权限
kubectl create rolebinding test-admin-binding --clusterrole=admin --user=test --namespace=testns
$ kubectl get rolebinding -n testns
NAME                 ROLE                AGE
test-admin-binding   ClusterRole/admin   33s

#linux随意创建用户,比如test1,将test.kubeconfig放入到test1家目录下.kube文件夹下,即可访问apiserver
useradd test1
passwd test1
mkdir -p /home/test1/.kube
cp /opt/test.kubeconfig /home/test1/.kube/config
chown -R test1.test1 /home/test1/.kube
#注意此时get pod的名称空间就为testns 
[test1@master ~]$ kubectl get pod
No resources found in testns namespace
#如果想要获取其他名称空间下的pod会被拒绝
[test1@master ~]$ kubectl get pod -n default
Error from server (Forbidden): pods is forbidden: User "test" cannot list resource "pods" in API group "" in the namespace "default"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

准入控制

准入控制是API Server的插件集合,通过添加不同的插件,实现额外的准入控制规则。甚至于API Server的一些主要的功能都需要通过 Admission Controllers 实现,比如 ServiceAccount,默认启用的插件

CertificateApproval, CertificateSigning, CertificateSubjectRestriction, DefaultIngressClass, DefaultStorageClass, DefaultTolerationSeconds, LimitRanger, MutatingAdmissionWebhook, NamespaceLifecycle, PersistentVolumeClaimResize, Priority, ResourceQuota, RuntimeClass, ServiceAccount, StorageObjectInUseProtection, TaintNodesByCondition, ValidatingAdmissionWebhook
  • 1
  • NamespaceLifecycle: 防止在不存在的 namespace 上创建对象,防止删除系统预置 namespace,删除namespace 时,连带删除它的所有资源对象。
  • LimitRanger:确保请求的资源不会超过资源所在 Namespace 的 LimitRange 的限制。
  • ServiceAccount: 实现了自动化添加 ServiceAccount。
    mespace 上创建对象,防止删除系统预置 namespace,删除namespace 时,连带删除它的所有资源对象。
  • LimitRanger:确保请求的资源不会超过资源所在 Namespace 的 LimitRange 的限制。
  • ServiceAccount: 实现了自动化添加 ServiceAccount。
  • ResourceQuota:确保请求的资源不会超过资源的 ResourceQuota 限制
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/178695
推荐阅读
相关标签
  

闽ICP备14008679号