How to know your on k8s master node?
Asked Answered
P

2

5

I'm writing some scripts that check the system to make sure of some cluster characteristics. Things running on private IP address spaces, etc. These checks are just a manual step when setting up a cluster, and used just for sanity checking.

They'll be run on each node, but I'd like a set of them to run when on the master node. Is there a bash, curl, kubectl, or another command that has information indicating the current node is a master node?

Phonemic answered 10/10, 2018 at 17:36 Comment(0)
K
5

The master(s) usually has the 'master' role associated with it. For example:

$ 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

It also has a label node-role.kubernetes.io/master associated with it. For example:

$ 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: ""
Kovacev answered 10/10, 2018 at 17:49 Comment(0)
C
4

Some more ways:

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

You can use kubectl with label selector:

$ 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

And you can get specific data via jsonpath, e.g master IPs/hostnames:

$ 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 
Cowpea answered 11/10, 2018 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.