This topic describes how to access the Kubernetes API by using REST calls based on curl commands. You can perform operations on Kubernetes clusters over the HTTPS protocol. The following examples describe how to create and delete pods, and how to create and modify deployments.

Obtain cluster credentials

  1. Log on to the the ACK console. On the Clusters page, find the cluster that you want to manage and click Details in the Actions column.Cluster management
  2. On the management page of the cluster, click the Connection Information tab. You can find cluster credentials in the kubeconfig file. Copy the content of the kubeconfig file and save it to the on-premises machine.
  3. Run the following commands to retrieve information about the certificate authority (CA), the private key, and the API server:
    # 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}'`

Access the Kubernetes API through curl commands

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 for pod management

    List all pods in the default namespace:

    # curl --cert client-cert.pem --key client-key.pem -k $APISERVER/api/v1/namespaces/default/pods

    Create a pod (JSON):

    # cat nginx-pod.json
    {
        "apiVersion":"v1",
        "kind":"Pod",
        "metadata":{
            "name":"nginx",
            "namespace": "default"
        },
        "spec":{
            "containers":[
                {
                    "name":"ngnix",
                    "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

    Create a pod (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

    Query the status of a pod:

    # curl --cert client-cert.pem --key client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx

    Query the logs of a pod:

    # curl --cert client-cert.pem --key client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx/log

    Query the metrics about a pod through the metrics server API:

    # curl --cert client-cert.pem --key client-key.pem -k $APISERVER/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/nginx

    Delete a pod:

    # curl --cert client-cert.pem --key client-key.pem -k $APISERVER/api/v1/namespaces/default/pods/nginx -X DELETE
  • Common operations for deployment management

    Use a YAML template to create a deployment:

    # cat nginx-deploy.yaml
    apiVersion: extensions/v1beta1
    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/extensions/v1beta1/namespaces/default/deployments -X POST --header 'content-type: application/yaml' --data-binary @nginx-deploy.yaml

    List all deployments in the default namespace:

    # curl --cert client-cert.pem --key client-key.pem -k $APISERVER/apis/extensions/v1beta1/namespaces/default/deployments

    Update a deployment by modifying the number of pod replicas:

    # curl --cert client-cert.pem --key client-key.pem -k $APISERVER/apis/extensions/v1beta1/namespaces/default/deployments/nginx-deploy -X PATCH -H 'Content-Type: application/strategic-merge-patch+json' -d '{"spec": {"replicas": 4}}'

    Update a deployment by modifying the container image:

    # curl --cert client-cert.pem --key client-key.pem -k $APISERVER/apis/extensions/v1beta1/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"}]}}}}'