LoginSignup
0
1

More than 3 years have passed since last update.

EKSクラスタのkubeconfigを設定してローカルからkubectlする

Posted at

これは何

EKSクラスタをローカルなどからkubectlするためにkubeconfigを設定します。

環境など

  • ローカル側

    • OS: Windows10
    • コマンドライン: PowerShell
    • awscli:
      • バージョン1
      • aws-cli/1.16.297 Python/2.7.16 Windows/10 botocore/1.13.33
    • kubectl: 1.14
  • EKS側

    • EKS: 1.14

前提

  • EKSクラスタが既に作成済

設定する

AWS CLIを使えるようにする

  • 今回はWindowsだったので、msiでインストールするのみ
  • 各OSごとの手順は公式を参考にする
  • aws eksコマンドを使用するため、バージョンがそれなりに新しい必要あり
    • これを見るに、恐らく1.16くらいは必要

aws configureで認証情報を設定する

  • 対象のEKSクラスタと同じリージョンを設定すること

kubeconfigを設定する

PS C:\dev> aws eks update-kubeconfig --name クラスタ名
  • クラスタ名はEKSサービスの画面から確認できるクラスタ名
    • ノードのEC2インスタンスのプレフィクスとかではない

kubectlする

PS C:\dev> kubectl get pod -o wide
NAME          READY     STATUS    RESTARTS   AGE       IP            NODE                                       NOMINATED NODE   READINESS GATES
cassandra-0   1/1       Running   0          11m       10.0.47.113   ip-10-0-41-23.us-east-2.compute.internal   <none>           <none>
cassandra-1   1/1       Running   0          10m       10.0.32.237   ip-10-0-43-23.us-east-2.compute.internal   <none>           <none>
cassandra-2   1/1       Running   0          8m39s     10.0.50.67    ip-10-0-41-23.us-east-2.compute.internal   <none>           <none>
cassandra-3   1/1       Running   0          7m9s      10.0.50.120   ip-10-0-43-23.us-east-2.compute.internal   <none>           <none>
cassandra-4   1/1       Running   0          5m43s     10.0.52.196   ip-10-0-41-23.us-east-2.compute.internal   <none>           <none>
cassandra-5   1/1       Running   0          4m34s     10.0.50.217   ip-10-0-43-23.us-east-2.compute.internal   <none>           <none>

トラブルシュート

kubectl時にエラーメッセージ

error: You must be logged in to the server (Unauthorized)

公式を参照する

Amazon EKS トラブルシューティング
https://docs.aws.amazon.com/ja_jp/eks/latest/userguide/troubleshooting.html#unauthorized

原因

これは、クラスターがAWS 認証情報のあるセット (IAM ユーザーまたはロール) から作成されたが、kubectl は別の認証情報のセットを使用していることが原因である可能性があります。

私の場合、EKSクラスタを作成したIAMユーザと、このローカルに認証情報を設定したIAMユーザは違ったため、まさにそれが原因となる。

対応

おおむねはこちらを参照。

クラスターのユーザーまたは IAM ロールの管理
https://docs.aws.amazon.com/ja_jp/eks/latest/userguide/add-user-role.html

kubectl edit -n kube-system configmap/aws-auth
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  mapRoles: |
    - rolearn: arn:aws:iam::555555555555:role/devel-worker-nodes-NodeInstanceRole-74RF4UBDUKL6
      username: system:node:{{EC2PrivateDNSName}}
      groups:
        - system:bootstrappers
        - system:nodes
  mapUsers: |
    - userarn: arn:aws:iam::555555555555:user/admin
      username: admin
      groups:
        - system:masters

ConfigMap内のmapRolesmapUserskubectlを許可したいIAMロール or IAMユーザを追加すればよい。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1