2024.11.19 - [devops/minikube] - 4. Connect to Minikube Cluster via Teleport(2)
4. Connect to Minikube Cluster via Teleport(2)
2024.11.19 - [devops/minikube] - 3. Connect to Minikube Cluster via Teleport(1) 3. Connect to Minikube Cluster via Teleport(1)Teleport란 무엇인가?텔레포트는 원래 인프라 접근을 위해 인증서 기반으로 사용자를 관리하는 솔
ggorockee.tistory.com
Teleport로 로그인을 할 수 있으니 이제 kubernetes와 연결을 해보도록하자. 이부분은 텔레포트이기보다는 kubernetes의 Clusterrole과 Clusterrolebinding이다.
$ kubectl apply -f - <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: devops
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: devops
subjects:
- kind: Group
name: devops
apiGroup: rbac.authorization.k8s.io
- kind: User
name: devops
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: devops
apiGroup: rbac.authorization.k8s.io
EOF
여기서 주의깊게 봐야할 부분은 ClusterRoleBinding에서 Group과 User부분이다. 여기에 써있는 값인 devops가 향후에 텔레포트에서 사용할 kubernetes_user와 kubernetes_group이다. 지금은 이 값이 devops라는 것만 기억하면 된다.
clusterrole과 clusterrolebinding이 만들어졌으면 이 내용을 텔레포트에 추가하자.
$ sudo tctl create -f - <<EOF
kind: role
metadata:
name: kube-access
version: v7
spec:
allow:
kubernetes_labels:
'*': '*'
kubernetes_resources:
- kind: '*'
namespace: '*'
name: '*'
verbs: ['*']
kubernetes_groups:
- devops
kubernetes_users:
- devops
deny: {}
EOF
tctl이라는 명령어를 이용해서 kube-access라는 텔레포틀 role를 만들겠다. 이 role은 kubernetes의 role과는 다르니 기억해두자.
teleport webui에서 로그아웃 후 재 로그인하면 User에서 방금 만든 Role을 추가할 수 있다.
새로운 리소스를 추가해보면 아래와 같은 명령어가 나오는데 그대로 실행하자. 나머지는 UI에서 시키는대로 하면된다.
인증이 완료되었다.
로컬에서 아래 명령어를 통해 접속해보자. 물론 proxy는 본인이 설치한 domain이다.
$ tsh login --proxy=teleport.dev.abc.com:443 --auth=local --user=ggorockee teleport.dev.abc.com
$ tsh kube login camping
$ kubectl get pods
ERROR: Cannot connect Kubernetes clients to Teleport Proxy directly. Please use `tsh proxy kube` or `tsh kubectl` instead.
E1115 09:54:43.310325 70075 memcache.go:265] couldn't get current server API group list: Get "https://teleport.dev.xxxx.com:443/api?timeout=32s": getting credentials: exec: executable /usr/local/bin/tsh failed with exit code 1
ERROR: Cannot connect Kubernetes clients to Teleport Proxy directly. Please use `tsh proxy kube` or `tsh kubectl` instead.
E1115 09:54:43.389209 70075 memcache.go:265] couldn't get current server API group list: Get "https://teleport.dev.xxxx.com:443/api?timeout=32s": getting credentials: exec: executable /usr/local/bin/tsh failed with exit code 1
ERROR: Cannot connect Kubernetes clients to Teleport Proxy directly. Please use `tsh proxy kube` or `tsh kubectl` instead.
E1115 09:54:43.453795 70075 memcache.go:265] couldn't get current server API group list: Get "https://teleport.dev.xxxx.com:443/api?timeout=32s": getting credentials: exec: executable /usr/local/bin/tsh failed with exit code 1
ERROR: Cannot connect Kubernetes clients to Teleport Proxy directly. Please use `tsh proxy kube` or `tsh kubectl` instead.
E1115 09:54:43.500500 70075 memcache.go:265] couldn't get current server API group list: Get "https://teleport.dev.xxxx.com:443/api?timeout=32s": getting credentials: exec: executable /usr/local/bin/tsh failed with exit code 1
ERROR: Cannot connect Kubernetes clients to Teleport Proxy directly. Please use `tsh proxy kube` or `tsh kubectl` instead.
E1115 09:54:43.549284 70075 memcache.go:265] couldn't get current server API group list: Get "https://teleport.dev.xxxx.com:443/api?timeout=32s": getting credentials: exec: executable /usr/local/bin/tsh failed with exit code 1
Unable to connect to the server: getting credentials: exec: executable /usr/local/bin/tsh failed with exit code 1
아직 뭔가 작동하지 않은 거 같다. 이게 proxy가 백그라운드에서 돌아가지 않아서 나오는 에러라고 말해준다.
다음 명령어로 proxy를 켜주자.
$ tsh proxy kube -p 8443
위 명령어를 실행하면 가이드가 나오는데 그 가이드를 참고하여 아래 명령어를 입력하면 된다. & 을 이용하여 백그라운드로 실행하자.
$ tsh proxy kube -p 8443 &
$ export KUBECONFIG=$HOME/.tsh/keys/teleport.dev.abc.com/ggorockee-kube/teleport.dev.abc.com/localproxy-8443-kubeconfig
$ kubectl get pod -A
아름답다!! 이제 로컬에서 접속이 된다.
지금 proxy를 백그라운드로 실행했기 때문에 이 포트번포 8443이 계속 살아 있는데 . 다음번에도 사용하기 위해 종료할 때마다 아래 명령어를 사용해야한다.
ps -ef | grep 8443
# 503 71302 91559 0 9:56AM ttys004 0:00.67 tsh proxy kube -p 8443
kill -9 71302
# [1] + 71302 killed tsh proxy kube -p 8443
뭔가 좀 맘에 안든다. 번거롭기 때문이다. 피날레로 shell scipt로 마무리 하면될거같다.
function tsl(){
if [[ "camping" == $1 ]]; then
tslkill()
echo "camping teleport login"
tsh login --proxy=teleport.dev.xxxx.com:443 --auth=local --user=ggorockee
echo "ggorockee login"
eval $(tsh env --unset)
tsh kube login camping
echo "ggorockee proxy"
tsh proxy kube -p 8443 &
echo "export KUBECONFIG"
export KUBECONFIG=/Users/whoopi/.tsh/keys/teleport.dev.xxxx.com/ggorockee-kube/teleport.dev.xxxx.com/localproxy-8443-kubeconfig
else
echo "argument is camping"
fi
}
function tslkill(){
kill -9 $(ps -ef | grep 8443 | awk 'NR==1 {print $2}')
}
이 쉘스크립트를 ~/.bashrc나 ~/.zshrc등에 추가하자. 그러면 이제
$ tsl camping
$ tslkill
이제 잘 된다. 원격으로 괜히 했나 싶긴할정도로 멀리 돌아왔다. 그러나 이런 경험이 하나하나 쌓여서 개발자나 엔지니어가 스텝업을 조금씩한다고 생각한다. 다음번엔 텔레포트를 이용해서 개발하도록 하겠다.
'devops > minikube' 카테고리의 다른 글
7. [Ubuntu] 서비스 등록 (systemd) (0) | 2024.11.19 |
---|---|
6. Istio: 서비스 메쉬의 선두주자 (25) | 2024.11.19 |
4. Connect to Minikube Cluster via Teleport(2) (24) | 2024.11.19 |
3. Connect to Minikube Cluster via Teleport(1) (24) | 2024.11.19 |
2. Minikube on EC2 (25) | 2024.11.19 |