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

Obtain cluster credentials

  1. Log on to the ACK console. On the Clusters page, find the target cluster and click Manage in the Actions column.Cluster management
  2. On the Basic Information page, you can find cluster credentials in the kubeconfig file. Copy the content of the kubeconfig file and save it to your local computer.kubeconfig
  3. Run the following commands to retrieve information about the certificate authority (CA), the private key, and 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

List all namespaces in the current cluster:

# curl --cert client-cert.pem --key client-key.pem -k $APISERVER/api/v1/namespaces
  • Commonly used commands 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 (in JSON format):

    # 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 (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

    Query pod status:

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

    Query pod logs:

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

    Query metrics about a pod (through the metric-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
  • Commonly used commands for deployment management

    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

    Query deployments:

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

    Modify a deployment (change the number of 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}}'

    Modify a deployment (change 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"}]}}}}'