k8s集群环境namespace共享和隔离

k8s集群环境namespace共享和隔离
不同的工作组可以在同一个kubernets集群中工作,使用命名空间和context来对不同的工作组进行划分,使其在同一个集群工作又互不干扰
namespace共享和隔离,适用于人员多,人事架构系统复杂,一遍情况下,使用标签就够公司使用

1、创建命名空间namespeace
namespace-development.yaml

apiVersion: v1
kind: Namespace
metadata:
    name: development

    
namespace-production.yaml

apiVersion: v1
kind: Namespace
metadata:
    name: production
    
使用yaml创建命令空间
[root@kubernetes k8s]# kubectl create -f namespace-development.yaml
namespace/development created
[root@kubernetes k8s]# kubectl create -f namespace-production.yaml
namespace/production created

查看创建的命名空间development,production)
[root@kubernetes k8s]# kubectl get namespace
NAME          STATUS   AGE
default       Active   180d
development   Active   97s
kube-public   Active   180d
kube-system   Active   180d
production    Active   73s

2、定义运行的环境Context)
使用kubectl config set-context来定义Context,并将Context放置于之前创建的命名空间中
使用这些命令前需要备份好/etc/kubernetes/admin.conf文件

set-cluster 设置访问集群的参数,下面的这条命令是访问 kubetnetes集群,指向kube-apiserver的地址
[root@kubernetes k8s]# kubectl config set-cluster kubernetes –server=http://192.168.73.152:6443
Cluster "kubernetes-cluster" set

[root@kubernetes k8s]# kubectl config set-context ctx-dev –namespace=development –cluster=kubernetes –user=dev
Context "ctx-dev" created.
[root@kubernetes k8s]# kubectl config set-context ctx-prod –namespace=production –cluster=kubernetes –user=prod
Context "ctx-prod" created.

原文件的内容
[root@kubernetes ~]# kubectl config view
apiVersion: v1
clusters:
– cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.73.152:6443
  name: kubernetes
contexts:
– context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
– name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

使用kubectl config view查看已经定义的Context新文件的内容)
[root@kubernetes k8s]# kubectl config view
apiVersion: v1
clusters:
– cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.73.152:6443
  name: kubernetes
contexts:
– context:
    cluster: kubernetes
    namespace: development
    user: dev
  name: ctx-dev
– context:
    cluster: kubernetes
    namespace: production
    user: prod
  name: ctx-prod
– context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
– name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

kubectl config 命令在${HOME}/.kube目录下生成了一个名为config的文件,文件的内容就是以kubectl config view命令查看的内容,所以,也可以通过手工编辑该文件的方式来设置Context

3、设置工作组在特定的命令空间环境中工作
使用kubectl config use-context <context_name>来设置当前的运行环境

将当前的运行环境设置为"ctx-dev"
[root@kubernetes k8s]# kubectl config use-context ctx-dev
Switched to context "ctx-dev".

在当前的命名空间创建pod
[root@kubernetes k8s]# cat redis-slave-conrtoller.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  replicas: 2
  selector:
    name: redis-slave
  template:
    metadata:
      labels:
        name: redis-slave
    spec:
      containers:
      – name: slave
        image: kubeguide/guestbook-redis-slave
        ports:
        – containerPort: 6379

创建pod时报错,用户的权限错误,需要给用户授权
[root@kubernetes k8s]# kubectl create -f redis-slave-conrtoller.yaml 
Error from server Forbidden): error when creating "redis-slave-conrtoller.yaml": replicationcontrollers is forbidden: User "system:anonymous" cannot create resource "replicationcontrollers" in API group "" in the namespace "development"

查看集群的admin.conf文件,查看配置参数
[root@kubernetes k8s]# cat /etc/kubernetes/admin.conf
apiVersion: v1
clusters:
– cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRFNU1ETXdNVEV4TXprMU4xb1hEVEk1TURJeU5qRXhNemsxT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wbwp0Y3NoYmxLNmZGU1lDVHB1T1lPRDFwLzNYLzZEaXhMdUVJR25jZlUxMEt0RkZSZno4Z0RKOUJzVlFKMD0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://192.168.73.152:6443
  name: kubernetes
contexts:
– context:
    cluster: kubernetes
    namespace: development
    user: dev
  name: ctx-dev
– context:
    cluster: kubernetes
    namespace: production
    user: prod
  name: ctx-prod
– context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
– name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJUWVZTXFnRzZxNUV3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RBek1ERXhNVE01TlRkYUZ3MHlNREF5TWpreE1UTTVOVGhhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQTgrUnYrM24rWUtucUptRHIKVXlWeGxpSXg5TXVUd2VKK0w2bXB3T2xTZSt1VFRkREVzNFZIQXY0QzV1bzlDL1FBSEJIdWdvUFI0alo0U0xWTwpoQ1l4MlVvSzVzNjQ4Q3d4enhabW9XenRPaVJEcHp6ZG44VFpVb2gxSVY3Zy9NbFpvYWhCYXNnaGdEWjNGRGVoCmZYTGFJMmlDS1FpWG1RMkR5bTkxYXRHblhXOXZXUzk2VTBtV296NU1YRHNUOXFySnBLVVNLMlZnU2dOSW1BMFUKbU5UV3A2NlkvaTV1bllyT1VscXkyam54VWY4K3Rzd1VDVVVLTmxMMWh1dTVObEtrOVRVQzErZG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plY3ZLcXdkVURPOTk3MC8vaFJXM3QrSlk2ZUVFekhKZjNDCjcxVi96UXlCSnJmc01IYWZUNWd3QWx2SUhGUDI5MStIVFpaS2lTUFJBb0dCQU0wcDU3dkFMUC9FV20renp3ZlkKVEdvczZtVlB1MXhpOHhncEF6L1lTOTJ3ZWZhajNjWU84Y3VSZk84Zmg3RWg5elBEUmpqMnFvSXp0NlA2RlNvRwpuemwyNkxUeWh6SkRUS0JwNWN6OUtBcnZXNm0xKzF0R2FpREhwbmV4a1RBelZHTGxTK3BWUmZyYVRxUVdCaWFUCk1uMUxjR3BFZ2JYdHpkQmdONmk0SGxkL0FvR0FCUjB6VGV1L3h6WHJjNVdXdkRMbGtLQlA4YzVFak1BL2Urci8KOU5PeTJwS2JjNTNTclFUajQ0VmphTE95QnBCenJad01ka1pIZ0hLRngyQnVESnVKak16a3NpRzY0WU11dGFzVQpLUUNnWE9nc0w3eW85QzVURTVxSUVvenBUMzJzSnJYN21LeWR4VTJEVExtaVEwS0hObUtBR2dCcHZiOVdYZThDClIwTmgxWkVDZ1lCYmFNNm80Uk1oMmpiUTczU0d5YUU0Zi9HWUZYejBDNDlSYkd4Q2lJdWRKS3NlcUU4NmIyRUcKQVBhYVlFekRvSDZ0MXc2ejhSOURDRlpQT2p2QWJ3NzZpNlE5NTF2RCtqOHREcCtuSkhnRmJFMDkySUpkWHhKQQpxU3Rpb1hpWTduR3h2YkhDc3VsY3JBamFVNzU1TThhcVBEc2VXZmxyYThZR0RMYU8yZHBZRGc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=

切换回默认的命名空间
[root@kubernetes k8s]# kubectl config use-context kubernetes-admin@kubernetes
Switched to context "kubernetes-admin@kubernetes".
[root@kubernetes k8s]# 
[root@kubernetes k8s]# 
[root@kubernetes k8s]# kubectl get nodes
NAME               STATUS   ROLES    AGE    VERSION
kubernetes         Ready    master   182d   v1.13.3
kubernetes-node1   Ready    <none>   182d   v1.13.3
kubernetes-node2   Ready    <none>   182d   v1.13.3

给报错的用户绑定一个cluster-admin的权限
[root@kubernetes k8s]# kubectl create clusterrolebinding system:anonymous   –clusterrole=cluster-admin   –user=system:anonymous
clusterrolebinding.rbac.authorization.k8s.io/system:anonymous created

切换到ctx-deve
[root@kubernetes k8s]# kubectl config use-context ctx-dev

创建pod
[root@kubernetes k8s]# kubectl create -f redis-slave-conrtoller.yaml 
replicationcontroller/redis-slave created

查看创建的pod,创建成功用户的权限错误在上面授权时已经解决了)
[root@kubernetes k8s]# kubectl get pod
NAME                READY   STATUS              RESTARTS   AGE
redis-slave-pndnq   0/1     ContainerCreating   0          12s
redis-slave-r69b2   0/1     ContainerCreating   0          12s

等了一会,已经有一个pod已经运行起来了
[root@kubernetes k8s]# kubectl get pod
NAME                READY   STATUS             RESTARTS   AGE
redis-slave-pndnq   1/1     Running            0          6m52s
redis-slave-r69b2   0/1     ImagePullBackOff   0          6m52s

参看创建的另一个环境ctx-prod,看ctx-dev和该环境是否隔离开
[root@kubernetes k8s]# kubectl config use-context ctx-prod
Switched to context "ctx-prod".
[root@kubernetes k8s]# kubectl get cs
NAME                 STATUS    MESSAGE              ERROR
scheduler            Healthy   ok                   
controller-manager   Healthy   ok                   
etcd-0               Healthy   {"health": "true"}   

查看ctx-prod环境下的pod发现没有pod,说明ctx-dev和ctx-prod这两个环境隔离开,他们各自内部运行的服务不会发生干涉
[root@kubernetes k8s]# kubectl get pod
No resources found.

在ctx-prod也创建pod
[root@kubernetes k8s]# kubectl create -f redis-slave-conrtoller.yaml 
replicationcontroller/redis-slave created
[root@kubernetes k8s]# kubectl get pods
NAME                READY   STATUS              RESTARTS   AGE
redis-slave-hdhdd   0/1     ContainerCreating   0          6s
redis-slave-llt4s   1/1     Running             0          6s
 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注