쿠버네티스

[쿠버네티스] curl로 API 접근하기

트리스탄1234 2024. 3. 5. 06:05
728x90
반응형

안녕하세요 이웃님들 ^.^
좋은 아침 입니다.
 
오늘은 쿠버네티스 환경에서 KUBECTL을 사용하지 않고 crul을 사용해 API에 접근 하는 
방법에 대해서 포스팅 해보겠습니다.
 
우선 API에 접근 하기  위해선 API의 정보를 알아야 겠죠?
그럼 아래의 절차대로 실행을 해서 API정보를 알아 볼까요?
 
1) 우선 Kubectl의 설정 정보를 확인 합니다.

반응형
ubuntu@ip-172-31-31-84:~$ cat ~/.kube/config
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /home/ubuntu/.minikube/ca.crt
    extensions:
    - extension:
        last-update: Sat, 02 Mar 2024 10:34:20 UTC
        provider: minikube.sigs.k8s.io
        version: v1.28.0
      name: cluster_info
    server: https://172.31.31.84:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    extensions:
    - extension:
        last-update: Sat, 02 Mar 2024 10:34:20 UTC
        provider: minikube.sigs.k8s.io
        version: v1.28.0
      name: context_info
    namespace: default
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /home/ubuntu/.minikube/profiles/minikube/client.crt
    client-key: /home/ubuntu/.minikube/profiles/minikube/client.key

여기서 아래 부분을 별도 메모해 놓으세요. 

  • clusters.cluster.server: https://172.31.31.84:8443
  • clusters.cluster.certificate-authority: /home/ubuntu/.minikube/ca.crt
  • users.user.client-certificate: /home/ubuntu/.minikube/profiles/minikube/client.crt
  • users.user.client-key: /home/ubuntu/.minikube/profiles/minikube/client.key

2) 다음 노드 정보를 아래와 같이 조회 합니다.

ubuntu@ip-172-31-31-84:~$ kubectl explain node
KIND:     Node
VERSION:  v1

DESCRIPTION:
     Node is a worker node in Kubernetes. Each node will have a unique
     identifier in the cache (i.e. in etcd).

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
     Spec defines the behavior of a node.
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       <Object>
     Most recently observed status of the node. Populated by the system.
     Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

ubuntu@ip-172-31-31-84:~$

여기서 version값을 확인 하세요.


 
그럼 이제 위에서 확인한 값을 이용해서 아래와 같이 API에 접속을 해보겠습니다.

curl https://172.31.31.84:8443/api/v1/nodes --cacert /home/ubuntu/.minikube/ca.crt --cert /home/ubuntu/.minikube/profiles/minikube/client.crt --key /home/ubuntu/.minikube/profiles/minikube/client.key

 
아래는 결과 값입니다.

{
  "kind": "NodeList",
  "apiVersion": "v1",
  "metadata": {
    "resourceVersion": "1123"
  },
  "items": [
    {
      "metadata": {
        "name": "ip-172-31-31-84",
        "uid": "f9f408d5-cfc6-4b04-933c-1f025f403a4d",
        "resourceVersion": "892",
        "creationTimestamp": "2024-03-02T10:34:16Z",
        "labels": {
          "beta.kubernetes.io/arch": "amd64",
          "beta.kubernetes.io/os": "linux",
          "kubernetes.io/arch": "amd64",
          "kubernetes.io/hostname": "ip-172-31-31-84",
          "kubernetes.io/os": "linux",
          "minikube.k8s.io/commit": "986b1ebd987211ed16f8cc10aed7d2c42fc8392f",
          "minikube.k8s.io/name": "minikube",
          "minikube.k8s.io/primary": "true",
          "minikube.k8s.io/updated_at": "2024_03_02T10_34_20_0700",
          "minikube.k8s.io/version": "v1.28.0",
          "node-role.kubernetes.io/control-plane": "",
          "node-role.kubernetes.io/master": "",
          "node.kubernetes.io/exclude-from-external-load-balancers": ""
        },
        "annotations": {
          "kubeadm.alpha.kubernetes.io/cri-socket": "/var/run/dockershim.sock",
          "node.alpha.kubernetes.io/ttl": "0",
          "volumes.kubernetes.io/controller-managed-attach-detach": "true"
        },
        "managedFields": [
          {
            "manager": "Go-http-client",
            "operation": "Update",
            "apiVersion": "v1",
            "time": "2024-03-02T10:34:19Z",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:annotations": {
                  ".": {},
                  "f:kubeadm.alpha.kubernetes.io/cri-socket": {},
                  "f:volumes.kubernetes.io/controller-managed-attach-detach": {}
                },
                "f:labels": {
                  ".": {},
                  "f:beta.kubernetes.io/arch": {},
                  "f:beta.kubernetes.io/os": {},
                  "f:kubernetes.io/arch": {},
                  "f:kubernetes.io/hostname": {},
                  "f:kubernetes.io/os": {},
                  "f:node-role.kubernetes.io/control-plane": {},
                  "f:node-role.kubernetes.io/master": {},
                  "f:node.kubernetes.io/exclude-from-external-load-balancers": {}
                }
              }
            }
          },
          {
            "manager": "kubectl-label",
            "operation": "Update",
            "apiVersion": "v1",
            "time": "2024-03-02T10:34:20Z",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:labels": {
                  "f:minikube.k8s.io/commit": {},
                  "f:minikube.k8s.io/name": {},
                  "f:minikube.k8s.io/primary": {},
                  "f:minikube.k8s.io/updated_at": {},
                  "f:minikube.k8s.io/version": {}
                }
              }
            }
          },
          {
            "manager": "kube-controller-manager",
            "operation": "Update",
            "apiVersion": "v1",
            "time": "2024-03-02T10:34:32Z",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:annotations": {
                  "f:node.alpha.kubernetes.io/ttl": {}
                }
              },
              "f:spec": {
                "f:podCIDR": {},
                "f:podCIDRs": {
                  ".": {},
                  "v:\"10.244.0.0/24\"": {}
                }
              }
            }
          },
          {
            "manager": "Go-http-client",
            "operation": "Update",
            "apiVersion": "v1",
            "time": "2024-03-02T10:34:51Z",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:status": {
                "f:conditions": {
                  "k:{\"type\":\"DiskPressure\"}": {
                    "f:lastHeartbeatTime": {}
                  },
                  "k:{\"type\":\"MemoryPressure\"}": {
                    "f:lastHeartbeatTime": {}
                  },
                  "k:{\"type\":\"PIDPressure\"}": {
                    "f:lastHeartbeatTime": {}
                  },
                  "k:{\"type\":\"Ready\"}": {
                    "f:lastHeartbeatTime": {},
                    "f:lastTransitionTime": {},
                    "f:message": {},
                    "f:reason": {},
                    "f:status": {}
                  }
                },
                "f:images": {}
              }
            },
            "subresource": "status"
          }
        ]
      },
      "spec": {
        "podCIDR": "10.244.0.0/24",
        "podCIDRs": [
          "10.244.0.0/24"
        ]
      },
      "status": {
        "capacity": {
          "cpu": "2",
          "ephemeral-storage": "30297152Ki",
          "hugepages-1Gi": "0",
          "hugepages-2Mi": "0",
          "memory": "8024104Ki",
          "pods": "110"
        },
        "allocatable": {
          "cpu": "2",
          "ephemeral-storage": "30297152Ki",
          "hugepages-1Gi": "0",
          "hugepages-2Mi": "0",
          "memory": "8024104Ki",
          "pods": "110"
        },
        "conditions": [
          {
            "type": "MemoryPressure",
            "status": "False",
            "lastHeartbeatTime": "2024-03-02T10:39:57Z",
            "lastTransitionTime": "2024-03-02T10:34:14Z",
            "reason": "KubeletHasSufficientMemory",
            "message": "kubelet has sufficient memory available"
          },
          {
            "type": "DiskPressure",
            "status": "False",
            "lastHeartbeatTime": "2024-03-02T10:39:57Z",
            "lastTransitionTime": "2024-03-02T10:34:14Z",
            "reason": "KubeletHasNoDiskPressure",
            "message": "kubelet has no disk pressure"
          },
          {
            "type": "PIDPressure",
            "status": "False",
            "lastHeartbeatTime": "2024-03-02T10:39:57Z",
            "lastTransitionTime": "2024-03-02T10:34:14Z",
            "reason": "KubeletHasSufficientPID",
            "message": "kubelet has sufficient PID available"
          },
          {
            "type": "Ready",
            "status": "True",
            "lastHeartbeatTime": "2024-03-02T10:39:57Z",
            "lastTransitionTime": "2024-03-02T10:34:31Z",
            "reason": "KubeletReady",
            "message": "kubelet is posting ready status. AppArmor enabled"
          }
        ],
        "addresses": [
          {
            "type": "InternalIP",
            "address": "172.31.31.84"
          },
          {
            "type": "Hostname",
            "address": "ip-172-31-31-84"
          }
        ],
        "daemonEndpoints": {
          "kubeletEndpoint": {
            "Port": 10250
          }
        },
        "nodeInfo": {
          "machineID": "ec2b0e64d9caf49c013fe5118624adb4",
          "systemUUID": "ec2b0e64-d9ca-f49c-013f-e5118624adb4",
          "bootID": "d09f992d-f06e-48a9-b4fd-4f64abe232a3",
          "kernelVersion": "5.15.0-1055-aws",
          "osImage": "Ubuntu 20.04.6 LTS",
          "containerRuntimeVersion": "docker://20.10.23",
          "kubeletVersion": "v1.23.0",
          "kubeProxyVersion": "v1.23.0",
          "operatingSystem": "linux",
          "architecture": "amd64"
        },
        "images": [
          {
            "names": [
              "k8s.gcr.io/etcd@sha256:64b9ea357325d5db9f8a723dcf503b5a449177b17ac87d69481e126bb724c263",
              "k8s.gcr.io/etcd:3.5.1-0"
            ],
            "sizeBytes": 292558922
          },
          {
            "names": [
              "k8s.gcr.io/ingress-nginx/controller@sha256:5516d103a9c2ecc4f026efbd4b40662ce22dc1f824fb129ed121460aaa5c47f8"
            ],
            "sizeBytes": 289315470
          },
          {
            "names": [
              "k8s.gcr.io/kube-apiserver@sha256:d10db42c2353539ce15006854edfb6707ba6025f282d59d962729ed3b6039004",
              "k8s.gcr.io/kube-apiserver:v1.23.0"
            ],
            "sizeBytes": 135154064
          },
          {
            "names": [
              "k8s.gcr.io/kube-controller-manager@sha256:0bfbb13e5e9cec329523b6f654687af8ce058adbc90b42e5af7a929ac22e2a53",
              "k8s.gcr.io/kube-controller-manager:v1.23.0"
            ],
            "sizeBytes": 124963795
          },
          {
            "names": [
              "k8s.gcr.io/kube-proxy@sha256:2e8292d30042bb75f745d2a90d0fc4fbc3a3b1bdbe5b9d3bf50dd866c62b2ba7",
              "k8s.gcr.io/kube-proxy:v1.23.0"
            ],
            "sizeBytes": 112319647
          },
          {
            "names": [
              "k8s.gcr.io/metrics-server/metrics-server@sha256:5ddc6458eb95f5c70bd13fdab90cbd7d6ad1066e5b528ad1dcb28b76c5fb2f00"
            ],
            "sizeBytes": 68793965
          },
          {
            "names": [
              "k8s.gcr.io/kube-scheduler@sha256:af8166ce28baa7cb902a2c0d16da865d5d7c892fe1b41187fd4be78ec6291c23",
              "k8s.gcr.io/kube-scheduler:v1.23.0"
            ],
            "sizeBytes": 53476049
          },
          {
            "names": [
              "k8s.gcr.io/ingress-nginx/kube-webhook-certgen@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660"
            ],
            "sizeBytes": 47736388
          },
          {
            "names": [
              "k8s.gcr.io/coredns/coredns@sha256:5b6ec0d6de9baaf3e92d0f66cd96a25b9edbce8716f5f15dcd1a616b3abd590e",
              "k8s.gcr.io/coredns/coredns:v1.8.6"
            ],
            "sizeBytes": 46829283
          },
          {
            "names": [
              "gcr.io/k8s-minikube/storage-provisioner@sha256:18eb69d1418e854ad5a19e399310e52808a8321e4c441c1dddad8977a0d7a944",
              "gcr.io/k8s-minikube/storage-provisioner:v5"
            ],
            "sizeBytes": 31465472
          },
          {
            "names": [
              "k8s.gcr.io/pause@sha256:3d380ca8864549e74af4b29c10f9cb0956236dfb01c40ca076fb6c37253234db",
              "k8s.gcr.io/pause:3.6"
            ],
            "sizeBytes": 682696
          },
          {
            "names": [
              "hello-world@sha256:d000bc569937abbe195e20322a0bde6b2922d805332fd6d8a68b19f524b7d21d",
              "hello-world:latest"
            ],
            "sizeBytes": 13256
          }
        ]
      }
    }
  ]
728x90
반응형