Kubernetes API adalah antarmuka HTTP berbasis resource yang bersifat RESTful dan mendukung metode GET, POST, PUT, PATCH, dan DELETE. Gunakan API ini untuk mengkueri dan mengelola resource kluster secara programatik—tanpa kubectl. Lakukan autentikasi dengan sertifikat klien dan panggil API menggunakan cURL untuk mengelola Pod dan Deployment.
Prasyarat
Sebelum memulai, pastikan Anda telah memiliki:
-
Kluster ACK
-
Izin untuk mengakses Konsol ACK
-
cURL yang terinstal di mesin lokal Anda
Ekstrak kredensial dari file kubeconfig
-
Masuk ke Konsol ACK.
-
Klik Go to RAM console untuk membuka halaman Otorisasi Cepat Resource Access Management, lalu klik Confirm Authorization Policy. Setelah otorisasi selesai, refresh Konsol ACK.
-
Di panel navigasi sebelah kiri, klik Clusters.
-
Pada halaman Clusters, klik nama kluster atau klik Details di kolom Actions.
-
Klik tab Connection Information. Salin konten kubeconfig dan simpan secara lokal sebagai
./kubeconfig. -
Ekstrak sertifikat klien, kunci privat, dan alamat server API dari file kubeconfig:
cat ./kubeconfig | grep client-certificate-data | awk -F ' ' '{print $2}' | base64 -d > ./client-cert.pem cat ./kubeconfig | grep client-key-data | awk -F ' ' '{print $2}' | base64 -d > ./client-key.pem APISERVER=`cat ./kubeconfig | grep server | awk -F ' ' '{print $2}'`Perintah-perintah ini mendekode data sertifikat dan menyimpan URL server API ke dalam variabel
$APISERVER. Semua perintah cURL berikutnya menggunakan kredensial ini dan variabel$APISERVER.
Flag-kmelewati verifikasi server TLS. Untuk lingkungan produksi, ganti-kdengan--cacert <path-to-ca.pem>untuk memverifikasi identitas server dan mencegah serangan man-in-the-middle.
Kelola Pod
Semua panggilan API Pod menggunakan path $APISERVER/api/v1/namespaces/<namespace>/pods.
| Operasi | Metode HTTP | Path |
|---|---|---|
| Daftar semua namespace | GET | /api/v1/namespaces |
| Daftar semua Pod | GET | /api/v1/namespaces/default/pods |
| Buat Pod | POST | /api/v1/namespaces/default/pods |
| Dapatkan status Pod | GET | /api/v1/namespaces/default/pods/nginx |
| Dapatkan log Pod | GET | /api/v1/namespaces/default/pods/nginx/log |
| Dapatkan metrik Pod | GET | /apis/metrics.k8s.io/v1beta1/namespaces/default/pods/nginx |
| Hapus Pod | DELETE | /api/v1/namespaces/default/pods/nginx |
Daftar semua namespace di kluster:
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces
Daftar semua Pod di namespace default:
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods
Buat Pod dari manifes JSON:
cat nginx-pod.json
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "nginx",
"namespace": "default"
},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx:alpine",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods \
-X POST --header 'content-type: application/json' -d@nginx-pod.json
Buat Pod dari manifes YAML:
cat nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: default
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods \
-X POST --header 'content-type: application/yaml' --data-binary @nginx-pod.yaml
Dapatkan status Pod:
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx
Dapatkan log Pod:
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx/log
Dapatkan metrik Pod melalui Metrics Server API:
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/nginx
Hapus Pod:
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx -X DELETE
Kelola Deployment
Semua panggilan API Deployment menggunakan path $APISERVER/apis/apps/v1/namespaces/<namespace>/deployments.
Buat Deployment:
cat nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
resources:
requests:
cpu: "2"
memory: "4Gi"
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/apis/apps/v1/namespaces/default/deployments \
-X POST --header 'content-type: application/yaml' --data-binary @nginx-deploy.yaml
Daftar semua Deployment di namespace default:
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/apis/apps/v1/namespaces/default/deployments
Skalakan Deployment:
curl --cert ./client-cert.pem --key ./client-key.pem -k \
$APISERVER/apis/apps/v1/namespaces/default/deployments/nginx-deploy \
-X PATCH -H 'Content-Type: application/strategic-merge-patch+json' \
-d '{"spec": {"replicas": 4}}'
Perbarui gambar kontainer:
curl --cert ./client-cert.pem --key ./client-key.pem -k \
$APISERVER/apis/apps/v1/namespaces/default/deployments/nginx-deploy \
-X PATCH -H 'Content-Type: application/strategic-merge-patch+json' \
-d '{"spec": {"template": {"spec": {"containers": [{"name": "nginx","image": "nginx:1.7.9"}]}}}}'
Langkah selanjutnya
Untuk akses programatik di luar cURL, gunakan pustaka klien Kubernetes resmi:
| Bahasa | Pustaka klien | Contoh |
|---|---|---|
| Go | client-go | Jelajahi |
| Python | client-python | Jelajahi |
| Java | client-java | Jelajahi |
Lihat Kubernetes client libraries untuk daftar lengkap bahasa yang didukung.
Jelajahi metode akses alternatif di Access clusters using the Kubernetes API.