本文介紹瞭如何經過Kubespray來進行部署高可用k8s集羣,k8s版本爲1.12.5。node
代碼倉庫:https://github.com/kubernetes...python
參考文檔:https://kubespray.io/#/linux
因爲kubespray是依賴於ansible,ansible經過ssh協議進行主機之間的訪問,因此部署以前須要設置主機之間免密登陸,步驟以下:git
ssh-keygen -t rsa scp ~/.ssh/id_rsa.pub root@IP:/root/.ssh ssh root@IP cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
注意:不要經過使用github倉庫master分支的代碼,我這裏使用的是tag v2.8.3進行部署github
wget https://github.com/kubernetes-sigs/kubespray/archive/v2.8.3.tar.gz tar -xvf v2.8.3 cd kubespray-v2.8.3
Kubernetes安裝大部分都是使用的國外的鏡像,因爲防火牆緣由沒有辦法獲取到這些鏡像,因此須要本身建立鏡像倉庫並將這些鏡像獲取到上傳到鏡像倉庫中。docker
鏡像倉庫咱們選用的組件是Harbor,安裝步驟參考:centos
https://github.com/goharbor/h...api
在文件roles/download/defaults/main.yml文件中,能夠看到使用的全量鏡像列表,注意某些鏡像因爲功能未使用的緣由因此暫時沒有用到,咱們主要用到有以下鏡像:性能優化
使用的鏡像列表以下,在這裏我申請了一臺國外的阿里雲主機,在該臺主機下載所需鏡像而後上傳至私有鏡像倉庫服務器
例如操做某個鏡像時,須要執行以下命令:
docker pull gcr.io/google_containers/kubernetes-dashboard-amd64:v1.10.0 docker tag gcr.io/google_containers/kubernetes-dashboard-amd64:v1.10.0 106.14.219.69:5000/google_containers/kubernetes-dashboard-amd64:v1.10.0 docker push 106.14.219.69:5000/google_containers/kubernetes-dashboard-amd64:v1.10.0
在inventory/testcluster/group_vars/k8s-cluster/k8s-cluster.yml文件中添加以下配置:
# kubernetes image repo define kube_image_repo: "10.0.0.183:5000/google_containers" ## modified by: robbin # comment: 將使⽤的組件的鏡像倉庫修改成私有鏡像倉庫地址 etcd_image_repo: "10.0.0.183:5000/coreos/etcd" coredns_image_repo: "10.0.0.183:5000/coredns" calicoctl_image_repo: "10.0.0.183:5000/calico/ctl" calico_node_image_repo: "10.0.0.183:5000/calico/node" calico_cni_image_repo: "10.0.0.183:5000/calico/cni" calico_policy_image_repo: "10.0.0.183:5000/calico/kube-controllers" hyperkube_image_repo: "{{ kube_image_repo }}/hyperkube-{{ image_arch }}" pod_infra_image_repo: "{{ kube_image_repo }}/pause-{{ image_arch }}" dnsautoscaler_image_repo: "{{ kube_image_repo }}/cluster-proportional-autoscaler-{ { image_arch }}" dashboard_image_repo: "{{ kube_image_repo }}/kubernetes-dashboard-{{ image_arch }}"
因爲咱們的私有鏡像倉庫未配置https證書,須要在 inventory/testcluster/group_vars/all/docker.yml文件中添加以下配置:
docker_insecure_registries: - 10.0.0.183:5000
因爲默認從Docker官方源安裝docker,速度很是慢,這裏咱們更換爲國內阿里源,在inventory/testcluster/group_vars/k8s-cluster/k8s-cluster.yml文件中添加以下配置:
# CentOS/RedHat docker-ce repo docker_rh_repo_base_url: 'https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable' docker_rh_repo_gpgkey: 'https://mirrors.aliyun.com/docker-ce/linux/centos/gpg' dockerproject_rh_repo_base_url: 'https://mirrors.aliyun.com/docker-engine/yum/repo/main/centos/7' dockerproject_rh_repo_gpgkey: 'https://mirrors.aliyun.com/docker-engine/yum/gpg'
另外因爲須要從google以及github下載一些可執行文件,因爲防火牆緣由沒法直接在服務器上下載,咱們能夠預先將這些執行文件下載好,而後上傳到指定的服務器路徑中
可執行文件下載地址能夠在roles/download/defaults/main.yml文件中查找到,下載路徑以下:
kubeadm_download_url: "https://storage.googleapis.com/kubernetes-release/release/v 1.12.5/bin/linux/amd64/kubeadm" hyperkube_download_url: "https://storage.googleapis.com/kubernetes-release/release /v1.12.5/bin/linux/amd64/hyperkube" cni_download_url: "https://github.com/containernetworking/plugins/releases/downloa d/v0.6.0/cni-plugins-amd64-v0.6.0.tgz"
接下來修改文件權限,並上傳到每臺服務器的/tmp/releases目錄下
chmod 755 cni-plugins-amd64-v0.6.0.tgz hyperkube kubeadm scp cni-plugins-amd64-v0.6.0.tgz hyperkube kubeadm root@node1:/tmp/releases
k8s所須要的組件
可選插件列表
k8s的服務發現依賴於DNS,涉及到兩種類型的網絡:主機網絡和容器網絡,因此Kubespray提供了兩種配置來進行管理
dns_mode 主要用於集羣內的域名解析,有以下幾種類型,咱們的技術選型是coredns,注意:選擇某種dns_mode,可能須要下載安裝多個容器鏡像,其鏡像版本也可能不一樣
resolvconf_mode主要用來解決當容器部署爲host網絡模式的時候,如何使用k8s的dns,這裏咱們使用的是docker_dns
resolvconf_mode: docker_dns
kube-proxy能夠選擇ipvs或者iptables,在這裏咱們選擇的是ipvs模式,關於這二者的區別能夠參考 華爲雲在 K8S 大規模場景下的 Service 性能優化實踐(https://zhuanlan.zhihu.com/p/...)
網絡插件列表以下,咱們的技術選型是calico,注意:選擇某種網絡插件,可能須要一個或多個容器鏡像,其鏡像版本也可能不一樣
# Install dependencies from ``requirements.txt`` sudo pip install -r requirements.txt # Copy `inventory/sample` as `inventory/mycluster` cp -rfp inventory/sample inventory/mycluster # Update Ansible inventory file with inventory builder declare -a IPS=(10.10.1.3 10.10.1.4 10.10.1.5) CONFIG_FILE=inventory/mycluster/hosts.ini python3 contrib/inventory_builder/invent ory.py ${IPS[@]} # Review and change parameters under `inventory/mycluster/group_vars` cat inventory/mycluster/group_vars/all/all.yml cat inventory/mycluster/group_vars/k8s-cluster/k8s-cluster.yml # Deploy Kubespray with Ansible Playbook - run the playbook as root # The option `-b` is required, as for example writing SSL keys in /etc/, # installing packages and interacting with various systemd daemons. # Without -b the playbook will fail to run! ansible-playbook -i inventory/mycluster/hosts.ini --become --become-user=root clus ter.yml
部署完成,能夠登陸到k8s-master所在的主機,執行以下命令,能夠看到各個組件正常
kubectl cluster-info kubectl get node kubectl get pods --all-namespaces
參考文檔:
https://github.com/kubernetes...
https://xdatk.github.io/2018/...
https://jicki.me/kubernetes/d...