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.

138 lines
4.2 KiB

  1. # Kubernetes on NIFCLOUD with Terraform
  2. Provision a Kubernetes cluster on [NIFCLOUD](https://pfs.nifcloud.com/) using Terraform and Kubespray
  3. ## Overview
  4. The setup looks like following
  5. ```text
  6. Kubernetes cluster
  7. +----------------------------+
  8. +---------------+ | +--------------------+ |
  9. | | | | +--------------------+ |
  10. | API server LB +---------> | | | |
  11. | | | | | Control Plane/etcd | |
  12. +---------------+ | | | node(s) | |
  13. | +-+ | |
  14. | +--------------------+ |
  15. | ^ |
  16. | | |
  17. | v |
  18. | +--------------------+ |
  19. | | +--------------------+ |
  20. | | | | |
  21. | | | Worker | |
  22. | | | node(s) | |
  23. | +-+ | |
  24. | +--------------------+ |
  25. +----------------------------+
  26. ```
  27. ## Requirements
  28. * Terraform 1.3.7
  29. ## Quickstart
  30. ### Export Variables
  31. * Your NIFCLOUD credentials:
  32. ```bash
  33. export NIFCLOUD_ACCESS_KEY_ID=<YOUR ACCESS KEY>
  34. export NIFCLOUD_SECRET_ACCESS_KEY=<YOUR SECRET ACCESS KEY>
  35. ```
  36. * The SSH KEY used to connect to the instance:
  37. * FYI: [Cloud Help(SSH Key)](https://pfs.nifcloud.com/help/ssh.htm)
  38. ```bash
  39. export TF_VAR_SSHKEY_NAME=<YOUR SSHKEY NAME>
  40. ```
  41. * The IP address to connect to bastion server:
  42. ```bash
  43. export TF_VAR_working_instance_ip=$(curl ifconfig.me)
  44. ```
  45. ### Create The Infrastructure
  46. * Run terraform:
  47. ```bash
  48. terraform init
  49. terraform apply -var-file ./sample-inventory/cluster.tfvars
  50. ```
  51. ### Setup The Kubernetes
  52. * Generate cluster configuration file:
  53. ```bash
  54. ./generate-inventory.sh > sample-inventory/inventory.ini
  55. ```
  56. * Export Variables:
  57. ```bash
  58. BASTION_IP=$(terraform output -json | jq -r '.kubernetes_cluster.value.bastion_info | to_entries[].value.public_ip')
  59. API_LB_IP=$(terraform output -json | jq -r '.kubernetes_cluster.value.control_plane_lb')
  60. CP01_IP=$(terraform output -json | jq -r '.kubernetes_cluster.value.control_plane_info | to_entries[0].value.private_ip')
  61. export ANSIBLE_SSH_ARGS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand=\"ssh root@${BASTION_IP} -W %h:%p\""
  62. ```
  63. * Set ssh-agent"
  64. ```bash
  65. eval `ssh-agent`
  66. ssh-add <THE PATH TO YOUR SSH KEY>
  67. ```
  68. * Run cluster.yml playbook:
  69. ```bash
  70. cd ./../../../
  71. ansible-playbook -i contrib/terraform/nifcloud/inventory/inventory.ini cluster.yml
  72. ```
  73. ### Connecting to Kubernetes
  74. * [Install kubectl](https://kubernetes.io/docs/tasks/tools/) on the localhost
  75. * Fetching kubeconfig file:
  76. ```bash
  77. mkdir -p ~/.kube
  78. scp -o ProxyCommand="ssh root@${BASTION_IP} -W %h:%p" root@${CP01_IP}:/etc/kubernetes/admin.conf ~/.kube/config
  79. ```
  80. * Rewrite /etc/hosts
  81. ```bash
  82. sudo echo "${API_LB_IP} lb-apiserver.kubernetes.local" >> /etc/hosts
  83. ```
  84. * Run kubectl
  85. ```bash
  86. kubectl get node
  87. ```
  88. ## Variables
  89. * `region`: Region where to run the cluster
  90. * `az`: Availability zone where to run the cluster
  91. * `private_ip_bn`: Private ip address of bastion server
  92. * `private_network_cidr`: Subnet of private network
  93. * `instances_cp`: Machine to provision as Control Plane. Key of this object will be used as part of the machine' name
  94. * `private_ip`: private ip address of machine
  95. * `instances_wk`: Machine to provision as Worker Node. Key of this object will be used as part of the machine' name
  96. * `private_ip`: private ip address of machine
  97. * `instance_key_name`: The key name of the Key Pair to use for the instance
  98. * `instance_type_bn`: The instance type of bastion server
  99. * `instance_type_wk`: The instance type of worker node
  100. * `instance_type_cp`: The instance type of control plane
  101. * `image_name`: OS image used for the instance
  102. * `working_instance_ip`: The IP address to connect to bastion server
  103. * `accounting_type`: Accounting type. (1: monthly, 2: pay per use)