This topic describes how to call the Kubernetes API by using cURL to make REST calls. This allows you to send HTTPS requests to manage Kubernetes clusters. The following examples demonstrate how to create and delete pods, and how to create and modify Deployments.
Obtain cluster credentials in the kubeconfig file
Use cURL to call the Kubernetes API
Run the following command to query all namespaces in the cluster:
curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/api/v1/namespaces
- Common operations to manage pods
Run the following command to query 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 (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 (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 log data 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 collect metrics of a pod by using the metric-server API:
--certcurl --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 operations to manage Deployments
Create a Deployment. The following YAML template is an example:
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 query 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 replicated pods:
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}}'
Run the following command to update a Deployment by changing the container image:
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"}]}}}}'