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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

HTML5秘籍

HTML5秘籍

Matthew MacDonald / 李松峰、朱巍 / 人民邮电出版社 / 2012-8 / 79.00元

《HTML5秘籍》共包括四个部分,共12章。第一部分介绍了html5的发展历程,利用html5重新构造网页,以及html5的语义元素。第二部分介绍了html5对传统web表单的翻新、html5中的音频与视频、canvas绘图技术、css3等内容。第三部分介绍了数据存储、离线应用、与web服务器通信,以及html5与javascript技术的强大结合等内容。第四部分为附录,简单介绍了css和java......一起来看看 《HTML5秘籍》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

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

HEX CMYK 互转工具