You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.9 KiB

  1. ## Kubernetes on AWS with Terraform
  2. **Overview:**
  3. This project will create:
  4. * VPC with Public and Private Subnets in # Availability Zones
  5. * Bastion Hosts and NAT Gateways in the Public Subnet
  6. * A dynamic number of masters, etcd, and worker nodes in the Private Subnet
  7. * even distributed over the # of Availability Zones
  8. * AWS ELB in the Public Subnet for accessing the Kubernetes API from the internet
  9. **Requirements**
  10. - Terraform 0.8.7 or newer
  11. **How to Use:**
  12. - Export the variables for your AWS credentials or edit `credentials.tfvars`:
  13. ```
  14. export AWS_ACCESS_KEY_ID="www"
  15. export AWS_SECRET_ACCESS_KEY ="xxx"
  16. export AWS_SSH_KEY_NAME="yyy"
  17. export AWS_DEFAULT_REGION="zzz"
  18. ```
  19. - Rename `contrib/terraform/aws/terraform.tfvars.example` to `terraform.tfvars`
  20. - Update `contrib/terraform/aws/terraform.tfvars` with your data. By default, the Terraform scripts use CoreOS as base image. If you want to change this behaviour, see note "Using other distrib than CoreOs" below.
  21. - Allocate a new AWS Elastic IP. Use this for your `loadbalancer_apiserver_address` value (below)
  22. - Create an AWS EC2 SSH Key
  23. - Run with `terraform apply --var-file="credentials.tfvars"` or `terraform apply` depending if you exported your AWS credentials
  24. Example:
  25. ```commandline
  26. terraform apply -var-file=credentials.tfvars -var 'loadbalancer_apiserver_address=34.212.228.77'
  27. ```
  28. - Terraform automatically creates an Ansible Inventory file called `hosts` with the created infrastructure in the directory `inventory`
  29. - Ansible will automatically generate an ssh config file for your bastion hosts. To connect to hosts with ssh using bastion host use generated ssh-bastion.conf.
  30. Ansible automatically detects bastion and changes ssh_args
  31. ```commandline
  32. ssh -F ./ssh-bastion.conf user@$ip
  33. ```
  34. - Once the infrastructure is created, you can run the kubespray playbooks and supply inventory/hosts with the `-i` flag.
  35. Example (this one assumes you are using CoreOS)
  36. ```commandline
  37. ansible-playbook -i ./inventory/hosts ./cluster.yml -e ansible_ssh_user=core -e bootstrap_os=coreos -b --become-user=root --flush-cache
  38. ```
  39. ***Using other distrib than CoreOs***
  40. If you want to use another distribution than CoreOS, you can modify the search filters of the 'data "aws_ami" "distro"' in variables.tf.
  41. For example, to use:
  42. - Debian Jessie, replace 'data "aws_ami" "distro"' in variables.tf with
  43. data "aws_ami" "distro" {
  44. most_recent = true
  45. filter {
  46. name = "name"
  47. values = ["debian-jessie-amd64-hvm-*"]
  48. }
  49. filter {
  50. name = "virtualization-type"
  51. values = ["hvm"]
  52. }
  53. owners = ["379101102735"]
  54. }
  55. - Ubuntu 16.04, replace 'data "aws_ami" "distro"' in variables.tf with
  56. data "aws_ami" "distro" {
  57. most_recent = true
  58. filter {
  59. name = "name"
  60. values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-*"]
  61. }
  62. filter {
  63. name = "virtualization-type"
  64. values = ["hvm"]
  65. }
  66. owners = ["099720109477"]
  67. }
  68. - Centos 7, replace 'data "aws_ami" "distro"' in variables.tf with
  69. data "aws_ami" "distro" {
  70. most_recent = true
  71. filter {
  72. name = "name"
  73. values = ["dcos-centos7-*"]
  74. }
  75. filter {
  76. name = "virtualization-type"
  77. values = ["hvm"]
  78. }
  79. owners = ["688023202711"]
  80. }
  81. **Troubleshooting**
  82. ***Remaining AWS IAM Instance Profile***:
  83. If the cluster was destroyed without using Terraform it is possible that
  84. the AWS IAM Instance Profiles still remain. To delete them you can use
  85. the `AWS CLI` with the following command:
  86. ```
  87. aws iam delete-instance-profile --region <region_name> --instance-profile-name <profile_name>
  88. ```
  89. ***Ansible Inventory doesnt get created:***
  90. It could happen that Terraform doesnt create an Ansible Inventory file automatically. If this is the case copy the output after `inventory=` and create a file named `hosts`in the directory `inventory` and paste the inventory into the file.
  91. **Architecture**
  92. Pictured is an AWS Infrastructure created with this Terraform project distributed over two Availability Zones.
  93. ![AWS Infrastructure with Terraform ](docs/aws_kubespray.png)