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.

144 lines
6.9 KiB

7 years ago
  1. # Getting started
  2. ## Building your own inventory
  3. Ansible inventory can be stored in 3 formats: YAML, JSON, or INI-like. There is
  4. an example inventory located
  5. [here](https://github.com/kubernetes-sigs/kubespray/blob/master/inventory/sample/inventory.ini).
  6. You can use an
  7. [inventory generator](https://github.com/kubernetes-sigs/kubespray/blob/master/contrib/inventory_builder/inventory.py)
  8. to create or modify an Ansible inventory. Currently, it is limited in
  9. functionality and is only used for configuring a basic Kubespray cluster inventory, but it does
  10. support creating inventory file for large clusters as well. It now supports
  11. separated ETCD and Kubernetes control plane roles from node role if the size exceeds a
  12. certain threshold. Run `python3 contrib/inventory_builder/inventory.py help` for more information.
  13. Example inventory generator usage:
  14. ```ShellSession
  15. cp -r inventory/sample inventory/mycluster
  16. declare -a IPS=(10.10.1.3 10.10.1.4 10.10.1.5)
  17. CONFIG_FILE=inventory/mycluster/hosts.yml python3 contrib/inventory_builder/inventory.py ${IPS[@]}
  18. ```
  19. Then use `inventory/mycluster/hosts.yml` as inventory file.
  20. ## Starting custom deployment
  21. Once you have an inventory, you may want to customize deployment data vars
  22. and start the deployment:
  23. **IMPORTANT**: Edit my\_inventory/groups\_vars/\*.yaml to override data vars:
  24. ```ShellSession
  25. ansible-playbook -i inventory/mycluster/hosts.yml cluster.yml -b -v \
  26. --private-key=~/.ssh/private_key
  27. ```
  28. See more details in the [ansible guide](/docs/ansible.md).
  29. ### Adding nodes
  30. You may want to add worker, control plane or etcd nodes to your existing cluster. This can be done by re-running the `cluster.yml` playbook, or you can target the bare minimum needed to get kubelet installed on the worker and talking to your control planes. This is especially helpful when doing something like autoscaling your clusters.
  31. - Add the new worker node to your inventory in the appropriate group (or utilize a [dynamic inventory](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html)).
  32. - Run the ansible-playbook command, substituting `cluster.yml` for `scale.yml`:
  33. ```ShellSession
  34. ansible-playbook -i inventory/mycluster/hosts.yml scale.yml -b -v \
  35. --private-key=~/.ssh/private_key
  36. ```
  37. ### Remove nodes
  38. You may want to remove **control plane**, **worker**, or **etcd** nodes from your
  39. existing cluster. This can be done by re-running the `remove-node.yml`
  40. playbook. First, all specified nodes will be drained, then stop some
  41. kubernetes services and delete some certificates,
  42. and finally execute the kubectl command to delete these nodes.
  43. This can be combined with the add node function. This is generally helpful
  44. when doing something like autoscaling your clusters. Of course, if a node
  45. is not working, you can remove the node and install it again.
  46. Use `--extra-vars "node=<nodename>,<nodename2>"` to select the node(s) you want to delete.
  47. ```ShellSession
  48. ansible-playbook -i inventory/mycluster/hosts.yml remove-node.yml -b -v \
  49. --private-key=~/.ssh/private_key \
  50. --extra-vars "node=nodename,nodename2"
  51. ```
  52. If a node is completely unreachable by ssh, add `--extra-vars reset_nodes=false`
  53. to skip the node reset step. If one node is unavailable, but others you wish
  54. to remove are able to connect via SSH, you could set `reset_nodes=false` as a host
  55. var in inventory.
  56. ## Connecting to Kubernetes
  57. By default, Kubespray configures kube_control_plane hosts with insecure access to
  58. kube-apiserver via port 8080. A kubeconfig file is not necessary in this case,
  59. because kubectl will use <http://localhost:8080> to connect. The kubeconfig files
  60. generated will point to localhost (on kube_control_planes) and kube_node hosts will
  61. connect either to a localhost nginx proxy or to a loadbalancer if configured.
  62. More details on this process are in the [HA guide](/docs/ha-mode.md).
  63. Kubespray permits connecting to the cluster remotely on any IP of any
  64. kube_control_plane host on port 6443 by default. However, this requires
  65. authentication. One can get a kubeconfig from kube_control_plane hosts
  66. (see [below](#accessing-kubernetes-api)) or connect with a [username and password](/docs/vars.md#user-accounts).
  67. For more information on kubeconfig and accessing a Kubernetes cluster, refer to
  68. the Kubernetes [documentation](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/).
  69. ## Accessing Kubernetes Dashboard
  70. Supported version is kubernetes-dashboard v2.0.x :
  71. - Login option : token/kubeconfig by default
  72. - Deployed by default in "kube-system" namespace, can be overridden with `dashboard_namespace: kubernetes-dashboard` in inventory,
  73. - Only serves over https
  74. Access is described in [dashboard docs](https://github.com/kubernetes/dashboard/tree/master/docs/user/accessing-dashboard). With kubespray's default deployment in kube-system namespace, instead of kubernetes-dashboard :
  75. - Proxy URL is <http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#/login>
  76. - kubectl commands must be run with "-n kube-system"
  77. Accessing through Ingress is highly recommended. For proxy access, please note that proxy must listen to [localhost](https://github.com/kubernetes/dashboard/issues/692#issuecomment-220492484) (`proxy --address="x.x.x.x"` will not work)
  78. For token authentication, guide to create Service Account is provided in [dashboard sample user](https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/creating-sample-user.md) doc. Still take care of default namespace.
  79. Access can also by achieved via ssh tunnel on a control plane :
  80. ```bash
  81. # localhost:8081 will be sent to control-plane-1's own localhost:8081
  82. ssh -L8001:localhost:8001 user@control-plane-1
  83. sudo -i
  84. kubectl proxy
  85. ```
  86. ## Accessing Kubernetes API
  87. The main client of Kubernetes is `kubectl`. It is installed on each kube_control_plane
  88. host and can optionally be configured on your ansible host by setting
  89. `kubectl_localhost: true` and `kubeconfig_localhost: true` in the configuration:
  90. - If `kubectl_localhost` enabled, `kubectl` will download onto `/usr/local/bin/` and setup with bash completion. A helper script `inventory/mycluster/artifacts/kubectl.sh` also created for setup with below `admin.conf`.
  91. - If `kubeconfig_localhost` enabled `admin.conf` will appear in the `inventory/mycluster/artifacts/` directory after deployment.
  92. - The location where these files are downloaded to can be configured via the `artifacts_dir` variable.
  93. NOTE: The controller host name in the admin.conf file might be a private IP. If so, change it to use the controller's public IP or the cluster's load balancer.
  94. You can see a list of nodes by running the following commands:
  95. ```ShellSession
  96. cd inventory/mycluster/artifacts
  97. ./kubectl.sh get nodes
  98. ```
  99. If desired, copy admin.conf to ~/.kube/config.
  100. ## Setting up your first cluster
  101. [Setting up your first cluster](/docs/setting-up-your-first-cluster.md) is an
  102. applied step-by-step guide for setting up your first cluster with Kubespray.