IAM 조인을 사용한 Kubernetes 클러스터 등록
이 가이드에서는 에이전트의 IAM 자격 증명을 사용하여 Teleport 클러스터에 자동으로 조인하는 방식으로 Kubernetes 클러스터를 Teleport에 등록하는 방법을 보여드립니다. 조인 시크릿을 Kubernetes 클러스터에 배포할 필요 없이 등록하려는 각 클러스터에 Teleport Kubernetes 서비스를 배포하여 여러 Kubernetes 클러스터를 Teleport에 등록할 수 있습니다.
이 가이드에서는 에이전트의 IAM 자격 증명을 사용하여 Teleport 클러스터에 자동으로 조인하는 방식으로 Kubernetes 클러스터를 Teleport에 등록하는 방법을 보여드립니다.
작동 방식#
조인 시크릿을 Kubernetes 클러스터에 배포할 필요 없이 등록하려는 각 클러스터에 Teleport Kubernetes 서비스를 배포하여 여러 Kubernetes 클러스터를 Teleport에 등록할 수 있습니다.
Kubernetes 클러스터가 처음 등록되면 에이전트는 Teleport 자격 증명을 Kubernetes 시크릿에 저장합니다. 에이전트는 이후 재시작 시 이 자격 증명을 사용하여 클러스터에 자동으로 조인합니다.
사전 요구사항#
-
A running Teleport cluster. If you want to get started with Teleport, sign up for a free trial or set up a demo environment.
-
The
tctlandtshclients.Installing `tctl` and `tsh` clients
-
Determine the version of your Teleport cluster. The
tctlandtshclients must be at most one major version behind your Teleport cluster version. Send a GET request to the Proxy Service at/v1/webapi/findand use a JSON query tool to obtain your cluster version. Replace with the web address of your Teleport Proxy Service:$ TELEPORT_DOMAIN= $ TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')" -
Follow the instructions for your platform to install
tctlandtshclients:
-
- Kubernetes 클러스터 버전 >=
v
[kubernetes.major_version].[kubernetes.minor_version].0 - 클러스터에 대한 기존 IAM OpenID Connect (OIDC) 공급자
- Helm >=
[helm.version] - AWS CLI >=
2.10.3또는1.27.81
Verify that Helm and Kubernetes are installed and up to date.
$ helm version
# version.BuildInfo{Version:"v(=helm.version=)"}
$ kubectl version
# Client Version: version.Info{Major:"(=kubernetes.major_version=)", Minor:"(=kubernetes.minor_version=)+"}
# Server Version: version.Info{Major:"(=kubernetes.major_version=)", Minor:"(=kubernetes.minor_version=)+"}
To check that you can connect to your Teleport cluster, sign in with tsh login, then
verify that you can run tctl commands using your current credentials.
For example, run the following command, assigning to the domain name of the Teleport Proxy Service in your cluster and to your Teleport username:
$ tsh login --proxy= --user=
$ tctl status
# Cluster (=teleport.url=)
# Version (=teleport.version=)
# CA pin (=presets.ca_pin=)
If you can connect to the cluster and run the tctl status command, you can use your
current credentials to run subsequent tctl commands from your workstation.
If you host your own Teleport cluster, you can also run tctl commands on the computer that
hosts the Teleport Auth Service for full permissions.
1/3단계. IAM 자격 증명을 가진 Kubernetes 서비스 계정 생성#
Teleport는 AWS에서 실행 중인 에이전트가 실행 중인 IAM 자격 증명을 사용하여 클러스터에 조인할 수 있는 모드를 지원합니다. 이를 통해 Kubernetes 클러스터에 조인 시크릿을 배포하지 않고도 AWS에서 실행 중인 Kubernetes 클러스터를 등록할 수 있습니다.
EKS 노드의 자격 증명에 의존하지 않고 클러스터에 안전하게 조인하려면 Teleport 에이전트가 IAM 역할이 연결된 별도의 Kubernetes 서비스 계정으로 실행되어야 합니다. IAM Roles for Service Accounts(IRSA)가 구성되지 않은 경우 노드에서 실행되는 모든 파드가 노드의 자격 증명에 접근할 수 있으므로 노드의 자격 증명에 의존하는 것은 권장하지 않습니다.
IRSA가 올바르게 작동하려면 Kubernetes 클러스터에 IAM 역할을 Kubernetes 서비스 계정에 매핑하는 IAM OpenID Connect가 필요합니다.
Kubernetes 서비스 계정은 sts:GetCallerIdentity API에 접근할 수 있어야 하지만 다른 권한은 필요하지 않습니다.
IAM 정책을 생성하려면 다음 명령을 실행합니다:
$ cat >iam-policy.json <
그런 다음 IAM 정책을 생성합니다:
$ aws iam create-policy --policy-name --policy-document file://iam-policy.json
{
"Policy": {
"PolicyName": "",
"PolicyId": "ANPAW2Y2Q2Y2Y2Y2Y2Y2Y",
"Arn": "arn:aws:iam::aws:policy/",
"Path": "/",
"DefaultVersionId": "v1",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 0,
"IsAttachable": true,
"Description": "",
"CreateDate": "2021-03-18T15:12:00+00:00",
"UpdateDate": "2021-03-18T15:12:00+00:00"
}
}
이제 Kubernetes 서비스 계정을 생성하고 IAM 역할에 매핑해야 합니다. 이를 위한 두 가지 방법이 있습니다. eksctl을 사용하여 클러스터를 프로비저닝했다면 이를 사용하거나 AWS CLI 방법을 사용할 수 있습니다.
eksctl은 새 IAM 역할을 자동으로 생성하고 대상 네임스페이스의 Kubernetes 서비스 계정에 매핑하는 것을 지원합니다.
$ eksctl create iamserviceaccount \
--name \
--namespace \
--cluster \
--region \
--attach-policy-arn arn:aws:iam::aws:policy/ \
--role-name \
--approve
참조 매개변수:
- 는 Kubernetes 서비스 계정의 이름입니다.
- 는 Teleport Kubernetes 서비스가 실행 중인 네임스페이스입니다.
- 은 클러스터가 실행 중인 AWS 리전입니다.
- 는 이전 단계에서 생성한 IAM 정책의 이름입니다.
- 는 Kubernetes 클러스터의 이름입니다.
- 은 생성할 IAM 역할의 이름입니다.
명령이 완료되면 AWS 계정에 새 IAM 역할이 생성되고 대상 네임스페이스에 새 Kubernetes 서비스 계정이 생성됩니다.
AWS CLI를 사용하여 새 IAM 역할을 생성하고 대상 네임스페이스의 Kubernetes 서비스 계정에 매핑하려면 몇 가지 추가 단계가 필요합니다.
먼저 Kubernetes 클러스터에 대상 네임스페이스와 Kubernetes 서비스 계정을 생성해야 합니다.
$ kubectl create ns
namespace/ created
$ kubectl create sa -n
serviceaccount/ created
그런 다음 IAM 역할과 신뢰 관계를 생성해야 합니다. 이를 위해 AWS 계정 ID와 OIDC 공급자 URL을 얻어야 합니다. 클러스터에 구성된 것이 없다면 다음 가이드를 확인하세요: IAM OpenID Connect (OIDC).
AWS 계정 ID를 추출하려면 다음 명령을 사용할 수 있습니다:
$ account_id=$(aws sts get-caller-identity --query "Account" --output text)
OIDC 공급자 URL은 클러스터 구성에서 추출할 수 있습니다:
$ oidc_provider=$(aws eks describe-cluster --name --region --query "cluster.identity.oidc.issuer" --output text | sed -e "s/^https:\/\///")
$ echo $oidc_provider
oidc.eks.eu-west-1.amazonaws.com/id/[...]
명령의 출력이 비어 있다면 위에서 언급한 대로 OIDC 공급자를 구성해야 합니다.
IAM 역할과 신뢰 관계를 생성하려면 다음 명령을 실행합니다:
$ cat >trust-relationship.json <
IAM 역할을 생성하려면 다음 명령을 실행합니다:
$ aws iam create-role --role-name --assume-role-policy-document file://trust-relationship.json --description "my-role-description"
그런 다음 IAM 역할 어노테이션으로 서비스 계정을 연결합니다:
$ kubectl annotate serviceaccount -n eks.amazonaws.com/role-arn=arn:aws:iam::$account_id:role/
이 시점에서 IAM 역할은 Teleport Kubernetes 서비스의 서비스 계정에서 사용할 준비가 되었습니다.
2/3단계. AWS 조인 토큰 생성#
에이전트가 정의된 역할을 사용하여 AWS 계정에서 Teleport 클러스터에 조인할 수 있는 동적 토큰을 생성합니다.
내부적으로 Kubernetes 서비스 인스턴스는 AWS 조인 토큰에 구성된 허용 규칙과 일치하는 서명된 자격 증명 문서를 보내 AWS 계정에서 실행 중임을 증명합니다.
AWS 계정과 에이전트가 실행할 AWS ARN을 지정하는 allow 규칙이 있는 token.yaml을 생성합니다.
$ cat >token.yaml <
tctl create token.yaml을 실행하여 토큰을 생성합니다.
3/3단계. Teleport Kubernetes 서비스 배포#
Configure Helm to fetch Teleport charts from the Teleport Helm repository:
$ helm repo add teleport (=teleport.helm_repo_url=)
Refresh the local Helm cache by fetching the latest charts:
$ helm repo update
kubectl을 Kubernetes 클러스터로 전환하고 실행합니다:
# Kubernetes 에이전트를 배포합니다. tele.example.com Teleport 클러스터로 다이얼백합니다.
$ CLUSTER=iam-cluster
$ PROXY=tele.example.com:443
# Teleport Kubernetes 에이전트를 설치합니다. 서비스 계정을 생성하지 않고 기존
# 서비스 계정을 사용합니다. serviceAccount.create 및 serviceAccount.name 매개변수를 참조하세요.
$ helm install teleport-agent teleport/teleport-kube-agent \
--set kubeClusterName=${CLUSTER?} \
--set roles="kube\,app\,discovery" \
--set proxyAddr=${PROXY?} \
--set joinParams.method=iam \
--set joinParams.tokenName= \
--set serviceAccount.create=false \
--set serviceAccount.name= \
--create-namespace \
--namespace= \
--version (=teleport.version=)
Teleport 에이전트 파드가 실행 중인지 확인합니다. 단일 준비 컨테이너가 있는 하나의 Teleport 에이전트 파드가 표시되어야 합니다:
$ kubectl -n get pods
NAME READY STATUS RESTARTS AGE
teleport-agent-0 1/1 Running 0 32s
tsh kube ls를 사용하여 연결된 클러스터를 나열하고 tsh kube login을 사용하여 전환합니다:
$ tsh kube ls
# Kube Cluster Name Selected
# ----------------- --------
# iam-cluster
# kubeconfig가 이제 iam-cluster 클러스터를 가리킵니다
$ tsh kube login iam-cluster
# Kubernetes 클러스터 "iam-cluster"에 로그인했습니다. 'kubectl version'으로 연결을 테스트해보세요.
# kubectl 명령은 `iam-cluster`에서 실행되지만 `tele.example.com` 클러스터를 통해 라우팅됩니다.
$ kubectl get pods
에이전트 파드가 정상이고 준비되어 있지만 Kubernetes 클러스터가 보이지 않는다면 역할과 관련된 RBAC 권한 문제일 가능성이 높습니다. 반면 Kubernetes 클러스터가 보이지만 파드가 보이지 않는다면 Teleport 역할이 Kubernetes 클러스터의 파드에 대한 접근을 허용하지 않는 것입니다. 두 경우 모두 아래 섹션을 참조하세요.
Kubernetes 클러스터가 보이지 않나요?
To authenticate to a Kubernetes cluster via Teleport, your Teleport user's roles must allow access as at least one Kubernetes user or group.
-
Retrieve a list of your current user's Teleport roles. The example below requires the
jqutility for parsing JSON:$ CURRENT_ROLES=$(tsh status -f json | jq -r '.active.roles | join ("\n")') -
Retrieve the Kubernetes groups your roles allow you to access:
$ echo "$CURRENT_ROLES" | xargs -I{} tctl get roles/{} --format json | \ jq '.[0].spec.allow.kubernetes_groups[]?' -
Retrieve the Kubernetes users your roles allow you to access:
$ echo "$CURRENT_ROLES" | xargs -I{} tctl get roles/{} --format json | \ jq '.[0].spec.allow.kubernetes_users[]?' -
If the output of one of the previous two commands is non-empty, your user can access at least one Kubernetes user or group, so you can proceed to the next step.
-
If both lists are empty, create a Teleport role for the purpose of this guide that can view Kubernetes resources in your cluster.
Create a file called
kube-access.yamlwith the following content:kind: role metadata: name: kube-access version: v7 spec: allow: kubernetes_labels: '*': '*' kubernetes_resources: - kind: '*' namespace: '*' name: '*' verbs: ['*'] kubernetes_groups: - viewers deny: {} -
Apply your changes:
$ tctl create -f kube-access.yaml(!docs/pages/includes/create-role-using-web.mdx!)
-
(!docs/pages/includes/add-role-to-user.mdx role="kube-access"!)
-
Configure the
viewersgroup in your Kubernetes cluster to have the built-inviewClusterRole. When your Teleport user assumes thekube-accessrole and sends requests to the Kubernetes API server, the Teleport Kubernetes Service impersonates theviewersgroup and proxies the requests.Create a file called
viewers-bind.yamlwith the following contents, binding the built-inviewClusterRole with theviewersgroup you enabled your Teleport user to access:apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: viewers-crb subjects: - kind: Group # Bind the group "viewers", corresponding to the kubernetes_groups we assigned our "kube-access" role above name: viewers apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole # "view" is a default ClusterRole that grants read-only access to resources # See: https://kubernetes.io/docs/reference/access-authn-authz/rbac/#user-facing-roles name: view apiGroup: rbac.authorization.k8s.io -
Apply the
ClusterRoleBindingwithkubectl:$ kubectl apply -f viewers-bind.yaml
다음 단계#
teleport-kube-agent Helm 차트의 값 파일에서 설정할 수 있는 모든 옵션을 보려면 참조 가이드를 참조하세요.
