Calling the Kubernetes API
Jun 13, 2023
2 minute read
There are three ways to call the apiserver:
Use the official SDK;
From inside a Pod, use the service account token over port 443 — this gives better permission control and is easier to migrate (recommended);
Send requests directly to port 6443.
Official SDK
The official GitHub has support for many languages: Go, Java, Perl, Ruby. There are plenty of simple examples, so I will not belabor the point — just Google it.
Python SDK:
https://github.com/kubernetes-client/python
Calling from inside a Pod
Fetch the token first, then issue the request against the apiserver.
1
2
TOKEN = $( cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt -H "Authorization: Bearer $TOKEN " -s https://10.96.0.1:443/api/v1/namespaces/default/pods/
Reference:
https://blog.csdn.net/russle/article/details/105333738
Calling port 6443
Shell version:
1
2
3
4
curl https://192.168.1.100:6443/api/v1/nodes \
--cacert /etc/kubernetes/pki/ca.crt \
--cert /etc/kubernetes/pki/apiserver-kubelet-client.crt \
--key /etc/kubernetes/pki/apiserver-kubelet-client.key
Python version:
u
t
e
e
r
r
x
l
l
y
c
s
p
:
e
e
r
=
r
c
p
p
:
i
e
e
t
r
n
"
s
r
i
t
h
t
E
n
(
t
=
=
x
t
"
t
(
c
(
o
p
r
"
e
e
k
s
e
/
p
)
"
:
q
e
t
)
/
u
t
i
/
e
c
i
s
n
p
t
k
:
s
u
a
6
.
b
s
4
g
e
4
e
r
e
3
t
n
:
/
(
e
a
u
t
p
r
e
i
l
s
/
,
/
v
p
1
v
k
/
e
i
n
r
/
o
i
a
d
f
p
e
y
i
s
=
s
/
"
e
u
/
r
n
e
v
i
t
e
s
c
r
6
/
-
"
k
k
u
u
b
b
e
e
r
l
n
e
e
t
t
-
e
c
s
l
/
i
p
e
k
n
i
t
/
.
c
c
a
r
.
t
p
"
e
,
m
"
"
/
,
e
t
c
/
k
u
b
e
r
n
e
t
e
s
/
p
k
i
/
a
p
i
s
e
r
v
e
r
-
k
u
b
e
l
e
t
-
c
l
i
e
n
t
.
k
e
y
"
)
,
t
i
m
e
o
u
t
=
1
5
)
Some ways to call the metrics endpoint:
https://blog.csdn.net/u014106644/article/details/84839055