如何知道您在k8s主节点上?

时间:2018-10-10 17:36:30

标签: kubernetes kubectl kubeadm

我正在编写一些脚本来检查系统,以确保某些群集特征。事情在私有IP地址空间等上运行。这些检查只是设置集群时的手动步骤,仅用于完整性检查。

它们将在每个节点上运行,但是我希望它们在主节点上运行。是否有bash,curl,kubectl或其他具有指示当前节点为主节点的信息的命令?

2 个答案:

答案 0 :(得分:2)

主机通常具有与其关联的“主机”角色。例如:

$ kubectl get nodes
NAME                                         STATUS    ROLES     AGE       VERSION
ip-x-x-x-x.us-west-2.compute.internal        Ready     <none>    7d        v1.11.2
ip-x-x-x-x.us-west-2.compute.internal        Ready     master    78d       v1.11.2
ip-x-x-x-x.us-west-2.compute.internal        Ready     <none>    7d        v1.11.2
ip-x-x-x-x.us-west-2.compute.internal        Ready     <none>    7d        v1.11.2
ip-x-x-x-x.us-west-2.compute.internal        Ready     <none>    7d        v1.11.2

它还具有与之关联的标签node-role.kubernetes.io/master。例如:

$ kubectl get node ip-x-x-x-x.us-west-2.compute.internal -o=yaml
apiVersion: v1
kind: Node
metadata:
  annotations:
    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
    node.alpha.kubernetes.io/ttl: "0"
    projectcalico.org/IPv4Address: x.x.x.x/20
    volumes.kubernetes.io/controller-managed-attach-detach: "true"
  creationTimestamp: 2018-07-23T21:10:22Z
  labels:
    beta.kubernetes.io/arch: amd64
    beta.kubernetes.io/instance-type: t3.medium
    beta.kubernetes.io/os: linux
    failure-domain.beta.kubernetes.io/region: us-west-2
    failure-domain.beta.kubernetes.io/zone: us-west-2c
    kubernetes.io/hostname: ip-x-x-x-x.us-west-2.compute.internal
    node-role.kubernetes.io/master: ""

答案 1 :(得分:0)

更多方法:

$ kubectl cluster-info 
Kubernetes master is running at https://node1.example.com:8443
...

您可以将kubectl与标签选择器一起使用:

$ kubectl get nodes -l node-role.kubernetes.io/master=true
NAME                STATUS    ROLES     AGE       VERSION
node1.example.com   Ready     master    1d        v1.10.5
node2.example.com   Ready     master    1d        v1.10.5

您可以通过jsonpath获取特定数据,例如主IP /主机名:

$ kubectl get nodes -l node-role.kubernetes.io/master=true -o 'jsonpath={.items[*].status.addresses[?(@.type=="InternalIP")].address}'
192.168.168.197 192.168.168.198

$ kubectl get nodes -l node-role.kubernetes.io/master=true -o 'jsonpath={.items[*].status.addresses[?(@.type=="Hostname")].address}'
node1.example.com node2.example.com