LoginSignup
8
10

More than 5 years have passed since last update.

KubernetesクライアントkubectlからAKSに接続手順について

Posted at

Azure Cliインストール&確認

CentOSにAzureCliをインストール

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[azure-cli]\nname=Azure CLI\nbaseurl=https://packages.microsoft.com/yumrepos/azure-cli\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/azure-cli.repo'
sudo yum install azure-cli -y

Windows へのインストール

MSI インストーラーをダウンロードしてインストールします。

macOS へのインストール

brew update && brew install azure-cli

ほかのOSへのインストール

こちらの公式手順を参照してください。

バージョン確認

[user@localhost ~]$ az --version
azure-cli (2.0.45)
...

Azureにログイン

Azureにログインするのは、OSにより違います。

CentOSでログイン

  • ログインコマンドを実行して、表示されたメッセージに従って別途ブラウザからhttps://microsoft.com/devicelogin にアクセスし、表示された認証コードを入力してからAzureにログインします。
[user@localhost ~]$ az login
No handlers could be found for logger "cli.azure.cli.core._session"
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code *XXXXXXXXX* to authenticate.
  • ブラウザからログインしたら、CentOSに下記のような内容が表示され、ログインが完了しました。
[
  {
    "cloudName": "AzureCloud",
    "id": "3e6a8eed-c177-4c49-b56d-xxxxxxxxxxxx",
    "isDefault": true,
    "name": "Azure Pass",
    "state": "Enabled",
    "tenantId": "3c729906-f921-xxxx-99d3-xxxxxxxxxxxx",
    "user": {
      "name": "xxxxx@live.com",
      "type": "user"
    }
  }
]
[user@localhost ~]$

Windows、MacOSでログイン

  • ログインコマンドを実行して、自動的に表示されたブラウザからログインすればAzureにログインします。
C:\Users\xxxxxx>az login
Note, we have launched a browser for you to login. For old experience with device code, use "az login --use-device-code"
You have logged in. Now let us find all subscriptions you have access to...
[
  {
    "cloudName": "AzureCloud",
    "id": "3e6a8eed-c177-4c49-b56d-xxxxxxxxxxxx",
    "isDefault": true,
    "name": "Azure Pass",
    "state": "Enabled",
    "tenantId": "3c729906-f921-4f0d-99d3-xxxxxxxxxxxx",
    "user": {
      "name": "xxxxxx@live.com",
      "type": "user"
    }
  }
]

Azureリソース プロバイダーの登録ステータスの確認

  • az provider show --namespace Microsoft.ContainerService --query "{Provider:namespace, Status:registrationState}"コマンドでAzureリソース プロバイダーの登録ステータスを確認します。
[user@localhost ~]$ az provider show --namespace Microsoft.ContainerService --query "{Provider:namespace, Status:registrationState}"
{
  "Provider": "Microsoft.ContainerService",
  "Status": "Registered"
}
  • 上記結果に"Status": "Registered"が表示されない場合、az provider register -n Microsoft.ContainerServiceコマンドでリソース プロバイダーを登録します。
[user@localhost ~]$ az provider register -n Microsoft.ContainerService
Registering is still on-going. You can monitor using 'az provider show -n Microsoft.ContainerService'

Kubenetesクライアントアプリ(kubectl)のインストール

  • AZコマンドaz aks install-cliで簡単にKubenetesクライアントアプリをインストールすることができます。
[user@localhost ~]$ sudo az aks install-cli
No handlers could be found for logger "cli.azure.cli.core._session"
Downloading client to "/usr/local/bin/kubectl" from "https://storage.googleapis.com/kubernetes-release/release/v1.11.2/bin/linux/amd64/kubectl"
Please ensure that /usr/local/bin is in your search PATH, so the `kubectl` command can be found.
  • インストールが終わりましたら、kubectl versionコマンドでkubectlのバージョンを確認します。
[user@localhost ~]$ kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.3", GitCommit:"a4529464e4629c21224b3d52edfe0ea91b072862", GitTreeState:"clean", BuildDate:"2018-09-09T18:02:47Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
[user@localhost ~]$

まだKubernetesクラスターにログインしていないため、クラスターのバージョンが表示されませんでした。

Azure Kubernetesクラスター認証情報の取得

kubectlクライアントからAzure Kubernetesクラスターにログインするには、az aks get-credentials --resource-group ${リソースグループ名} --name ${クラスター名}コマンドでクラスター認証情報を取得します。

リソースグループ名とクラスター名はいずれもクラスター作成時に指定した引数です。

[user@localhost ~]$ az aks get-credentials --resource-group firstkube  --name firstkubecluster
/home/user/.kube/config has permissions "644".
It should be readable and writable only by its owner.
Merged "firstkubecluster" as current context in /home/user/.kube/config
[user@localhost ~]$ ls ~/.kube
cache  config  http-cache
[user@localhost ~]$

ログイン結果の確認

  • 再度kubectl versionコマンドでkubectlのバージョンを確認すると、クラスターのバージョンも表示されます。
[user@localhost ~]$ kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.3", GitCommit:"a4529464e4629c21224b3d52edfe0ea91b072862", GitTreeState:"clean", BuildDate:"2018-09-09T18:02:47Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.5", GitCommit:"32ac1c9073b132b8ba18aa830f46b77dcceb0723", GitTreeState:"clean", BuildDate:"2018-06-21T11:34:22Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
  • kubectl cluster-infoコマンドでクラスターの詳しい情報を表示することができます。
[user@localhost ~]$ kubectl cluster-info
Kubernetes master is running at https://firstkubecluster-XXXXXX.hcp.eastus.azmk8s.io:443
addon-http-application-routing-default-http-backend is running at https://firstkubecluster-XXXXXX.hcp.eastus.azmk8s.io:443/api/v1/namespaces/kube-system/services/addon-http-application-routing-default-http-backend/proxy
addon-http-application-routing-nginx-ingress is running at http://168.62.xxx.xxx:80 http://168.62.xxx.xxx:443
Heapster is running at https://firstkubecluster-XXXXXX.hcp.eastus.azmk8s.io:443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://firstkubecluster-XXXXXX.hcp.eastus.azmk8s.io:443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kubernetes-dashboard is running at https://firstkubecluster-XXXXXX.hcp.eastus.azmk8s.io:443/api/v1/namespaces/kube-system/services/kubernetes-dashboard/proxy
Metrics-server is running at https://firstkubecluster-XXXXXX.hcp.eastus.azmk8s.io:443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[user@localhost ~]$

ここでAKSに接続ができました。

8
10
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
8
10