k8S的node节点管理集群

[root@master ~]# kubectl get node -o wide
NAME     STATUS   ROLES                  AGE    VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
master   Ready    control-plane,master   218d   v1.20.0   10.70.36.250   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0
node1    Ready    node                   218d   v1.20.0   10.70.36.251   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0
node2    Ready    node                   218d   v1.20.0   10.70.36.252   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0

node节点管理集群

[root@node1 ~]# kubectl get node 
The connection to the server localhost:8080 was refused - did you specify the right host or port?
[root@node1 ~]# 

发现不行是因为node节点缺少kubeconfig,要想管理需要把mater上的/root/.kube/config复制到node节点的/root/.kube/config,前提是安装kubectl 

master节点

[root@master .kube]# scp    /root/.kube/config  root@node1:/root/
root@10.70.36.220's password: 
config 

node节点,前提是安装kubectl 

[root@node1 ~]# ls
anaconda-ks.cfg  config
[root@node1 ~]# mkdir /root/.kube
[root@node1 ~]# mv config  /root/.kube/
[root@node1 ~]# ll -a /root/.kube/
总用量 8
drwxr-xr-x  3 root root   33 7月  20 15:16 .
dr-xr-x---. 5 root root  184 7月  20 15:16 ..
drwxr-x---  4 root root   35 7月  20 15:16 cache
-rw-------  1 root root 5564 7月  20 15:15 config
[root@node1 ~]# kubectl get node 
NAME     STATUS   ROLES                  AGE    VERSION
master   Ready    control-plane,master   218d   v1.20.0
node1    Ready    node                   218d   v1.20.0
node2    Ready    node                   218d   v1.20.0

很多人疑惑kubeconfig哪里来的,在master节点

[root@master ~]# ll -a /etc/kubernetes/admin.conf 
-rw-------. 1 root root 5564 12月 14 2020 /etc/kubernetes/admin.conf
[root@master ~]# ll -a /root/.kube/config 
-rw-------. 1 root root 5564 12月 14 2020 /root/.kube/config

他们两个内容是一样的,在部署有这一步操作,他是这样来的

kubectl管理多个k8s集群 

有一台管理主机把每个k8s集群的config配置文件放到/root/.kube/目录下,改为不同名字,通过–kubeconfig实现不同集群操作

[root@cluster_management ~]# cat >>/etc/yum.repos.d/kubernetes.repo <<EOF
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
> enabled=1
> gpgcheck=1
> repo_gpgcheck=1
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF
[root@cluster_management ~]# yum install kubectl -y
[root@cluster_management ~]# ls
config
[root@cluster_management ~]# mkdir /root/.kube
[root@cluster_management ~]# mv config  /root/.kube/cluster1_config
[root@cluster_management ~]# ll -a /root/.kube/
总用量 8
drwxr-xr-x  3 root root   33 7月  20 15:16 .
dr-xr-x---. 5 root root  184 7月  20 15:16 ..
drwxr-x---  4 root root   35 7月  20 15:16 cache
-rw-------  1 root root 5564 7月  20 15:15 cluster1_config

 又有一个新集群可以把config改为cluster1_config

[root@cluster_management ~]# kubectl  --kubeconfig=/root/.kube/cluster1_config get node
NAME     STATUS   ROLES                  AGE    VERSION
master   Ready    control-plane,master   218d   v1.20.0
node1    Ready    node                   218d   v1.20.0
node2    Ready    node                   218d   v1.20.0
[root@cluster_management ~]# kubectl  --kubeconfig=/root/.kube/cluster1_config get node -o wide
NAME     STATUS   ROLES                  AGE    VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
master   Ready    control-plane,master   218d   v1.20.0   10.70.36.250   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0
node1    Ready    node                   218d   v1.20.0   10.70.36.251   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0
node2    Ready    node                   218d   v1.20.0   10.70.36.252   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0

又有一个新集群可以把config改为cluster2_config

[root@cluster_management ~]# kubectl  --kubeconfig=/root/.kube/cluster2_config get node
NAME     STATUS   ROLES                  AGE    VERSION
master   Ready    control-plane,master   21d   v1.20.0
node1    Ready    node                   21d   v1.20.0
node2    Ready    node                   21d   v1.20.0
[root@cluster_management ~]# kubectl  --kubeconfig=/root/.kube/cluster2_config get node -o wide
NAME     STATUS   ROLES                  AGE    VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
master   Ready    control-plane,master   21d   v1.20.0   10.70.36.240   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0
node1    Ready    node                   21d   v1.20.0   10.70.36.241   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0
node2    Ready    node                   21d   v1.20.0   10.70.36.242   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0

 当然上面的管理机器放在/root/.kube/下觉得费劲,可以自己建立一个文件夹cluster_management

[root@cluster_management ~]# mkdir cluster_management
[root@cluster_management ~]# cp /root/.kube/cluster1_config  cluster_management/
[root@cluster_management ~]# kubectl  --kubeconfig=cluster_management/cluster1_config get node -o wide
NAME     STATUS   ROLES                  AGE    VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
master   Ready    control-plane,master   218d   v1.20.0   10.70.36.250   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0
node1    Ready    node                   218d   v1.20.0   10.70.36.251   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0
node2    Ready    node                   218d   v1.20.0   10.70.36.252   <none>        CentOS Linux 7 (Core)   3.10.0-1062.el7.x86_64   docker://20.10.0

Logo

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

更多推荐