すべてのプロダクト
Search
ドキュメントセンター

Container Service for Kubernetes:Kubernetes API の使用

最終更新日:Feb 26, 2026

Kubernetes API は、HTTP 上でリソースベース(RESTful)のプログラミングインターフェイスを提供します。クラスターリソースのクエリ、作成、更新、削除を行うための標準的な HTTP リクエスト(POST、PUT、PATCH、DELETE、GET など)をサポートしています。cURL コマンドやその他のプログラミング手法を使用して Kubernetes API にアクセスできます。本トピックでは、cURL コマンドを使用した Pod および Deployment の管理方法について例を示します。

KubeConfig アクセス認証情報の取得

  1. Container Service for Kubernetes (ACK) コンソールまたは コンソールにログインします。

  2. RAM コンソールに移動 をクリックして Resource Access Management Quick Authorization ページに移動します。その後、Confirm をクリックします。

    権限付与後、ACK を使用するにはコンソールをリフレッシュしてください。

  3. 左側のナビゲーションウィンドウで クラスターリスト をクリックします。

  4. [クラスター] ページで、管理対象のクラスターを見つけ、クラスター名をクリックするか、[操作] 列の [詳細] をクリックします。クラスターの詳細ページが表示されます。

  5. 接続情報 タブをクリックします。[KubeConfig] セクションでクラスターのアクセス認証情報を確認し、KubeConfig ファイルをローカルに保存します。

  6. 以下のコマンドを実行して、KubeConfig ファイルから CA、キー、APIServer 情報を抽出します。

    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}'`

cURL コマンドによる Kubernetes API の操作

以下のコマンドを実行して、現在のクラスター内のすべての名前空間を表示します。

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

以下に、cURL コマンドを使用した Pod および Deployment の管理に関する一般的な操作例を示します。

Pod の一般的な操作

以下のコマンドを実行して、デフォルト名前空間内のすべての Pod を表示します。

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

以下のコマンドを実行して、JSON 形式で Pod を作成します。

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

以下のコマンドを実行して、YAML 形式で Pod を作成します。

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

以下のコマンドを実行して、Pod のステータスをクエリします。

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

以下のコマンドを実行して、Pod のログをクエリします。

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

以下のコマンドを実行して、metric-server API を使用して Pod のメトリックをクエリします。

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

以下のコマンドを実行して、Pod を削除します。

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

Deployment の一般的な操作

以下は、Deployment を作成するための YAML ファイルの例です。

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

以下のコマンドを実行して、Deployment を表示します。

curl --cert ./client-cert.pem --key ./client-key.pem -k $APISERVER/apis/apps/v1/namespaces/default/deployments

以下のコマンドを実行して、レプリカ数を変更することで Deployment を更新します。

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}}'

以下のコマンドを実行して、コンテナイメージを変更することで Deployment を更新します。

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"}]}}}}'