HOWTO

Kubernetes API 사용 테스트

한크크 2022. 3. 25. 16:03

요즘 kubernetes cluster 는 대부분 CSP에서 제공하는 managed kubernetes 를 사용하기 때문에, cluster 를 생성하고 워커노드를 추가/변경하기 위한 API 는 CSP 에서 제공하지만, pod 나 deployment 를 생성/관리하기 위해서는 kubernetes 자체에서 제공하는 api 를 사용해야 한다. 

 

 

 

 

api 테스트를 하기 위해 먼저 jane 이라는 service account 가 nsistio 의 namespace 에 있는 pod 만 조회할 수 있는 role 을 생성하고 binding 해준다. 

 

1. service account 생성 

kubectl create sa jane -n nsistio

2. role 생성

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: custom-role
  namespace: nsistio
rules:
  - apiGroups: [""] # "" indicates the core API group
    resources: ["pods"]
    verbs: ["get", "watch", "list"]
kubectl create -f role.yaml

 

2. role binding 

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: custom-rolebinding
  namespace: nsistio
subjects:
  - kind: ServiceAccount
    name: jane
    namespace: nsistio
roleRef:
  kind: Role
  name: custom-role
  apiGroup: rbac.authorization.k8s.io
kubectl create -f rolebinding.yaml

 

3. pod list 조회 권한이 있는지 확인 - yes 라고 나와야 함 

kubectl auth can-i list pods --as=system:serviceaccount:nsistio:jane -n nsistio
yes

 

4. api 테스트 준비

 1) api 서버 확인 

kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'

2) service account jane의 token 확인

NAMESPACE=nsistio
SA=jane
TOKEN=$(kubectl -n $NAMESPACE describe secret $(kubectl get -n $NAMESPACE secrets | grep $SA | cut -f1 -d ' ') | grep -E '^token' | cut -f2 -d':' | tr -d ' ')

 

5. pod list api 실행

curl -X GET $APISERVER/api/v1/namespaces/$NAMESPACE/pods/ -H "Authorization: Bearer $TOKEN" --insecure | more

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/namespaces/nsistio/pods/",
    "resourceVersion": "138741515"
  },
  "items": [
    {
      "metadata": {
        "name": "details-v1-66b6955995-5cqzs",
        "generateName": "details-v1-66b6955995-",
        "namespace": "nsistio",
        "selfLink": "/api/v1/namespaces/nsistio/pods/details-v1-66b6955995-5cqzs",
        "uid": "feee7176-6290-4017-8462-5cc4924c92a7",
        "resourceVersion": "30226063",
        "creationTimestamp": "2021-09-01T12:52:55Z",
        "labels": {
          "app": "details",
          "istio.io/rev": "default",
          "pod-template-hash": "66b6955995",
          "security.istio.io/tlsMode": "istio",
          "service.istio.io/canonical-name": "details",
          "service.istio.io/canonical-revision": "v1",
          "version": "v1"
        },
        "annotations": {
          "kubectl.kubernetes.io/default-container": "details",
          "kubectl.kubernetes.io/default-logs-container": "details",
          "prometheus.io/path": "/stats/prometheus",
          "prometheus.io/port": "15020",
          "prometheus.io/scrape": "true",
          "sidecar.istio.io/status": "{\"initContainers\":[\"istio-init\"],\"containers\":[\"istio-proxy\"],\"volumes\":[\"istio-envoy\",\"istio-data\",\"istio-podinfo\",\"istiod-ca-cert\"],\"imagePullSecrets\":null}"
        },
        "ownerReferences": [
          {
            "apiVersion": "apps/v1",
            "kind": "ReplicaSet",
            "name": "details-v1-66b6955995",
            "uid": "70ab0e5b-0f1d-45f9-a29c-f981331f1d7c",
            "controller": true,
            "blockOwnerDeletion": true
          }

* kubernetes api reference 

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#podstatus-v1-core

반응형