Kubernetes의 동일한 네임 스페이스에서 실행중인 프로세스에서 ClusterRoleBinding 및 네임 스페이스 삭제

와하 브 알리

Namespacekube 클러스터에서을 생성하고 해당 네임 스페이스 내에 내 앱을 설치합니다. 앱은 서버에 정기적 인 하트 비트 요청을 보내고 서버에서 "자신을 제거" 응답을 받으면 전체 kube 네임 스페이스에서 delete를 호출하여 자체를 삭제합니다. 또한을 ClusterRoleBinding만들고 ServiceAccount주제를 만들고 ClusterRoleBinding이것으로 포드를 실행하여 앱 클러스터에 광범위한 액세스 권한을 부여합니다 ServiceAccount.

문제는 ClusterRoleBinding앱의 자체 제거 프로세스의 일부로 제거하고 싶다는 것입니다 (가능한 경우). ClusterRoleBinding이전에 제거하면 앱에서 삭제를 실행할 수 Namespace없으므로 닭고기와 계란 문제처럼 보입니다. 이를 수행하는 방법이 있습니까?

이것은 내가 이미 아무 소용이 없도록 시도한 것입니다.

  • 앱 컨테이너에 PreStop 핸들러를 추가했습니다. 이제 앱이 전체 네임 스페이스에서 delete를 호출하면 kube는 컨테이너를 종료하기 전에이 핸들러를 호출합니다. 에이 PreStop 핸들러에서 나는 삭제 호출하기 전에보다 5 초 동안 잠 경우 ClusterRoleBinding내가 얻을 "무단" 는 Kubernetes에서 응답 다시.

  • 이로 인해 앱이 PreStop 핸들러에서 를 삭제할 기회가 있기 전에에 ServiceAccount링크 된 것이 ClusterRoleBinding삭제 될 수 있다고 생각 하게됩니다 ClusterRoleBinding. 따라서이를 테스트하기 위해 네임 스페이스에서 삭제를 실행하기 전에에 종료자를 추가 한 ServiceAccount다음 PreStop 핸들러에서 5 초 동안 기다린 다음 삭제를 실행합니다 ClusterRoleBinding (다시 "승인되지 않음"오류가 발생 함). 그런 다음 ServiceAccount이름으로 개체를 가져옵니다. (get "Unauthorized"error back) , ServiceAccount (get error "error ="finalizer does n't exist for object '' ") 빈 객체에서 종료자를 제거 할 수 없기 때문에 종료자를 제거합니다. kubectl을 사용할 때 I (가) 찾을 ServiceAccount존재하지만에 "종단"

kube ServiceAccount"Terminating"아직 하드 삭제되지 않았는데도 상태 일 때 액세스를 취소합니까 ?

삭제해야하는 에서 실행중인 동일한 프로세스에서 ClusterRoleBinding을 제거하는 방법이 있습니까? ( 우리가 제거하려는 앱 이 처음 에 삭제할 수있는 권한을 부여 한다는 점을 감안하면 )NamespaceNamespaceClusterRoleBindingNamespace

어떤 도움이라도 대단히 감사하겠습니다!

ClusterRoleBindingServiceAccount대한 YAML 정의 는 다음과 같습니다.

### ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
    creationTimestamp: null
    name: xyz-myapp-cluster-admin
roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: cluster-admin
subjects:
- kind: ServiceAccount
  name: xyz
  namespace: xyz-myapp

### ServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:
    creationTimestamp: null
    name: xyz
    namespace: xyz-myapp

관련 애플리케이션 로그 :

time="2020-02-18T16:08:33Z" level=info msg="App instructed to remove itself"
time="2020-02-18T16:08:33Z" level=info msg="Created finalizer 'xyz.myapp.com/my-finalizer' on ServiceAccount"
time="2020-02-18T16:08:33Z" level=info msg="Called delete on Namespace"
time="2020-02-18T16:08:38Z" level=info msg="PreStop handler called"
time="2020-02-18T16:08:38Z" level=info msg="----- sleeping for 5 sec -----"
time="2020-02-18T16:08:43Z" level=info msg="Deleting ClusterRoleBinding"
time="2020-02-18T16:08:43Z" level=warning msg="Failed to delete ClusterRoleBinding" error="Unexpected error removing ClusterRolebinding: Unauthorized"
time="2020-02-18T16:08:43Z" level=warning msg="Failed to get ServiceAccount" error=Unauthorized
time="2020-02-18T16:08:43Z" level=warning msg="Failed to remove finalizer from ServiceAccount" error="finalizer 'xyz.myapp.com/my-finalizer' doesn't exist for object ''"
와하 브 알리

kubernetes 문서를 살펴본 후 가장 신뢰할 수있는 방법은 다음과 같습니다.

  1. 앱이 서버에서 "자신 제거" 응답을 받으면 앱 ClusterRoleBinding의 소유자가 Namespace실행됩니다.
  2. 이것은 패치를 사용하여 ClusterRoleBinding네임 스페이스 아래 에 추가하여 수행 할 수 있습니다 medatadata.owerReferences.
  3. ClusterRoleBinding의 소유자로가 성공적으로 추가 되면 Namespace앱은을 (를) ClusterRoleBinding사용하여 삭제를 호출 할 수 있습니다 DeletePropagationBackground.

다음은 추가 패치 방법의 예입니다 ClusterRoleBindingownerReferences(A)의이 Namespace(Golang에) 적용 할 수는.

import (
    "encoding/json"
    "k8s.io/client-go/kubernetes"
    v1 "k8s.io/api/core/v1"
    rbacv1 "k8s.io/api/rbac/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ownerReferencePatch struct {
    Op    string                  `json:"op"`
    Path  string                  `json:"path"`
    Value []metav1.OwnerReference `json:"value"`
}

func AddClusterRoleBindingOwnerReferenceToNamespace(client kubernetes.Interface, crb *rbacv1.ClusterRoleBinding, ns *v1.Namespace) (*v1.Namespace, error) {
    patch, err := json.Marshal([]ownerReferencePatch{
        {
            Op:   "add",
            Path: "/metadata/ownerReferences",
            Value: []metav1.OwnerReference{
                {
                    APIVersion:         crb.RoleRef.APIGroup,
                    BlockOwnerDeletion: func(in bool) *bool {return &in}(true),
                    Kind:               "ClusterRoleBinding",
                    Name:               crb.GetName(),
                    UID:                crb.GetUID(),
                },
            },
        },
    })
    if err != nil {
        return nil, err
    }

    return client.CoreV1().Namespaces().Patch(ns.GetName(), types.JSONPatchType, patch)
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관