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.

124 lines
3.8 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. - Create an AWS EC2 SSH Key
  22. - Run with `terraform apply --var-file="credentials.tfvars"` or `terraform apply` depending if you exported your AWS credentials
  23. Example:
  24. ```commandline
  25. terraform apply -var-file=credentials.tfvars
  26. ```
  27. - Terraform automatically creates an Ansible Inventory file called `hosts` with the created infrastructure in the directory `inventory`
  28. - 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.
  29. Ansible automatically detects bastion and changes ssh_args
  30. ```commandline
  31. ssh -F ./ssh-bastion.conf user@$ip
  32. ```
  33. - Once the infrastructure is created, you can run the kubespray playbooks and supply inventory/hosts with the `-i` flag.
  34. Example (this one assumes you are using CoreOS)
  35. ```commandline
  36. ansible-playbook -i ./inventory/hosts ./cluster.yml -e ansible_user=core -e bootstrap_os=coreos -b --become-user=root --flush-cache
  37. ```
  38. ***Using other distrib than CoreOs***
  39. If you want to use another distribution than CoreOS, you can modify the search filters of the 'data "aws_ami" "distro"' in variables.tf.
  40. For example, to use:
  41. - Debian Jessie, replace 'data "aws_ami" "distro"' in variables.tf with
  42. data "aws_ami" "distro" {
  43. most_recent = true
  44. filter {
  45. name = "name"
  46. values = ["debian-jessie-amd64-hvm-*"]
  47. }
  48. filter {
  49. name = "virtualization-type"
  50. values = ["hvm"]
  51. }
  52. owners = ["379101102735"]
  53. }
  54. - Ubuntu 16.04, replace 'data "aws_ami" "distro"' in variables.tf with
  55. data "aws_ami" "distro" {
  56. most_recent = true
  57. filter {
  58. name = "name"
  59. values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-*"]
  60. }
  61. filter {
  62. name = "virtualization-type"
  63. values = ["hvm"]
  64. }
  65. owners = ["099720109477"]
  66. }
  67. - Centos 7, replace 'data "aws_ami" "distro"' in variables.tf with
  68. data "aws_ami" "distro" {
  69. most_recent = true
  70. filter {
  71. name = "name"
  72. values = ["dcos-centos7-*"]
  73. }
  74. filter {
  75. name = "virtualization-type"
  76. values = ["hvm"]
  77. }
  78. owners = ["688023202711"]
  79. }
  80. **Troubleshooting**
  81. ***Remaining AWS IAM Instance Profile***:
  82. If the cluster was destroyed without using Terraform it is possible that
  83. the AWS IAM Instance Profiles still remain. To delete them you can use
  84. the `AWS CLI` with the following command:
  85. ```
  86. aws iam delete-instance-profile --region <region_name> --instance-profile-name <profile_name>
  87. ```
  88. ***Ansible Inventory doesnt get created:***
  89. 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.
  90. **Architecture**
  91. Pictured is an AWS Infrastructure created with this Terraform project distributed over two Availability Zones.
  92. ![AWS Infrastructure with Terraform ](docs/aws_kubespray.png)