본문 바로가기
네트워크/k8s

[k8s] 30. Authentication - 실습

by Lauren X Ming 2021. 2. 24.

Authentication - 실습

image

1. X509 Client Certs

image

  • 인증서를 갖고 https와 http에 대한 API를 날려봄
  • 클러스터에 있는 kubeconfig 확인하고, Clienty key와 Client crt를 가져와서 https를 날려볼 것
  • kubectl로 프록시를 띄우고 http로 API를 날려볼 것

Postman 설치 및 설정

  • 검색해서 알아서 다운 받으시고..
  • Settings > General > SSL certificate verification > OFF 설정
    image

실습 과정

  1. 마스터 노드에서 다음 경로 파일 확인image
    클러스터에 CA 인증서와 클러스터 정보, user에 user 정보, client key로 base64로 되어 있음
  2. cat /etc/kubernetes/admin.conf
  3. 마스터 노드에서 다음 명령어를 통해 클라이언트 인증서, 키 가져오기
    grep 'client-key-data' /etc/kubernetes/admin.conf | head -n 1 | awk '{print $2}' | base64 -d
    내 PC에 client.crt, client.key를 만들고 위 명령어를 통해 나온 값을 복사하기image
    image
  4. grep 'client-certificate-data' /etc/kubernetes/admin.conf | head -n 1 | awk '{print $2}' | base64 -d
  5. Postman에서 client.crt, client.key 등록하고 API 날려보기
    File -> setting -> certificate에 client.crt, client.key 등록하고image
    https://192.168.35.30:6443/api/v1/nodes API 날려보기!!image
    e2
    4. 마스터 노드에 다음 명령어로 kubeadm/kubectl/kubelet 설치--> 설치하면서 이미 함5. 마스터 노드에서 다음 명령어로 admin.conf 인증서 복사--> 설치하면서 이미 함
  6. mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
  7. yum install -y --disableexcludes=kubernetes kubeadm-1.15.5-0.x86_64 kubectl-1.15.5-0.x86_64 kubelet-1.15.5-0.x86_64
  8. 마스터 노드에서 다음 명령어로 kubectl proxy 띄우기포트는 8001, 접속 아이피는 마스터 노드 아이피
    accept-host='^*$' : 어느 호스트건 접속 가능, 내용 바꾸면 특정 호스트만 접속 가능하게 함
  9. nohup kubectl proxy --port=8001 --address=192.168.35.30 --accept-hosts='^*$' >/dev/null 2>&1 &
  10. postman에서 이번엔 무려 http로 http://192.168.35.30:8001/api/v1/nodes API 날려보기image
    9980B4405BC1AA051A

2. Kubectl

image

  • 두 클러스터에 있는 kubeconfig 파일을 복사해서 하나로 합친 다음에
  • kubectl config 명령어로 클러스터를 선택하고, 선택한 클러스터에서 자원이 조회되는지 확인
  • /etc/kubernetes/admin.conf에 있는 파일을 합칠 예정

Kubeconfig - 2개용

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1KVEUtLS0tLQo=
    server: https://192.168.0.30:6443
  name: cluster-a
- cluster:
    certificate-authority-data: LS0tLS1KVEUtLS0tLQo=
    server: https://192.168.0.50:6443
  name: cluster-b
contexts:
- context:
    cluster: cluster-a
    user: admin-a
  name: context-a
- context:
    cluster: cluster-b
    user: admin-b
  name: context-b
current-context: context-a
kind: Config
preferences: {}
users:
- name: admin-a
  user:
    client-certificate-data: LS0tLS1KVEUtLS0tLQo=
    client-key-data: LS0tLS1KVEUtLS0tLQo=
- name: admin-b
  user:
    client-certificate-data: LS0tLS1KVEUtLS0tLQo=
    client-key-data: LS0tLS1KVEUtLS0tLQo=
  • 이런식으로 2개의 클러스터를 하나로 합쳐서 관리
  • 그런데 난 클러스터가 1개잖아?
  • 나는 안 될거야 아마...
  • 난 그냥 1개로만 실습하겠음

Kuubeconfig - 흙수저용

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeE1ERXpNVEEzTVRreU1sb1hEVE14TURFeU9UQTNNVGt5TWxvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRU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ElGSUNBVEUtLS0tLQo=
    server: https://192.168.35.30:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJVkNFcFI5ZFhUUW93RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TVRBeE16RXdOekU1TWpKYUZ3MHlNakF4TXpFd056RTVNak5hTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQTBaMDl4SzB0cWRGNEk1NjMKVmRTeVVYb0lLcGZQRnJXUHNnY3NUaG81UHFPWW9VYVVjd3hzUCtYbHJjakxhMnh3M3p1TkpPcU5aV3BMLzlhegptdGMyZjg1TU5HMDJuVmNWQTVrUklxVk55ZDQyRUorWHZnTVpEMk5uVksxMm9YSy81SkRSZ2dJWEs4Z2ZLak9XCnd2UUgzTWNFYkE1QUd0NmdQK1FUdTVuUDRnc3NBVVZzVlNGb203OERzU2VNL2hlUGRnRmRVcXNDMEhHM09mWUIKMlZ4MzJ1dEtRc1R5TnVhVGxDQ3VYajFSMm41WUVibzVlb2tuT1VQNWIzRTRZWmlldzJqNjhIc0RtNEJhSWExUApXOVFIa3g0b0Y3Y1F0Y28weERYVE5vbXA2Mis4eGl2ZGVlN3l1SWszZm4zWGE5bWF2NXVNVVM1L01lbytsVDlUCmhwSXBod0lEQVFBQm95Y3dKVEFPQmdOVkhROEJBZjhFQkFNQ0JhQXdFd1lEVlIwbEJBd3dDZ1lJS3dZQkJRV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yYXRIaTBwWjdXQVJHalVDV2h2TllQVkU2RFRiMEQyckdiNApQZVpxdWpwd1A4TlFYdEZDWnlBMGhOaHY0RFhLSzRvNHVpN1FaaEJDS2hFRzRVMnRCN2U1dkpLOEhOWGFadHZRCmZTa2t6WnpFYnp4MTdHemhla3dVOWU4Q2dZQllrSzFJQStqZ3FVL1drMXVuZUJEejBobWhUNnEwZ3MxeDU4aG0KL1BLV2RxM0Rlc3VFSjVkdjRHVzB0b09xeURaNGIvZEZKQVZGaGkrdnVzR1lTeEtyQUFlWm43VHVhUVREbnRUMApJVXp0ZkswUmVlbDJzek0yU1gwVzBBTTJJM1BZcTFQV3VZME82VHV3QmdmR3JGVkw0NDZWcFZDVjA5c09PSkdWCmlzRFVjUUtCZ1FDeUtwV0E1cXJoWjJUOGNmRTBWUlBTOVdqNWlOVTg5NmxqV1piRDlIZ25TMUFMa2dYNFJ5dEkKOFl2Sy92NmJGUUxPNDR6RGMvb2JWbi9DK3dpUWgyQXBGOWtjMll6SVR0TGR6a3RueUdYNk0wbnBPR3pFaG15cQpuZnhlNk9ERVY3NStJWEVEdGR4cE1rcHlpY0haR0EwbjAxUWxhV2YzQ1V1ZGVsdUd0RWMrbnc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
  • context name: kubernetes-admin@kubernetes

실습 과정(흙수저 ver)

  1. /etc/kubernetes/admin.conf 내용 복사해서 PC 환경 config파일에 복사하기
    사용자 폴더에서 .kube에 넣어둠
  2. cmd를 통한 windows 버전 kubectl CLI 다운로드
    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/windows/amd64/kubectl.exe
  3. kubectl config 실행
    kubectl config use-context kubernetes-admin@kubernetes
    image

3. Service Account

image

  • 네임스페이스를 만듦
  • 네임스페이스가 만들어지면 자동으로 생성되는 ServiceAccount와 Secret 확인
  • 파드 생성하여 토큰값을 가져 API로 접근할 예정

실습 과정

  1. 마스터 노드에서 다음 명령어 실행으로 네임스페이스 생성
    kubectl create ns nm-01
  2. 네임스페이스에 있는 ServiceAccount 조회
    kubectl describe -n nm-01 serviceaccounts
    image
  3. Secret 조회
    kubectl describe -n nm-01 secrets
    image
    이 토큰값 복사
  4. Pod 생성
    cat <<EOF | kubectl create -f -
    apiVersion: v1
    kind: Pod
    metadata:
    name: pod-1
    namespace: nm-01
    labels:
      app: pod
    spec:
    containers:
    - name: container
     image: kubetm/app
    EOF
  5. Postman에서 기존 인증서 등록한 것 삭제
  6. Postman 도메인에 다음 주소 입력
    https://192.168.35.30:6443/api/v1
  7. Postman Headerer에 Key로 Authorization, Value에는 Bearer 한 칸 띄고 복사한 토큰 값 입력
    image
  8. Send 누르고 결과 보기
    image
  9. 파드 리스트 호출
    image
    https://192.168.35.30:6443/api/v1/namespaces/nm-01/pods/
    Default Service Account에는 파드 리스트를 조회할 수 없다는 에러
    Default Service Account에 파드를 조회할 수 있는 권한이 없어서 생기는 에러

출처

인프런 - 대세는 쿠버네티스