AWSã¨Terraformã§Kubernetesãå¦ã¶æ¬ãæ¸ããã®ã§ï¼ç« ãã¾ãã£ã¨å ¬éãã¾ã
ãã®è¨äºã¯ãKubernetes3 Advent Calendar 2020ã®25æ¥ç®ã®æ稿ã§ãã
æè¿AWSã¨Terraformã使ã£ã¦Kubernetesãå¦ã¶æ¬ãæ¸ãã¾ããã
æ¬è¨äºã§ã¯ããã®å
容ããã è¼ããã ãã®æ¥½ããã¦ãã¾ã£ã¦ãã¾ãããã¿ã¾ããããã ã®å®£ä¼ã§ãã
èå³ãæã£ãæ¹ã¯ãææ¥ãã販売éå§ã¨ãªãã¾ãã®ã§ãä¸è¨ããè³¼å
¥ãã¦é ããã°ã¨æãã¾ãã
ï¼ç« ã®å 容ã®ã»ãã«ã¯ãALBã¨ã®é£æºã«ãããµã¼ãã¹ã®å¤é¨å ¬éããªã¼ãã¹ã±ã¼ãªã³ã°ã®å®ç¾ãArgo CDã«ãããããã¤ã®å®è£ ãç£è¦ãéç¨ãªã©ã®å 容ãçãè¾¼ãã§ãã¾ãã
ããã§ã¯ãï¼ç« ãã¾ãã£ã¨å ¬éãã¾ãã
第1ç« ãã»ããã¢ãã
æ¬ç« ã§ã¯ãAWSã¨Terraformã使ã£ã¦ãKubernetesç°å¢ãæ§ç¯ãã¾ããAWSã§ã¯ãAmazon EKSï¼ä»¥éãEKSï¼ã使ãã¨ãããã¼ã¸ããªKubernetesç°å¢ãä½æã§ãã¾ããTerraformã§EKSã®ãªã½ã¼ã¹ãä½ãã®ã«ãTerraformå ¬å¼ã®ã¢ã¸ã¥ã¼ã«ã¬ã¸ã¹ããªã«ããterraform-aws-eks*1ãå©ç¨ãã¾ãã
Terraformã使ãã«ã¯AWS CLI*2ãå¿ è¦ã«ãªãã¾ããæ¬æ¸ã§ã¯ããã§ã«AWS CLIãã¤ã³ã¹ãã¼ã«ãã¦ãããã®ã¨ãã¾ãã
1.1ãTerraform
æ¬ç¯ã§ã¯ãTerraformã®ã»ããã¢ãããè¡ããTerraformã使ã£ã¦AWSã®ãªã½ã¼ã¹ãä½æãåé¤ã§ãããã¨ã確èªãã¾ããTerraformãAWSã§ä½¿ãã«ã¯ãTerraformç¨ã®IAMã¦ã¼ã¶ã¼ãä½æããã¢ã¯ã»ã¹ãã¼ãçºè¡ãã¾ãããã®ã¢ã¯ã»ã¹ãã¼ãAWS CLIã«è¨å®ããã¨TerraformããAWSãæä½ã§ãã¾ãã
1.1.1ãã»ããã¢ãã
TerraformããAWSã®ãªã½ã¼ã¹ãæä½ã§ããããã«ãTerraformã®ã»ããã¢ãããè¡ãã¾ãã
AWSã®ããã¼ã¸ã¡ã³ãã³ã³ã½ã¼ã«ã®IAMããã¦ã¼ã¶ã¼ãä½æãã¾ãï¼å³1.1ï¼ã
User nameãå ¥åãã¾ããProgrammatic accessããã§ãã¯ãã¦ãNextãã¯ãªãã¯ãã¾ãï¼å³1.2ï¼ã
Attach existing policies directlyãé¸æãã¾ããAdministratorAccessããã§ãã¯ããNextãã¯ãªãã¯ãã¾ãï¼å³1.3ï¼ã
ã¿ã°ã¯è¿½å ãããNextãã¯ãªãã¯ãã¾ããCreate userãã¯ãªãã¯ãã¾ãï¼å³1.4ï¼ã
ã¢ã¯ã»ã¹ãã¼ã表示ããã¾ãï¼å³1.5ï¼ã
ãã®ãã¨ã«ä½¿ç¨ãããããAccess key IDã¨Secret access keyãã³ãã¼ãã¦ããã¾ãã
AdministratorAccessã¯ãAWSã«ããã¦ç®¡çè 権éã¨ãªãIAMããªã·ã¼ã§ããä½ã§ãã§ãã¦ãã¾ã権éã®ãããã¢ã¯ã»ã¹ãã¼ã®æ±ãã«ã¯æ³¨æãå¿ è¦ã§ãã
次ã®ã³ãã³ãã§ãAWS CLIã®è¨å®ãè¡ãã¾ããè¨å®ãè¡ãã«ã¯ãaws configureãå©ç¨ãã¾ããæ¬æ¸ã§ã¯ããã§ã«AWS CLIãã¤ã³ã¹ãã¼ã«æ¸ã¿ã¨ãã¦ãã¾ãã
$ aws configure AWS Access Key ID [None]: AKIAXXXXXXXXXXXXXXX AWS Secret Access Key [None]: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Default region name [None]: ap-northeast-1 Default output format [None]: json
AWS Access Key IDã¨AWS Secret Access Keyã«ã³ãã¼ãã¦ãããã¢ã¯ã»ã¹ãã¼ãå ¥åãã¾ãã
次ã®ã³ãã³ãã§ãTerraformã®ã¤ã³ã¹ãã¼ã«ãè¡ãã¾ããmacOSã§ã¯brew*3ãå©ç¨ã§ãã¾ãã
$ brew install terraform
æ¬æ¸ã§ã¯ãTerraformã®ãã¼ã¸ã§ã³v0.13.5ã使ç¨ãã¦ãã¾ãã
次ã®ã¨ããã«ãterraformãå®è¡ãã¦Usageã®åºåãè¡ãããã°ãæ£å¸¸ã«ã¤ã³ã¹ãã¼ã«ããã¦ãã¾ãã
$ terraform Usage: terraform [-version] [-help] <command> [args] ...
1.1.2ãåºæ¬æä½
Terraformã§AWSãªã½ã¼ã¹ãä½æãåé¤ã§ãããä¸é£ã®æä½ãè¡ãã¾ãããã以éã®èª¬æã§ã¯ãTerraformã使ç¨ãã¦AWSãªã½ã¼ã¹ãä½æãã¾ãã®ã§ãããã§èª¬æããTerraformã®åºæ¬æä½ãç解ããããã«ãã¦ãã ããã
terraform init
Terraformã§ã¯ãä½æ¥ç¨ã®ãã£ã¬ã¯ããªãç¨æãã¦ãåæåãè¡ãã¾ãã次ã®ããã«ãã£ã¬ã¯ããªãç¨æãã¾ãã
$ mkdir example $ cd example
exampleãã£ã¬ã¯ããªã«main.tfãã¡ã¤ã«ãç¨æãããªã¹ã1.1ã®ããã«ã³ã¼ããæ¸ãã¾ãã
ãªã¹ã1.1ã¯ãEC2ã¤ã³ã¹ã¿ã³ã¹ãä½æããTerraformã®ã³ã¼ãã«ãªãã¾ãã
Terraformã®tfãã¡ã¤ã«ãç¨æã§ããããterraform initãå®è¡ãã¦ãTerraformç¨ã®å®è¡ãã£ã¬ã¯ããªã¨ãã¦exampleãã£ã¬ã¯ããªãã»ããã¢ãããã¾ãã
$ terraform init
terraform initã«ãã£ã¦ãAWSãæ±ãããã®Providerã¨ããããã°ã©ã ããã¦ã³ãã¼ãããã¦ãTerraformã§AWSãæä½ã§ãã¾ãã
terraform plan
èªèº«ã®AWSç°å¢ã«å¯¾ãã¦ãTerraformãã©ã®ããã«AWSãªã½ã¼ã¹ãä½æããã®ãã確èªãã¾ããterraform planãå®è¡ããã¨ã次ã®ããã«ãã©ã®AWSãªã½ã¼ã¹ã«å¤æ´ãããããææ¡ã§ãã¾ãã
$ terraform plan An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { ... } Plan: 1 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.
terraform planã®åºåçµæã«ããã+ããã¼ã¯ã¯ãªã½ã¼ã¹ãä½æãããã¨ã表ãã¦ãã¾ãããresource "aws_instance"ãã¨ããã®ã§ãEC2ã¤ã³ã¹ã¿ã³ã¹ãä½æããè¨ç»ã ã¨åããã¾ãã
terraform apply
å®è¡è¨ç»ã確èªã§ãããã次ã«ãTerraformã使ã£ã¦AWSãªã½ã¼ã¹ãä½æãã¾ããterraform applyãå®è¡ããã¨ãããããã¦planã®çµæã表示ãããå®è¡ããã確èªãè¡ããã¾ãã
$ terraform apply An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { ... } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
terraform applyã®å®è¡çµæã®æå¾ã«ãEnter a value:ãã¨åºåããããyesããå ¥åããã¨ãEC2ã¤ã³ã¹ã¿ã³ã¹ãä½æããã¾ãã
æå¾ã«æ¬¡ã®ããã«åºåãããã°æåã§ãã
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
å®éã«EC2ã¤ã³ã¹ã¿ã³ã¹ãä½æããã¦ããããAWSããã¼ã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§ç¢ºèªãã¾ããAWSããã¼ã¸ã¡ã³ãã³ã³ã½ã¼ã«ããEC2ã®ç»é¢ã表示ããã¨ãä½æããã¤ã³ã¹ã¿ã³ã¹ã確èªã§ãã¾ãï¼å³1.6ï¼ã
terraform destroy
Terraformã使ã£ã¦ãä½æãããªã½ã¼ã¹ãåé¤ãã¦ãå ã®ç¶æ ã«æ»ãã¾ãããã®ããã«ã¯ãterraform destroyãå®è¡ãã¾ãã
$ terraform destroy aws_instance.example: Refreshing state... [id=i-048b8ae615444c5f5] An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # aws_instance.example will be destroyed - resource "aws_instance" "example" { ... } Plan: 0 to add, 0 to change, 1 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes
terraform destroyã®åºåçµæã«ããã-ããã¼ã¯ã¯ãªã½ã¼ã¹ã®åé¤ã表ãã¦ãã¾ããapplyã³ãã³ãã¨åæ§ã«å®è¡ãã¦ããã確èªããã¾ãã®ã§ãyesã¨å ¥åãã¦ãåé¤ãå®è¡ãã¾ãã
AWSããã¼ã¸ã¡ã³ãã³ã³ã½ã¼ã«ã確èªããã¨ãEC2ã¤ã³ã¹ã¿ã³ã¹ãTerminatedã«ãªã£ã¦ãã¦ãåé¤ããã¦ãããã¨ãåããã¾ãï¼å³1.7ï¼ã
ããã¾ã§ãTerraformã®ã»ããã¢ããã¨åºæ¬æä½ã§ãã
ãã³ã©ã ãTerraformã®å¦ã³æ¹
æ¬æ¸ã§ã¯ãAWSãªã½ã¼ã¹ãä½ãããã ãã«Terraformã使ç¨ãã¦ãã¾ãããå®ã¯Terraformã®ä¸çã¯å¥¥ãæ·±ãã§ãããã¨ãã°ãã¢ã¸ã¥ã¼ã«æ©è½ã使ã£ã¦æ±ç¨çã«Terraformãªã½ã¼ã¹ãå®ç¾©ã§ãã¾ããã¾ããæ¢åã®AWSãªã½ã¼ã¹ãTerraformã§ç®¡çã§ããããã«ãã¤ã³ãã¼ãããæ©è½ãããã¾ãã
ãã®ããã«ãTerraformã«ã¯ããªã½ã¼ã¹ãä½ã以å¤ã«å¦ã¶ã¨ä¾¿å©ãªæ©è½ãããã¾ããæ¬æ¸ã§ã¯è¨åãã¾ããããæ¬æ ¼çã«Terraformãå¦ã³ããå ´åã«ã¯ããå®è·µTerraform AWSã«ãããã·ã¹ãã è¨è¨ã¨ãã¹ããã©ã¯ãã£ã¹ã*4ããªã¹ã¹ã¡ãã¾ãã
1.2ãAmazon EKS
æ¬ç¯ã§ã¯ãTerraformã使ç¨ãã¦ãEKSããã³EKSã«å¿ è¦ãªAWSãªã½ã¼ã¹ãä½æãã¾ããEKSã®ãªã½ã¼ã¹ãä½ãã®ã«terraform-aws-eks*1ãå©ç¨ãã¾ãããã®ã»ãVPCãªã©ã®ãããã¯ã¼ã¯é¢é£ã®AWSãªã½ã¼ã¹ãä½æãã¾ãã
ã¾ãã¯ã次ã®ããã«ä½æ¥ç¨ãã£ã¬ã¯ããªãç¨æãã¾ãã
ä½æ¥ç¨ãã£ã¬ã¯ããªå ã«å種tfãã¡ã¤ã«ãç¨æãã¾ããTerraformããAWSãå©ç¨ã§ããããã«ããªã¹ã1.2ã®ããã«å®ç¾©ãã¾ãã
regionã§AWSãªã¼ã¸ã§ã³ãæå®ãã¾ããlocalsã¯ãtfãã¡ã¤ã«å ã§ä½¿ç¨å¯è½ãªå¤æ°ãå®ç¾©ã§ãã¾ããããã§ã¯ãEKSã¯ã©ã¹ã¿ã®ååããã¼ã¸ã§ã³ãä¿æãã¦ãã¾ãã
EKSç¨ã®VPCä½æã¯ããªã¹ã1.3ã®ããã«vpc.tfãç¨æãã¾ãã
terraform-aws-modules/vpc/awsã使ãã¨VPCãSubnetãç°¡æ½ãªè¨è¿°ã§ä½æã§ãã¾ããCIDRã¯10.0.0.0/16ã使ç¨ãã¾ããèªèº«ã®ç°å¢ã§CIDRãæ¢åã®VPCã¨è¢«ã£ã¦ããå ´åã«ã¯å¤æ´ãå¿ è¦ã§ããpublic_subnetsã§Subnetãä½æãã¾ããSubnetã®IPã¢ãã¬ã¹æ°ã«ãã£ã¦ãKubernetesã¯ã©ã¹ã¿ã§èµ·åå¯è½ãªPodæ°ãå¶éããã¾ããããã§ã¯ããµãããããã¹ã¯ã24ã«ãã¦ãã¾ãããå®éã«ã¯ã¯ã©ã¹ã¿ã®è¦æ¨¡ã«å¿ãã¦èª¿æ´ãå¿ è¦ã§ããPodã«ã¤ãã¦ã¯ã第2ç« ã§èª¬æãã¾ããEKSã®æ±ºã¾ãã¨ãã¦ãå©ç¨ããsubnetã®ã¿ã°ã«ç¹å¥ãªæå®ãå¿ è¦ã§ãã*5
EKSã®ä½æã¯ããªã¹ã1.4ã®ããã«eks.tfãç¨æãã¾ãã
terraform-aws-modules/eks/awsã使ãã¨EKSãä½æã§ãã¾ããããã§ã¯ãClusterã®ãã¼ã¸ã§ã³ã«localsã®å¤æ°ã使ã£ã¦1.18ãæå®ãã¾ããnode_groupsãæå®ãã¦ãEKSã®ããã¼ã¸ããã¼ãã°ã«ã¼ããä½æãã¾ãã
terraform-aws-eksã§ã¯ããã¼ã¸ããã¼ãã°ã«ã¼ãã§ã¯ãªããã¼ãã°ã«ã¼ãã®ä½æããµãã¼ããã¦ãã¾ãããã®å ´åã¯ãworker_groupsãã£ã¼ã«ããæå®ãã¦ãã¼ãã°ã«ã¼ããä½æã§ãã¾ãã詳細ã¯terraform-aws-eksã®ããã¥ã¡ã³ãã確èªãã¦ã¿ã¦ãã ããã*1
Terraformã®å®è¡ä¸ã«Kubernetesç°å¢ã¸ã®ã¢ã¯ã»ã¹ãå¿ è¦ã«ãªãã¾ãããªã¹ã1.5ã®ããã«ãkubernetes.tfãç¨æãã¦ãTerraformãKubernetesç°å¢ã«ã¢ã¯ã»ã¹ã§ããããã«ãã¾ãã
ãªã¹ã1.5ã®ã³ã¼ãã¯ãçè ã試ããéãã§ã¯ãterraform applyã§ã¨ã©ã¼ã«ãªããªãããã«å¿ è¦ã§ããããªããããå¿ è¦ãªã®ãçè ãããåãã£ã¦ããªãã®ã§ããããã¾ããªãã¨æã£ã¦æ¸ãã¾ãããã
å©ç¨ããTerraformã®ãã¼ã¸ã§ã³ã«å¶éããããããããªã¹ã1.6ã®ããã«versions.tfãç¨æãã¾ãã
Terraformã®ãã¼ã¸ã§ã³ã0.12以ä¸ã§ä½¿ç¨ããããã«æå®ãã¾ãã
ããã¾ã§ã§ããããEKSã«é¢ä¿ããAWSãªã½ã¼ã¹ãä½æããæºåã¯å®äºã§ãã
次ã¯ãããã¾ã§ã®tfãã¡ã¤ã«ãå©ç¨ãã¦ãå®éã«AWSãªã½ã¼ã¹ãä½æãã¾ããã¾ãã¯ã次ã®ã¨ããã«terraform initãå®è¡ãã¦ãä½æ¥ãã£ã¬ã¯ããªã®ã»ããã¢ãããè¡ãã¾ãããã
$ terraform init
ç¶ãã¦ãterraform applyã§EKSãä½æãã¾ãã
$ terraform apply module.eks.data.aws_iam_policy_document.cluster_assume_role_policy: Refreshing state... module.eks.data.aws_partition.current: Refreshing state... module.eks.data.aws_iam_policy_document.cluster_elb_sl_role_creation[0]: Refreshing state... ... Enter a value: yes module.eks.aws_iam_policy.cluster_elb_sl_role_creation[0]: Creating... module.vpc.aws_vpc.this[0]: Creating... ... Apply complete! Resources: 33 added, 0 changed, 0 destroyed.
EKSã®ä½æå®äºã¾ã§ã«ã¯ã10åã»ã©ãããã¾ããã¨ã©ã¼ããªããapplyãå®äºããã°EKSãªã½ã¼ã¹ãä½ããã¦ãã¾ãã
1.3ãKubernetes
æ¬ç¯ã§ã¯ãEKSã§ä½æããKubernetesç°å¢ã®åä½ç¢ºèªã®ããã«ãKubernetesãªã½ã¼ã¹ã®åå¾ãã§ãããã確èªãã¾ããåç¯ã¾ã§ã§EKSã®ã»ããã¢ããã¯å®äºãã¦ãã¾ãããEKSã§ä½æããKubernetesç°å¢ã«æ¥ç¶ããããã®æä½ãå¿ è¦ã§ãã
次ã®ã³ãã³ãã§ãKubernetesãæä½ããããã®CLIãã¼ã«ã§ããkubectlãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ brew install kubectl
Kubernetesã«æ¥ç¶ããããã«å¿ è¦ãªkubeconfigã次ã®ã³ãã³ãã®ã¨ããã«ä½æãã¾ãã
$ aws eks --region ap-northeast-1 update-kubeconfig --name eks-example
aws eks update-kubeconfigã³ãã³ãã®--nameãªãã·ã§ã³ã«ã¯ãEKSã®ã¯ã©ã¹ã¿åãæå®ãã¾ãã
kubectlã¯kubeconfigã®è¨å®ãåç §ãã¦Kubernetesç°å¢ã«ã¢ã¯ã»ã¹ãã¾ããããã¾ã§ã®æä½ã§Kubernetesç°å¢ã¸ã®ã¢ã¯ã»ã¹ãå¯è½ã§ãã
æå¾ã«ãEKSã§ä½æããKubernetesç°å¢ã«ã¢ã¯ã»ã¹ã§ãããã確èªããããã«ã次ã®ã¨ããã«kubectl get podsãå®è¡ãã¦ã¿ã¾ãã
$ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system aws-node-5cckx 1/1 Running 0 9m37s kube-system aws-node-9rqqx 1/1 Running 0 9m37s kube-system coredns-86f7d88d77-ch98t 1/1 Running 0 23m kube-system coredns-86f7d88d77-gh9xm 1/1 Running 0 23m kube-system kube-proxy-6x6fz 1/1 Running 0 9m37s kube-system kube-proxy-shfjv 1/1 Running 0 9m37s
kubectl get podsã®åºåã«ã¯ãSTATUSãAGEã¨ãã£ãé ç®ãããã¾ããSTATUSãRunningã«ãªã£ã¦ããã°ãæ£å¸¸ã«Podãèµ·åãã¦ãã¾ããã¾ããAGEã¯Podã®èµ·åæéã表ãã¾ãã
ããã¾ã§ã§AWSã¨Terraformã使ã£ã¦ãã·ã³ãã«ãªKubernetesç°å¢ãæ§ç¯ã§ãã¾ããã注æã§ããããã®ã¾ã¾ç°å¢ãæ§ç¯ããã¾ã¾ã«ãã¦ããã¨ãEKSã§ã¯ï¼æéãããã0.10USDã®æéãçºçãã¾ããã¾ããå¥ã§EC2ã¤ã³ã¹ã¿ã³ã¹ã®æéãªã©ããããã¾ããä¸è¦ãªå ´åãé©å®terraform destroyã§åé¤ãã¾ãããã
ãããã§ããã§ããããï¼ããã¾ã§ãï¼ç« ã®å
容ã«ãªãã¾ãã
æ¹ãã¦ãæ¬æ¸ã®å
容ã«èå³ãæã£ãæ¹ã¯ãä¸è¨ããè³¼å
¥ããããããé¡ããã¾ãã