Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

What’s AKS

Slide 6

Slide 6 text

Kubernetes in Azure

Slide 7

Slide 7 text

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になってます。

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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のみ

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Practice AKS

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Set up AKS Cluster

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Deploy Application

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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が使用後も残ったままにならないようご注意ください 


Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Monitoring

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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を確認可能

Slide 34

Slide 34 text

Azure Monitoring ~ Insight ~

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Azure Monitoring ~ Log ~

Slide 38

Slide 38 text

Thank you for Listening !

Slide 39

Slide 39 text

Appendix

Slide 40

Slide 40 text

Set up AKS Cluster

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Terraform ~完成~

Slide 50

Slide 50 text

Deploy Application with ACR

Slide 51

Slide 51 text

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)

Slide 52

Slide 52 text

以下のコマンドで、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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

現在、何も入っていない状態である。 $ 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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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