Kubectl – Configuration Guide

栏目: IT技术 · 发布时间: 4年前

内容简介:Kubectl client gives us possibility to configure and switch easy between multiple K8s clusters.Main location forWe can have multiple config files by setting proper

Kubectl client gives us possibility to configure and switch easy between multiple K8s clusters.

Config files location for kubectl

Main location for kubectl config files is $HOME/.kube , by default we have single config file named config:

[node1 ~]$ cd $HOME/.kube
[node1 .kube]$ ls -lah
total 4.0K
drwxr-xr-x 1 root root   37 Mar 12 20:48 .
dr-xr-x--- 1 root root   19 Nov 29 11:46 ..
drwxr-x--- 3 root root   23 Mar 12 20:48 cache
lrwxrwxrwx 1 root root   26 Nov 29 11:46 config -> /etc/kubernetes/admin.conf
drwxr-x--- 3 root root 4.0K Mar 12 20:48 http-cache

Multiple config files and KUBECONFIG variable

We can have multiple config files by setting proper KUBECONFIG shell variable.

For example:

export KUBECONFIG=$KUBECONFIG:/root/.kube/additional_config

Check config from kubectl

We can check current config(it will be empty because I don’t have anything in additional_config file - we’ll add something later - and this is only file in my KUBECONFIG variable):

[node1 ~]$ kubectl config view
apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []

Or from specific file - let’s see default file:

[node1 ~]$ kubectl config --kubeconfig=.kube/config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.0.38: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

If you don’t set KUBECONFIG variable all configuration will be taken from $HOME/.kube/config

Objects in kubectl config

kubectl has in his config files following object types:

  • clusters - info about K8s cluster - contain cluster name and connection parameters
  • users - info about users by which you want connect to K8s clusters
  • contexts - triple of cluster/user/namespace

Modifying config from kubectl

Add cluster

[node1 ~]$ kubectl config --kubeconfig=.kube/additional_config set-cluster dev --server=https://192.168.1.100 --certificate-authority=fake-ca
Cluster "dev" set.
[node1 ~]$ kubectl config --kubeconfig=.kube/additional_config set-cluster prod --server=https://10.1.1.100 --certificate-authority=fake-ca
Cluster "prod" set.

Add users

[node1 ~]$ kubectl config --kubeconfig=.kube/additional_config set-credentials developer --client-certificate=fake-cert-file --client-key=fake-key-seefile
User "developer" set.
[node1 ~]$ kubectl config --kubeconfig=.kube/additional_config set-credentials prod_admin --client-certificate=fake-cert-file --client-key=fake-key-seefile
User "prod_admin" set.

Add contexts

[node1 ~]$ kubectl config --kubeconfig=.kube/additional_config set-context simple_app_development --cluster=dev --namespace=simple_app_dev --user=developer
Context "simple_app_development" created.
[node1 ~]$ kubectl config --kubeconfig=.kube/additional_config set-context simple_app_production --cluster=prod --namespace=simple_app_prod --user=prod_admin
Context "simple_app_production" created.

Delete cluster/user/context from config

kubectl --kubeconfig=.kube/additional_config config unset users.<name>
kubectl --kubeconfig=.kube/additional_config config unset clusters.<name>
kubectl --kubeconfig=.kube/additional_config config unset contexts.<name>

Sample config

[node1 ~]$ kubectl config --kubeconfig=.kube/additional_config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /root/fake-ca
    server: https://192.168.1.100
  name: dev
- cluster:
    certificate-authority: /root/fake-ca
    server: https://10.1.1.100
  name: prod
contexts:
- context:
    cluster: dev
    namespace: simple_app_dev
    user: developer
  name: simple_app_development
- context:
    cluster: prod
    namespace: simple_app_prod
    user: prod_admin
  name: simple_app_production
current-context: ""
kind: Config
preferences: {}
users:
- name: developer
  user:
    client-certificate: /root/fake-cert-file
    client-key: /root/fake-key-seefile
- name: prod_admin
  user:
    client-certificate: /root/fake-cert-file
    client-key: /root/fake-key-seefile

Changing context we working with

To get list of contexts(no need for --kubeconfig because we have our additional_config added to KUBECONFIG variable):

[node1 ~]$ kubectl config get-contexts
CURRENT   NAME                     CLUSTER   AUTHINFO     NAMESPACE
          simple_app_development   dev       developer    simple_app_dev
          simple_app_production    prod      prod_admin   simple_app_prod

To set context:

[node1 ~]$ kubectl config use-context simple_app_development
Switched to context "simple_app_development".

To get current context:

[node1 ~]$ kubectl config current-context
simple_app_development

For those that want to know even more… Worth to read!

Kubeconfig context as bash prompt - Georgi from Gardener - shell tricks and Windows CLI aproach


以上所述就是小编给大家介绍的《Kubectl – Configuration Guide》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

VC++.NET入门

VC++.NET入门

Davis Chapman / 中国电力出版社 / 2003-5 / 55.0

《VC++.NET入门》提供了学习Visual C++工具的循序渐进的指导和创建应用程序的向导。你将学习设计应用程序窗口、使用控件、显示图形、创建SDI和MDI应用程序、操作数据库以及创建多任务程序,也将学习在微软的新.E平台下使用Visual C++的一些基本技巧,以及学习它影响设计和编写应用程序的方式。一起来看看 《VC++.NET入门》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具