kubespray - 为部署生产环境的 Kubernetes 集群

Created at: 2015-10-04 04:18:11
Language: Jinja
License: Apache-2.0

部署生产就绪型 Kubernetes 集群

Kubernetes Logo

如果你有任何疑问,请查看 kubespray.io 的文档,并加入我们的 kubernetes slack、channel #kubespray。你可以在此处获取邀请

  • 可以部署在 AWS、GCE、AzureOpenStackvSphereEquinix Metal(裸机)、Oracle Cloud Infrastructure (Experimental) 或 Baremetal
  • 高可用性群集
  • 可组合(例如,选择网络插件)
  • 支持最流行的 Linux 发行版
  • 持续集成测试

快速入门

要部署集群,你可以使用 :

安思博

用法

根据 Ansible 安装指南安装 Ansible,然后运行以下步骤:

# 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.yaml python3 contrib/inventory_builder/inventory.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 `--become` is required, as for example writing SSL keys in /etc/,
# installing packages and interacting with various systemd daemons.
# Without --become the playbook will fail to run!
ansible-playbook -i inventory/mycluster/hosts.yaml  --become --become-user=root cluster.yml

注意:当 Ansible 已经通过控制计算机上的系统包安装时,通过 安装的其他 python 包将转到不同的目录树(例如 在 Ubuntu 上)来自 Ansible's (例如 仍然在 Ubuntu 上)。因此,命令将失败,如下所示:

sudo pip install -r requirements.txt
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/ansible
ansible-playbook

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

probably pointing on a task depending on a module present in requirements.txt.

One way of solving this would be to uninstall the Ansible package and then, to install it via pip but it is not always possible. A workaround consists of setting

ANSIBLE_LIBRARY
and
ANSIBLE_MODULE_UTILS
environment variables respectively to the
ansible/modules
and
ansible/module_utils
subdirectories of pip packages installation location, which can be found in the Location field of the output of
pip show [package]
before executing
ansible-playbook
.

A simple way to ensure you get all the correct version of Ansible is to use the pre-built docker image from Quay. You will then need to use bind mounts to get the inventory and ssh key into the container, like this:

docker pull quay.io/kubespray/kubespray:v2.19.0
docker run --rm -it --mount type=bind,source="$(pwd)"/inventory/sample,dst=/inventory \
  --mount type=bind,source="${HOME}"/.ssh/id_rsa,dst=/root/.ssh/id_rsa \
  quay.io/kubespray/kubespray:v2.19.0 bash
# Inside the container you may now run the kubespray playbooks:
ansible-playbook -i /inventory/inventory.ini --private-key /root/.ssh/id_rsa cluster.yml

Vagrant

For Vagrant we need to install python dependencies for provisioning tasks. Check if Python and pip are installed:

python -V && pip -V

If this returns the version of the software, you're good to go. If not, download and install Python from here https://www.python.org/downloads/source/

Install Ansible according to Ansible installation guide then run the following step:

vagrant up

Documents

Supported Linux Distributions

  • Flatcar Container Linux by Kinvolk
  • Debian Bullseye, Buster, Jessie, Stretch
  • Ubuntu 16.04, 18.04, 20.04, 22.04
  • CentOS/RHEL 7, 8
  • Fedora 34, 35
  • Fedora CoreOS (see fcos Note)
  • openSUSE Leap 15.x/Tumbleweed
  • Oracle Linux 7, 8
  • Alma Linux 8
  • Rocky Linux 8
  • Amazon Linux 2 (experimental: see amazon linux notes)

Note: Upstart/SysV init based OS types are not supported.

Supported Components

Container Runtime Notes

  • The list of available docker version is 18.09, 19.03 and 20.10. The recommended docker version is 20.10. The kubelet might break on docker's non-standard version numbering (it no longer uses semantic versioning). To ensure auto-updates don't break your cluster look into e.g. yum versionlock plugin or apt pin).
  • The cri-o version should be aligned with the respective kubernetes version (i.e. kube_version=1.20.x, crio_version=1.20)

Requirements

  • Minimum required version of Kubernetes is v1.22
  • Ansible v2.11+, Jinja 2.11+ and python-netaddr is installed on the machine that will run Ansible commands
  • The target servers must have access to the Internet in order to pull docker images. Otherwise, additional configuration is required (See Offline Environment)
  • The target servers are configured to allow IPv4 forwarding.
  • If using IPv6 for pods and services, the target servers are configured to allow IPv6 forwarding.
  • The firewalls are not managed, you'll need to implement your own rules the way you used to. in order to avoid any issue during deployment you should disable your firewall.
  • If kubespray is ran from non-root user account, correct privilege escalation method should be configured in the target servers. Then the
    ansible_become
    flag or command parameters
    --become or -b
    should be specified.

Hardware: These limits are safe guarded by Kubespray. Actual requirements for your workload can differ. For a sizing guide go to the Building Large Clusters guide.

  • Master
    • Memory: 1500 MB
  • Node
    • Memory: 1024 MB

Network Plugins

You can choose between 10 network plugins. (default:

calico
, except Vagrant uses
flannel
)

  • flannel: gre/vxlan (layer 2) networking.

  • Calico is a networking and network policy provider. Calico supports a flexible set of networking options designed to give you the most efficient networking across a range of situations, including non-overlay and overlay networks, with or without BGP. Calico uses the same engine to enforce network policy for hosts, pods, and (if using Istio and Envoy) applications at the service mesh layer.

  • canal: a composition of calico and flannel plugins.

  • cilium: layer 3/4 networking (as well as layer 7 to protect and secure application protocols), supports dynamic insertion of BPF bytecode into the Linux kernel to implement security services, networking and visibility logic.

  • weave: Weave is a lightweight container overlay network that doesn't require an external K/V database cluster. (Please refer to

    weave
    troubleshooting documentation).

  • kube-ovn: Kube-OVN integrates the OVN-based Network Virtualization with Kubernetes. It offers an advanced Container Network Fabric for Enterprises.

  • kube-router: Kube-router is a L3 CNI for Kubernetes networking aiming to provide operational simplicity and high performance: it uses IPVS to provide Kube Services Proxy (if setup to replace kube-proxy), iptables for network policies, and BGP for ods L3 networking (with optionally BGP peering with out-of-cluster BGP peers). It can also optionally advertise routes to Kubernetes cluster Pods CIDRs, ClusterIPs, ExternalIPs and LoadBalancerIPs.

  • macvlan: Macvlan is a Linux network driver. Pods have their own unique Mac and Ip address, connected directly the physical (layer 2) network.

  • multus: Multus is a meta CNI plugin that provides multiple network interface support to pods. For each interface Multus delegates CNI calls to secondary CNI plugins such as Calico, macvlan, etc.

The choice is defined with the variable

kube_network_plugin
. There is also an option to leverage built-in cloud provider networking instead. See also Network checker.

Ingress Plugins

  • nginx: the NGINX Ingress Controller.

  • metallb: the MetalLB bare-metal service LoadBalancer provider.

Community docs and resources

Tools and projects on top of Kubespray

CI Tests

构建图形

CI/end-to-end tests sponsored by: CNCF, Equinix Metal, OVHcloud, ELASTX.

See the test matrix for details.