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

查看所有标签

猜你喜欢:

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

天涯虚拟社区

天涯虚拟社区

刘华芹 / 民族出版社 / 2005-11 / 23.00元

网络空间很复杂,好多人并不完全了解或者只是了解到一些皮毛。比如说好多人对于见网友一事总是抱着浪漫或者暖昧的想法,而事实却并不总是想象的那样。作者在做虚拟社区研究甚至是在有这个想法之前并不常呆在网上,互联网对于作者来说就是查查资料、收发信年、看看新闻的工具。担是看着越来越多的人把时间花在网上,一处文化上的直觉告诉作者:有一种新的生活方式产生了。强烈的好奇心驱使着作者走到了网上,走到了天涯虚拟社区,并......一起来看看 《天涯虚拟社区》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

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

HEX CMYK 互转工具