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

查看所有标签

猜你喜欢:

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

王道程序员求职宝典

王道程序员求职宝典

电子工业出版社 / 2013-11 / 56.00元

本书精选了大量知名企业的程序员笔试、面试题,重点突出、解答翔实。全书共分为四部分,各部分如下:第一部分是程序设计基础及数据结构基础,讨论C/C++基础知识以及数据结构基础知识;第二部分是计算机网络基础,讨论网络模型、套接字编程基本操作、IPv4与IPv6、子网划分、网络常用测试工具等;第三部分是操作系统基础,讨论进程与线程的基本知识、进程间通信与进程同步、内存管理的相关知识等;第四部分是其他计算机......一起来看看 《王道程序员求职宝典》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

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

正则表达式在线测试