Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AKS 101 in Kubernetes Novice Tokyo #1

ry
May 26, 2020

AKS 101 in Kubernetes Novice Tokyo #1

ry

May 26, 2020
Tweet

More Decks by ry

Other Decks in Technology

Transcript

  1. AKS 101
    2020 / 05 / 26
    Kubernetes Novice Meetup on Youtube
    @URyo_0213

    View Slide

  2. Who am I ?
    ry (@URyo_0213)
    Infrastructure Engineer
    ○ Storage構築
    ○ Kubernetes, Python, Ansible
    ● Kubernetes歴: 1年ちょっと

    View Slide

  3. What can we use for practice?
    ● kubeadm
    ● Kind (Kubernetes in Docker)
    ● Kubernetes Services
    ○ AKS (Azure)
    ○ EKS (AWS)
    ○ GKE (GCP)

    View Slide

  4. What I’ll talk about
    ● kubeadm
    ● Kind (Kubernetes in Docker)
    ● Managed Services
    ○ AKS (Azure)
    ○ EKS (AWS)
    ○ GKE (GCP)

    View Slide

  5. What’s AKS

    View Slide

  6. Kubernetes
    in Azure

    View Slide

  7. Kubernetes Architecture in Azure
    ● Cluster = Control Plane(Azure Managed) + Node (Customer Managed)
    ● Versionが豊富
    ● 1ヶ月 $200 クレジット(Azure全体)
    ● 1NodeのClusterで多くて1000円/day (放置するだけなら400円程度)
    ● DefaultのNode Specは、Standard_DS2_v2
    ○ vCPU: 2, Ram: 7GiB, 一時storage(SSD): 14GiB (GiB = 1024^3 Byte)
    ※kubectl alpha debug はdisableになってます。

    View Slide

  8. AKS Cluster
    ● Control Plane (Master Node)
    ○ AKS クラスターを作成時、自動的に作成される。
    ○ 課金対象にはならない。
    ○ 特定の方法で構成したいとか、直接のアクセスが必要などの用件がある場
    合は、aks-engine を使用する必要がある。
    ● Node (Worker Node)
    ○ Serviceを動かす際に、1 つ以上のノードが必要。
    ○ CPU 数、メモリ量、ストレージのサイズと種類 (SSD, HDD など) を指定可
    能。
    ○ 同じ構成のNodeは同一Node Poolに所属する。

    View Slide

  9. Node Pool
    Node数1で作成したAKS Clusterの例は以下のようになる。
    $ az aks nodepool list --resource-group kubernetes --cluster-name aksCluster -o table
    Name OsType KubernetesVersion VmSize Count MaxPods ProvisioningState
    --------- -------- ------------------- --------------- ------- ---------
    -------------------
    nodepool1 Linux 1.15.10 Standard_DS2_v2 1 110 Succeeded
    nodepool1に入るのは、
    OS: Linux
    Kubernetes Version: 1.15.10
    VM Size: Standard_DS2_v2
    であるNodeのみ

    View Slide

  10. Node Pool
    $ az aks nodepool add --resource-group kubernetes --cluster-name aksCluster
    --name nodepool2 --node-count 1 --node-vm-size Standard_DS1_v2
    --kubernetes-version 1.15.7
    $ az aks nodepool list --resource-group kubernetes --cluster-name aksCluster -o table
    Name OsType KubernetesVersion VmSize Count MaxPods ProvisioningState
    --------- -------- ------------------- --------------- ------- ---------
    -------------------
    nodepool1 Linux 1.15.10 Standard_DS2_v2 1 110 Succeeded
    nodepool2 Linux 1.15.7 Standard_DS1_v1 1 110 Succeeded
    $ kubectl get nodes
    NAME STATUS ROLE AGE VERSION
    aks-nodepool1-23888308-vmss000000 Ready agent 77m v1.15.10
    aks-nodepool2-23888308-vmss000000 Ready agent 8m50s v1.15.7

    View Slide

  11. Practice AKS

    View Slide

  12. Contents
    ● Set up AKS Cluster
    ○ Step1. Create Resource Group
    ○ Step2. Deploy AKS Cluster
    ○ Step3. Get Credential
    ● Deploy Application
    ○ Create nginx pod
    ○ Create nginx pod with ACR
    ● Monitoring

    View Slide

  13. Set up AKS Cluster

    View Slide

  14. Set up AKS Cluster
    ● Step1. Create Resource Group
    ● Step2. Deploy AKS Cluster
    ● Step3. Get Credential

    View Slide

  15. Step1: Create Resource Group
    Resource Group:
    以下のコマンドにて作成。
    $ az group create -n kubernetes -l eastus2
    ※terraformで作成する際は、作成はしないですすむ 。

    View Slide

  16. Step2: Deploy AKS Cluster
    3つの作成法を紹介
    ● Azure Portal (GUI)
    ● Azure ctl
    以下は、Appendixを参照ください。
    ● Terraform
    ○ tf files (https://github.com/ryojsb/AKS-Terraform)

    View Slide

  17. Azure Portal ~step1~
    Kubernetes Serviceを選択し、「Add」を選択。

    View Slide

  18. Azure Portal ~step2~
    必須パラメータ(Resource Group, AKS名)を入力し「Review + create」を選択。

    View Slide

  19. Azure Portal ~step2~
    「Next」を押して詳細に設定をしていく場合
    ● Node pools
    ○ 追加のNode Pool設定
    ○ Virtual Node (必要なときのみ立ち上げるノード )
    ○ VM Scale Sets (同一Nodeをグルーピングし、自動スケーリングを実施 )
    ● Authentication
    ○ Service principals (Azure上でのアプリケーションがもつ ID)
    ○ RBAC
    ● Networking
    ● Integrations
    ○ ACR (Azure Container Registry)
    ○ Azure Monitor
    ● Tag

    View Slide

  20. Azure Portal ~step3~
    内容を確認し、「Create」を選択。

    View Slide

  21. Azure ctl
    $ az aks create -g kubernetes -n aksCluster \
    --kubernetes-version 1.15.10 --node-count 1 \
    --generate-ssh-keys
    その他、optionは以下を参照。
    https://docs.microsoft.com/en-us/cli/azure/aks?view=azure-cli-latest#az-aks-create

    View Slide

  22. Step3: Get Credential
    AKSが作成されたことを確認。
    $ az aks list -o table
    Name Location ResourceGroup KubernetesVersion ProvisioningState Fqdn
    ---------- ---------- --------------- ------------------- ------------------- -----------------------------------------------------------
    aksCluster eastus2 kubernetes 1.15.10 Succeeded akscluster-kubernetes-cb54f9-9f418604.hcp.eastus2.azmk8s.io
    Contextの追加をする。
    $ az aks get-credentials --resource-group kubernetes --name
    Cluster の確認をします。
    $ kubectl get nodes
    NAME STATUS ROLES AGE VERSION
    aks-nodepool1-28971671-vmss000000 Ready agent 7m26s v1.15.10

    View Slide

  23. Deploy Application

    View Slide

  24. Deploy Option
    ● Deploy application with outside registry
    以下はAppendixを参照ください。
    ● Deploy application with ACR (Azure Container Registry)

    View Slide

  25. ● vi deployment.yaml
    Create nginx pod ~ Create manifest ~

    View Slide

  26. Create nginx pod ~ Apply Manifest ~
    $ kubectl create ns nginx

    $ kubectl apply -f deployment.yaml -n nginx
    $ kubectl get all
    NAME READY STATUS RESTARTS AGE
    pod/sample-nginx-75444db6d6-dv9pd 1/1 Running 0 87s
    pod/sample-nginx-75444db6d6-gbfz9 1/1 Running 0 87s
    pod/sample-nginx-75444db6d6-xrbxq 1/1 Running 0 87s
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    service/kubernetes ClusterIP 10.0.0.1 443/TCP 27h
    service/nginx-service LoadBalancer 10.0.144.35 52.167.75.0 80:32552/TCP 87s
    NAME READY UP-TO-DATE AVAILABLE AGE
    deployment.apps/sample-nginx 3/3 3 3 88s
    NAME DESIRED CURRENT READY AGE
    replicaset.apps/sample-nginx-75444db6d6 3 3 3 88s
    ※ LoadBalancer使用時は、切り出されたExternal IPが使用後も残ったままにならないようご注意ください 


    View Slide

  27. http://:80
    Create nginx pod ~ Check the application ~

    View Slide

  28. Monitoring

    View Slide

  29. OSS
    - Prometheus ・Grafana (Metrics)
    - ELK (Logging)
    - etc
    SaaS
    - DataDog
    - Sysdig
    - etc
    Azure Native
    - Azure Monitoring
    AKS Monitoring options

    View Slide

  30. OSS
    - Prometheus ・Grafana (Metrics)
    - ELK (Logging)
    - etc
    SaaS
    - DataDog
    - Sysdig
    - etc
    Azure Native
    - Azure Monitoring
    AKS Monitoring options

    View Slide

  31. Azure Monitor for container
    Kubernetes クラスターのパフォーマンスと正常性、およびコンテナーのワークロード
    を把握できます。
    ● 設定
    ○ GUI: AKS Cluster作成時にdefaultでEnableになっています。
    ○ CLI: 「--enable-addons monitoring」オプション
    主に以下を見ていく
    - Insight
    - Metric
    - Log
    Azure Monitoring

    View Slide

  32. Azure Monitoring
    Azure Monitor リソースを開き、サイドバーより「 Containers」を選択

    View Slide

  33. Azure Monitoring ~ Insight ~
    Insightでは、NodeやContainerの現状を可視化してくれます。
    ● Cluster
    ○ Node CPU utilization %
    ○ Node memory utilization %
    ○ Node count
    ○ Active pod count
    ● Health (Preview)
    ○ Kubernetesのインフラ(API server, Core DNS, kube-proxy, etc…)等のStatusを確認可能
    ● Node
    ○ Node毎のPodのStatusを確認可能
    ● Container
    ○ 各ContainerのStatusを確認可能
    ● Deployment (Preview)
    ○ 各DeploymentのStatusを確認可能

    View Slide

  34. Azure Monitoring ~ Insight ~

    View Slide

  35. Azure Monitoring ~ Metric ~
    以下の項目を指定し、グラフを表示可能
    ● Scope:
    ● Metric Namespace: Metricを取得する対象  ※ KubernetesのNamespaceではない
    ● Metric: 取得したいMetric
    ● Aggregation: Avg, Max, Min

    View Slide

  36. Azure Monitoring ~ Log ~
    Queryを記述して、Logの情報を可視化していきます。
    また、PrometheusからのMetricsをここに統合することができます。
    Metricsを回収し、それをLogへと変換してここで可視化することができます。
    https://docs.microsoft.com/ja-jp/azure/azure-monitor/insights/container-insights-prometheus-integration#applying-updated-configmap

    View Slide

  37. Azure Monitoring ~ Log ~

    View Slide

  38. Thank you for Listening !

    View Slide

  39. Appendix

    View Slide

  40. Set up AKS Cluster

    View Slide

  41. Terraform ~step1(Azure providerの定義)~
    main.tf を作成

    View Slide

  42. Terraform ~step2 (kubernetes cluster 定義)~
    k8s.tf を作成

    View Slide

  43. Terraform ~step3 (変数を宣言)~
    variables.tf を作成

    View Slide

  44. Terraform ~step4 (Terraform 出力ファイルを作成)~
    output.tf を作成

    View Slide

  45. Terraform ~step5~
    1. Portalにて「Storage Account」を選択。
    2. terraformが情報を格納しておくためのStorage Accountを選択
    (ない場合は作成してください)

    View Slide

  46. Terraform ~step5~
    3. Settings > Access Keyにおける「key1」の「キー値」を記録

    View Slide

  47. Terraform ~step5~
    4. 以下のコマンドを実施
    $ az storage container create -n tfstate \
    --account-name \
    --account-key
    5. 以下を実施し、出力の 及び を記録する。
    $ az ad sp create-for-rbac --skip-assignment --name

    View Slide

  48. Terraform ~step6 (AKS 構築)~
    あとは、以下のコマンドを実施して構築完了です。
    $ terraform init -backend-config="storage_account_name=" \
    -backend-config="container_name=tfstate" \
    -backend-config="access_key=" \
    -backend-config="key=codelab.microsoft.tfstate"
    $ export TF_VAR_client_id=
    $ export TF_VAR_client_secret=
    $ terraform plan -out out.plan
    $ terraform apply out.plan

    View Slide

  49. Terraform ~完成~

    View Slide

  50. Deploy Application
    with ACR

    View Slide

  51. Step1. Create ACR
    Step2. Login to ACR
    Step3. Push an image to ACR
    Step4. Establish authentication for ACR
    Step5. Deploy an application
    Create nginx pod with ACR (Azure Container Registry)

    View Slide

  52. 以下のコマンドで、ACRを作成します。
    $ az acr create --resource-group kubernetes --name rySampleACR --sku Basic
    確認をする。
    $ az acr list -o table
    NAME RESOURCE GROUP LOCATION SKU LOGIN SERVER CREATION DATE ADMIN ENABLED
    ----------- ---------------- ---------- ----- ---------------------- -------------------- ---------------
    rySampleACR kubernetes eastus2 Basic rysampleacr.azurecr.io 2020-03-29T07:50:04Z
    ここの「LOGIN SERVER」を記録しておいてください。
    Step1. Create ACR

    View Slide

  53. 以下のコマンドで、ACRにLoginします。
    $ az acr login -n rySampleACR
    Login Succeededと出力が表示されることを確認します。
    Step2. Login to ACR

    View Slide

  54. 現在、何も入っていない状態である。
    $ az acr repository list --name rySampleACR
    []
    docker tag コマンドでimage名を変えたものをpushします。
    $ docker push rysampleacr.azurecr.io/nginx:1.17.5
    確認してみます。
    $ az acr repository list --name rySampleACR
    [
    "nginx"
    ]
    Step3. Push an image to ACR

    View Slide

  55. AKSとACRとの間で認証が構成されている必要があります。
    まず、AKS に対して構成されているサービス プリンシパルの IDとACRのリソースIDを取得します。 


    $ CLIENT_ID=`az aks show --resource-group kubernetes --name aksCluster --query "servicePrincipalProfile.clientId" --output tsv`
    $ ACR_ID=`az acr show --name rySampleACR --resource-group kubernetes --query "id" --output tsv`
    その後、適切なアクセス権を付与するロールを作成します。
    $ az role assignment create --assignee $CLIENT_ID --role Reader --scope $ACR_ID --output json > ./Create_Role-`date
    "+%Y%m%d_%H%M%S"`.json
    Step4. Establish authentication for ACR

    View Slide

  56. Step5. Deploy an application ~ Create manifest ~
    ● vi deployment.yaml

    View Slide

  57. Step5. Deploy an application ~ Create manifest ~
    $ kubectl create ns nginx2
    $ kubectl apply -f deployment.yaml -n nginx2
    $ kubectl get all -n nginx2
    NAME READY STATUS RESTARTS AGE
    pod/sample-nginx2-d96fc9f8-hf7jj 1/1 Running 0 52s
    pod/sample-nginx2-d96fc9f8-m6fsg 1/1 Running 0 52s
    pod/sample-nginx2-d96fc9f8-x5mhv 1/1 Running 0 52s
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    service/nginx-service LoadBalancer 10.0.49.148 40.70.168.193 80:31531/TCP 52s
    NAME READY UP-TO-DATE AVAILABLE AGE
    deployment.apps/sample-nginx2 3/3 3 3 53s
    NAME DESIRED CURRENT READY AGE
    replicaset.apps/sample-nginx2-d96fc9f8 3 3 3 53s

    View Slide

  58. http://:80
    Step5. Deploy an application ~ Check the application ~

    View Slide