KubeKey安装K8s集群
系统要求
- 一台或多台运行兼容 deb/rpm 的 Linux 操作系统的计算机,例如 Ubuntu 或 CentOS。
- 每台机器 2 GB 以上的内存,内存不足时应用会受限制。
- 用作控制平面节点的计算机上至少有 2 个 CPU。
- 集群中所有计算机之间具有完全的网络连接。你可以使用公共网络或专用网络。
节点网络配置:
root@master:~# cat /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
ens33:
dhcp4: false
addresses:
- 10.10.0.14/24
routes:
- to: default
via: 10.10.0.254
nameservers:
addresses: [114.114.114.114, 8.8.8.8]
root@worker:~# cat /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
ens33:
dhcp4: false
addresses:
- 10.10.0.18/24
routes:
- to: default
via: 10.10.0.254
nameservers:
addresses: [114.114.114.114, 8.8.8.8]
系统依赖
Kubernetes 要求操作系统预装以下依赖:
socat conntrack ipset ebtables chrony ipvsadm
安装系统依赖
apt install socat conntrack ebtables ipset chrony ipvsadm -y
在线安装会自动download依赖包进行安装
KubeKey 已为部分 Linux 发行版制作了预编译的依赖包,可在 iso-latest 获取。 支持的具体发行版与构建方式详见 依赖包管理。
一、获取KubeKey
- 方式一: Release 页面
在 Release 页面获取对应的二进制文件。
- 方式二: 执行脚本下载KubeKey最新版本
export KKZONE=cn
curl -sfL https://get-kk.kubesphere.io | SKIP_WEB_INSTALLER=true SKIP_PACKAGE=true sh -
| 原文件 | 解压后文件 |
|---|---|
| kubekey-v4.x.x-linux-amd64.tar.gz | kk:为 KubeKey 的二进制文件 |
| web-installer.tgz | dist:Web 页面资源。 host-check.yaml,kubernetes,kubesphere:任务模板文件。 schema:配置文件。 README.md:安装说明文档。 |
| package.sh | 离线安装包的构建脚本。 |
[!NOTE]
在线安装
二、安装K8s
2.1创建节点配置文件,执行以下命令创建文件
root@master:~# ./kk create inventory -o .
write inventory file to inventory.yaml success.
节点配置文件如下:
root@master:~# cat inventory.yaml
apiVersion: kubekey.kubesphere.io/v1
kind: Inventory
metadata:
name: default
spec:
hosts: # your can set all nodes here. or set nodes on special groups.
master:
connector:
type: ssh
host: 10.10.0.14
port: 22
user: root
password: "000000"
internal_ipv4: 10.10.0.14
worker:
connector:
type: ssh
host: 10.10.0.18
port: 22
user: root
password: "000000"
internal_ipv4: 10.10.0.18
groups:
# all kubernetes nodes.
k8s_cluster:
groups:
- kube_control_plane
- kube_worker
# control_plane nodes
kube_control_plane:
hosts:
- master
# worker nodes
kube_worker:
hosts:
- worker
# etcd nodes when etcd_deployment_type is external
etcd:
hosts:
- master
# image_registry:
# hosts:
# - master
# nfs nodes for registry storage. and kubernetes nfs storage
# nfs:
# hosts:
# - localhost
[!note]
密码字段字符串需用引号引起来
2.2 创建安装配置文件
root@master:~# ./kk create config --with-kubernetes v1.34 -o .
write config file to config-v1.34.yaml success.
root@master:~# cat config-v1.34.yaml
apiVersion: kubekey.kubesphere.io/v1
kind: Config
spec:
# If set to "cn", online downloads will prioritize domestic sources when available.
zone: "cn"
kubernetes:
# Specify the Kubernetes version to be installed.
kube_version: v1.34.3
# Specify the Helm version to be installed.
helm_version: v3.18.5
# Tag for the sandbox (pause) image used by pods.
sandbox_image:
tag: "3.10.1"
control_plane_endpoint:
# Supported HA types: local, kube_vip, haproxy.
# If set to local, configure local hostname resolution as follows:
# - Control-plane nodes: 127.0.0.1 .kubernetes.control_plane_endpoint.host
# - Worker nodes: .init_kubernetes_node .kubernetes.control_plane_endpoint.host
type: local
# Kube-vip image tag used for static pod deployment (when type is kube_vip).
kube_vip:
image:
tag: v0.7.2
# HAProxy image tag used for static pod deployment (when type is haproxy).
haproxy:
image:
tag: 2.9.6-alpine
etcd:
# Specify the etcd version to be installed.
etcd_version: v3.6.5
image_registry:
# Image registry type to install. Supported: harbor, docker-registry.
# Leave empty to skip installation (assuming an existing registry is available).
type: ""
# auth:
# # Address of the private image registry
# registry: "dockerhub.kubekey.local"
# Specify a VIP for image registry high availability. If set, enables HA.
ha_vip: ""
# ========== Image Registry - High Availability ==========
# keepalived image tag for load balancing when multiple registry nodes exist.
# keepalived_version: 2.0.20
# ========== Image Registry - Harbor ==========
# Harbor image tag (only valid if type is harbor).
#harbor_version: v2.10.2
# docker-compose binary
#dockercompose_version: v2.20.3
# ========== Image Registry - Docker Registry ==========
# Docker Registry image tag (only valid if type is docker-registry).
# docker_registry_version: 2.8.3
cri:
# Container runtime type. Supported: containerd, docker.
container_manager: containerd
# ========== CRI Tool ==========
# crictl binary version.
crictl_version: v1.34.0
# ========== Docker Runtime ==========
# Docker binary version.
#docker_version: 25.0.5
# cridockerd version (required for Kubernetes 1.24+).
# cridockerd_version: v0.3.21
# ========== Containerd Runtime ==========
# containerd binary version (active only when container_manager is containerd).
# containerd_version: v1.7.13
# runc binary version (active only when container_manager is containerd).
# runc_version: v1.1.12
cni:
# CNI plugin type. Supported: calico, cilium, flannel, hybridnet, kubeovn, other
type: calico
# ========== Multi-CNI ==========
# Multi-CNI type. Supported: multus, spiderpool, none.
multi_cni: none
# ========== Multi-CNI - Multus ==========
# Image tag for Multus (configure if needed).
# multus:
# image:
# tag: v4.2.4
# ========== Multi-CNI - Spiderpool ==========
# Spiderpool version (configure if needed).
# spiderpool_version: v1.1.1
# ========== Calico CNI ==========
# Calico version (effective only when type is calico).
calico_version: v3.31.3
# ========== Cilium CNI ==========
# Cilium version (effective only when type is cilium).
#cilium_version: 1.19.1
# ========== Flannel CNI ==========
# Flannel version (effective only when type is flannel).
#flannel_version: v0.27.4
# ========== Kube-OVN CNI ==========
# Kube-OVN version (effective only when type is kubeovn).
#kubeovn_version: v1.15.0
# ========== Hybridnet CNI ==========
# Hybridnet version (effective only when type is hybridnet).
#hybridnet_version: 0.6.8
storage_class:
# ========== Storage Class Configuration ==========
# ========== Local/OpenEBS Storage Class ==========
# Local storage class settings.
local:
enabled: true # Enable the local storage class.
default: true # Set as the default storage class.
# openebs/dynamic-localpv-provisioner helm chart version.
localpv_provisioner_version: 4.4.0
# ========== NFS Storage Class ==========
nfs:
enabled: false # Enable the NFS storage class.
# nfs-provisioner Helm chart version.
#nfs_provisioner_version: 4.0.18
dns:
# CoreDNS settings.
coredns:
image:
tag: v1.12.1
# NodeLocalDNS settings.
nodelocaldns:
enabled: true
image:
tag: 1.26.4
# the external images will to add in packages
image_manifests: []
三、安装Kubernetes
./kk create cluster -i inventory.yaml -c config.yaml
四、启用 kubectl 自动补全
KubeKey 默认不启用 kubectl 自动补全。
前置条件:确保已安装 bash-autocompletion 并正常工作。
# 安装 bash-completion
apt-get install bash-completion
# 将补全脚本添加到 ~/.bashrc
echo 'source <(kubectl completion bash)' >>~/.bashrc
# 将补全脚本添加到 /etc/bash_completion.d 目录 旧版
# 使用新标准目录
kubectl completion bash > /usr/share/bash-completion/completions/kubectl
五、删除集群
[!note]
清理包括网络资源、容器运行时、DNS hosts等配置
./kk delete cluster -i inventory.yaml --all --with-data
六、添加节点
方式一:清单文件
./kk add nodes -i inventory.yaml -c config.yaml
方式二:Add Nodes via Command-Line Flags
Ensure
inventory.yamldefines the connection information for the nodes to be added.Example:
spec: hosts: node1: connector: type: ssh host: 192.168.1.101 port: 22 user: root password: 123456 node2: connector: type: ssh host: 192.168.1.102 port: 22 user: root password: 123456
Execute the following command to add nodes and specify their roles:
./kk add nodes --control-plane node1 --worker node2 -i inventory.yaml -c config.yaml --override
--control-plane: Specifies the hostnames to be added as control plane nodes. Multiple nodes are separated by commas.--worker: Specifies the hostnames to be added as worker nodes. Multiple nodes are separated by commas.--etcd: Specifies the hostnames to be added as etcd nodes. Multiple nodes are separated by commas.--override: After successful execution, automatically adds the nodes to the corresponding groups and updatesinventory.yaml.