정적 kubeconfig로 Kubernetes 클러스터 등록
해당 클러스터에서 Teleport Kubernetes 서비스를 실행하여 Kubernetes 클러스터를 Teleport에 등록할 수 있지만, 클러스터 외부의 Linux 호스트에서 Teleport Kubernetes 서비스를 실행할 수도 있습니다.
해당 클러스터에서 Teleport Kubernetes 서비스를 실행하여 Kubernetes 클러스터를 Teleport에 등록할 수 있지만, 클러스터 외부의 Linux 호스트에서 Teleport Kubernetes 서비스를 실행할 수도 있습니다. 이는 Teleport 배포를 접근을 관리하려는 Kubernetes 클러스터와 분리하려는 경우에 유용합니다.
작동 방식#
이 설정에서 Teleport Kubernetes 서비스는 kubeconfig 파일을 사용하여 선택한 Kubernetes 클러스터의 API 서버에 인증합니다.
사전 요구사항#
-
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 클러스터.
- Teleport Kubernetes 서비스를 실행하기 위한 자체 인프라에 배포된 Linux 호스트. Kubernetes 클러스터 외부에서 실행할 수 있습니다.
- 워크스테이션에 설치된
kubectl커맨드 라인 도구.
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.
Best practices for production security
When running Teleport in production, you should adhere to the following best practices to avoid security incidents:
- Avoid using
sudoin production environments unless it's necessary. - Create new, non-root, users and use test instances for experimenting with Teleport.
- Run Teleport's services as a non-root user unless required. Only the SSH
Service requires root access. Note that you will need root permissions (or
the
CAP_NET_BIND_SERVICEcapability) to make Teleport listen on a port numbered <1024(e.g.443). - Follow the principle of least privilege. Don't give users
permissive roles when more a restrictive role will do.
For example, don't assign users the built-in
access,editorroles, which give them permissions to access and edit all cluster resources. Instead, define roles with the minimum required permissions for each user and configure Access Requests to provide temporary elevated permissions. - When you enroll Teleport resources—for example, new databases or applications—you
should save the invitation token to a file.
If you enter the token directly on the command line, a malicious user could view
it by running the
historycommand on a compromised system.
You should note that these practices aren't necessarily reflected in the examples used in documentation. Examples in the documentation are primarily intended for demonstration and for development environments.
1/4단계. kubeconfig 파일 생성#
Teleport Kubernetes 서비스는 kubeconfig 파일을 사용하여 Kubernetes 클러스터에 인증합니다. 이 섹션에서는 이 가이드의 뒷부분에서 사용할 Teleport Kubernetes 서비스를 구성하기 위한 kubeconfig 파일을 생성합니다.
컨텍스트가 올바른지 확인#
먼저 로컬 kubectl 명령이 등록하려는 Kubernetes 클러스터를 가리키도록 구성합니다. 다음 명령으로 올바른 클러스터가 선택되었는지 확인할 수 있습니다:
$ kubectl config get-contexts
이 명령을 사용하여 에 할당된 클러스터로 전환합니다:
# 예: my-context
$ kubectl config use-context
스크립트 실행#
워크스테이션에서 kubeconfig 파일을 생성하는 데 사용할 Teleport의 get-kubeconfig.sh 스크립트를 다운로드합니다:
$ curl -OL \
https://raw.githubusercontent.com/gravitational/teleport/v(=teleport.version=)/examples/k8s-auth/get-kubeconfig.sh
get-kubeconfig.sh는 Kubernetes 파드를 가져오고 사용자, 그룹 및 기타 서비스 계정을 가장(impersonate)할 수 있는 Teleport Kubernetes 서비스용 서비스 계정을 생성합니다. Teleport Kubernetes 서비스는 이 서비스 계정을 사용하여 Kubernetes 클러스터의 리소스에 대한 접근을 관리합니다. 또한 스크립트는 서비스 계정 자격 증명을 저장하기 위해 클러스터에 Kubernetes Secret이 있는지 확인합니다.
get-kubeconfig.sh는 배포하는 리소스를 위해 teleport라는 네임스페이스를 생성하지만, 스크립트를 실행하는 셸에서 TELEPORT_NAMESPACE 환경 변수를 할당하여 다른 이름을 선택할 수 있습니다.
리소스를 생성한 후 get-kubeconfig.sh는 스크립트를 실행한 디렉터리에 kubeconfig라는 새 kubeconfig 파일을 작성합니다.
get-kubeconfig.sh 스크립트를 실행합니다:
$ bash get-kubeconfig.sh
스크립트가 성공적이면 이 메시지가 표시됩니다:
Done!
kubeconfig 파일을 Teleport Kubernetes 서비스를 실행하는 호스트로 이동합니다. kubeconfig 파일이 /var/lib/teleport/kubeconfig에 있다고 가정합니다.
여러 Kubernetes 클러스터를 연결하나요?
여러 항목이 포함된 경우 하나의 kubeconfig 파일에서 여러 Kubernetes 클러스터를 Teleport에 연결할 수 있습니다. get-kubeconfig.sh로 생성된 여러 kubeconfig 파일을 결합하려면 merge-kubeconfigs.sh를 사용합니다.
2/4단계. Teleport Kubernetes 서비스 설정#
이 단계에서는 Teleport Kubernetes 서비스를 설치하고 앞에서 생성한 kubeconfig 파일을 사용하여 Kubernetes 클러스터에 접근하도록 구성합니다.
조인 토큰 얻기#
조인 토큰을 생성하여 Teleport 클러스터와 새 Kubernetes 서비스 인스턴스 간의 신뢰를 설정합니다:
$ tctl tokens add --type=kube --format=text --ttl=1h
(=presets.tokens.first=)
에 조인 토큰을 할당합니다. Teleport Kubernetes 서비스를 실행하는 호스트에서 토큰만으로 구성된 /tmp/token이라는 파일을 생성합니다:
$ echo | sudo tee /tmp/token
Teleport Kubernetes 서비스 설치#
Teleport Kubernetes 서비스를 설치할 호스트에서 다음 명령을 실행합니다:
To install a Teleport Agent on your Linux server:
The recommended installation method is the cluster install script. It will select the correct version, edition, and installation mode for your cluster.
-
Assign to your Teleport cluster hostname and port, but not the scheme (https://).
-
Run your cluster's install script:
$ curl "https:///scripts/install.sh" | sudo bash
Teleport Kubernetes 서비스 구성#
에 Teleport Proxy 서비스 또는 Teleport Cloud 테넌트의 호스트와 포트를 할당합니다(예: mytenant.teleport.sh:443). Teleport Kubernetes 서비스를 실행할 호스트에서 /etc/teleport.yaml 파일에 다음 내용을 생성합니다:
version: v3
teleport:
join_params:
token_name: "/tmp/token"
method: token
proxy_server:
You can check the status of the Teleport Kubernetes Service with systemctl status teleport
and view its logs with journalctl -fu teleport.
3/4단계. Teleport 사용자에게 접근 권한 부여#
이 가이드의 뒷부분에서 클러스터에 연결할 수 있도록 Teleport 사용자가 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 jq utility 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.yaml with 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 viewers group in your Kubernetes cluster to have the built-in
view ClusterRole. When your Teleport user assumes the kube-access role
and sends requests to the Kubernetes API server, the Teleport Kubernetes
Service impersonates the viewers group and proxies the requests.
Create a file called viewers-bind.yaml with the following contents, binding
the built-in view ClusterRole with the viewers group 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 ClusterRoleBinding with kubectl:
$ kubectl apply -f viewers-bind.yaml
4/4단계. Kubernetes 클러스터 접근#
위의 구성으로 Teleport가 시작되면 모든 새 클러스터를 볼 수 있어야 합니다:
$ tsh kube ls
Kube Cluster Name Labels Selected
--------------------------------------- ------ --------
my-cluster region=us-east-1
클러스터에 접근하려면 다음 명령을 실행합니다. my-cluster를 접근하려는 클러스터 이름으로 바꿉니다:
$ tsh kube login my-cluster
Logged into kubernetes cluster "my-cluster". Try 'kubectl version' to test the connection.
