创建Namespace

创建命名空间1

# namespace-development.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: development

创建命名空间2

# namespace-production.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: production

创建命名空间

  • kubectl create -f namespace-development.yaml
  • kubectl create -f namespace-production.yaml

查看命名空间

  • kubectl get namespace

定义Context

Context,即运行环境。这个运行环境将属于某个特定的命名空间

查看config文件

# 查看命令 kubectl config view
# [root@k8s-master1 namespace]# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.73.130:12567
  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

# 文件中默认的clusters名称为kubernetes,users的名称为kubernetes-admin
# 创建Context时,需要使用到这些信息
# 使用这两个信息,省去其他的认证过程

kubectl config set-context命令定义Context,并将Context置于之前创建的命名空间中

  • cluster信息及user信息见config文件
  • kubectl config set-cluster kubernetes --server=https://192.168.73.130:12567
  • kubectl config set-context ctx-dev --namespace=development --cluster=kubernetes --user=kubernetes-admin
  • kubectl config set-context ctx-prod --namespace=production --cluster=kubernetes --user=kubernetes-admin

kubectl config view命令查看已定义的Context

  • kubectl config view

查看的config内容

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.73.130:12567
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    namespace: development
    user: kubernetes-admin
  name: ctx-dev
- context:
    cluster: kubernetes
    namespace: production
    user: kubernetes-admin
  name: ctx-prod
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: ctx-dev
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

也可以手工编辑 ${HOME}/.kube/config 文件来设置Context

设置工作组在特定Context中工作

用户授权

  • kubectl create clusterrolebinding system:anonymous --clusterrole=cluster-admin --user=system:anonymous

通过kubectl config use-context <context_name>命令设置当前运行环境

  • kubectl config use-context ctx-dev
  • kubectl config use-context ctx-prod

测试

切换到ctx-dev ,创建pod,然后切换到ctx-prod ,查看pod是否存在,再创建,查看同样的两个pod是否能同时存在

  • kubectl config use-context ctx-dev
  • cat test-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cm-test-app
spec:
  containers:
  - name: cm-test-app
    #image: kubeguide/tomcat-app:v1
    image: busybox:latest
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
    #ports:
    #- containerPort: 8080
  • kubectl create -f test-pod.yaml
  • kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
cm-test-app   1/1     Running   0          31s

  • kubectl config use-context ctx-prod
  • kubectl get pods
  • kubectl create -f test-pod.yaml
  • kubectl get pods --all-namespaces -o wide |grep cm-test-app
# 同样的容器,在不同空间中,这两个空间已经隔离开
development            cm-test-app                                  1/1     Running             0               8m42s
production             cm-test-app                                  1/1     Running             0               25s
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐