# Install Kubernetes with Kubespray
### 安裝 Python
```bash
sudo apt update
sudo apt install -y python python-pip
```
## 下載 Source Code
```shell
git clone https://github.com/kubernetes-incubator/kubespray
```
## 安裝 Requirments
```shell
pip install -r requirements.txt
```
## 更改設定檔
需要更改的設定檔有兩個,先將 sample 複製成一個新的
```bash
cp -rfp inventory/sample inventory/cluster
```
需要更改的有 ```inventory/cluster/group_vars/all.yml``` , ```inventory/cluster/host.ini``` 跟 ```inventory/cluster/group_vars/k8s-cluster.yml```
### ```all.yml```
在安裝 Kubernetes 在 OpenStack 並想使用 Cinder 作為 persistant volume backend 的話,將```all.yml``` 中的 cloud_provider 選項註解拿掉,並將值設為 ```'openstack'```
### ```host.ini```
```bash
declare -a IPS=(192.168.113.2 192.168.113.3 192.168.113.4)
CONFIG_FILE=inventory/mycluster/hosts.ini python3 contrib/inventory_builder/inventory.py ${IPS[@]}
```
將 IPS 中的 IP address 改為要安裝的 hosts 即可
### OpenStack
安裝在 OpenStack 的時候要確保
- ```host.ini``` 中的 hostname 要跟 instance name 一至
- ```ansible_host``` 部署主機需要能夠 access, ip 要為實際 instance 看到的 IP
- 使用 calico 的時候 OpenStack port 要設定 ```allowed_address_pairs```
- ```openstack server list``` 可以看到 instance-id
- ```neutron port-list -c id -c device_id``` 找到對應的 port
- ```neutron port-update PORT_ID --allowed_address_pairs list=true type=dict ip_address=10.233.0.0/18 ip_address=10.233.64.0/18```
- 需要 ```source openstack_rc_file``` 提供給 kubespray 一些 OpenStack 相關設定參數
## 開始部署
```ansible-playbook -i inventory/cluster/hosts.ini cluster.yml -e "ansible_become=true ansible_user=ubuntu"```
```ansible_become=true``` 是為了讓 Ansible 登入後切換到 root user
```ansible_user=ubuntu``` 是讓 Ansible 使用 ubuntu 當 username 登入 instance