The Kubernetes API provides a resource-based (RESTful) programming interface over HTTP. It supports standard HTTP requests, such as POST, PUT, PATCH, DELETE, and GET, to query, create, update, and delete cluster resources. You can use cURL commands or other programming methods to access the Kubernetes API. This topic provides examples of how to manage Pods and Deployments using cURL commands.
Obtain KubeConfig Access Credentials
Log on to the ACK console.
-
Click Go to RAM console to go to the Resource Access Management Quick Authorization page. Then, click Confirm.
After you grant the authorization, refresh the console to use ACK.
-
In the navigation pane on the left, click Clusters.
On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
-
Click the Connection Information tab. View the cluster access credential in the KubeConfig section. Save the KubeConfig file locally.
-
Run the following commands to extract CA, Key, and APIServer information from the KubeConfig file.
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}'`
Use cURL Commands to Operate the Kubernetes API
Run the following command to view all namespaces in the current cluster.
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces
The following examples show common operations for managing Pods and Deployments using cURL commands.
Common Pod Operations
Run the following command to view all Pods in the default namespace.
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods
Run the following command to create a Pod in JSON format.
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
Run the following command to create a Pod in YAML format.
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
Run the following command to query the status of a Pod.
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx
Run the following command to query the logs of a Pod.
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx/log
Run the following command to query Pod metrics using the metric-server API.
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/nginx
Run the following command to delete a Pod.
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx -X DELETE
Common Deployment Operations
The following is an example of a YAML file for creating a 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
Run the following command to view a Deployment.
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/apis/apps/v1/namespaces/default/deployments
Run the following command to update a Deployment by modifying the number of replicas.
curl --cert ./client-cert.pem --key ./client-cert.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}}'
Run the following command to update a Deployment by modifying the container image.
curl --cert ./client-cert.pem --key ./client-cert.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"}]}}}}'