使用kubekey快速搭建k8s集群

项目仓库地址

支持的Kubernetes Versions

安装

选择自己想要下载的版本
复制下载链接并下载

示例:

shell 复制代码
wget https://github.com/kubesphere/kubekey/releases/download/v3.1.5/kubekey-v3.1.5-linux-amd64.tar.gz
解压到需要安装的位置

示例:

shell 复制代码
tar -xf kubekey-v*-linux-amd64.tar.gz -C /usr/local/bin
echo "export PATH=$PATH:/usr/local/bin/kk" >> ~/.bashrc
source ~/.bashrc
命令自动补全

(存疑:执行后没有效果)

shell 复制代码
echo "source <(kk completion -t bash)" >> ~/.bashrc
source ~/.bashrc

搭建k8s集群

示例:

生成一个配置文件
shell 复制代码
kk create config --name config-cluster-example
# 会生成一个config-cluster-example.yaml的配置文件
编辑配置文件
shell 复制代码
vim config-cluster-example.yaml
配置文件概述
yaml 复制代码
apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Cluster
metadata:
  name: config-cluster-example
spec:
  hosts: #配置集群的所有node信息,name是node的主机名,会自动把节点主机名改为name的值,address是节点ip,internalAddress是节点内网ip,user和password是登陆节点的用户名和密码,如果配置了ssh密钥登陆,可以删除user和password字段。
  - {name: node1, address: 172.16.0.2, internalAddress: 172.16.0.2, user: ubuntu, password: "Qcloud@123"}
  - {name: node2, address: 172.16.0.3, internalAddress: 172.16.0.3}
  roleGroups: # 指定节点的身份
    etcd:
    - node1
    control-plane: 
    - node1
    worker:
    - node1
    - node2
  controlPlaneEndpoint:
    ## Internal loadbalancer for apiservers 
    # internalLoadbalancer: haproxy

    domain: lb.kubesphere.local # apiServer监听的域名,默认即可
    address: "172.16.0.2" # apiServer监听的ip,需要自己指定
    port: 6443 # apiServer监听的端口号,默认即可
  kubernetes:
    version: v1.23.15 # 需要安装的集群版本,没有需求默认即可
    clusterName: cluster.local
    autoRenewCerts: true
    containerManager: docker
  etcd:
    type: kubekey
  network:
    plugin: calico
    kubePodsCIDR: 10.233.64.0/18
    kubeServiceCIDR: 10.233.0.0/18
    ## multus support. https://github.com/k8snetworkplumbingwg/multus-cni
    multusCNI:
      enabled: false
  registry:
    privateRegistry: ""
    namespaceOverride: ""
    registryMirrors: []
    insecureRegistries: []
  addons: []
安装必要组件
shell 复制代码
# 所有node都需要执行
apt install -y socat ipset conntrack chrony ipvsadm ebtables
创建集群
shell 复制代码
kk create cluster -yf config-cluster-example.yaml
安装kubectl(已安装则不用再进行安装)
shell 复制代码
# 命令自动补全
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc

扩展操作

删除集群
shell 复制代码
kk delete cluster <clusterName>
查看kubekey版本
shell 复制代码
kk version
更新集群版本
shell 复制代码
kk upgrade -y --with-kubernetes <version> <clusterName> 或
kk upgrade -y -f <config>.yaml # 推荐
删除node
shell 复制代码
kk delete node <nodeName>
添加node
shell 复制代码
kk add -y nodes <nodeName>或
kk add -y nodes -f <config>.yaml # 推荐
在worker上可以管理集群
shell 复制代码
mkdir -p .kube/config
scp root@<master>:/etc/kubernetes/admin.conf ~/.kube/config # 从master上拷贝 集群管理员的权限
echo "export KUBECONFIG=.kube/config/admin.conf" >> .bashrc
source .bashrc

# KubeKey 默认不会启用 kubectl 自动补全功能。
# 将 completion 脚本添加到你的 ~/.bashrc 文件
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc
# 将 completion 脚本添加到 /etc/bash_completion.d 目录
kubectl completion bash >/etc/bash_completion.d/kubect
创建一个部署了 KubeSphere 的 Kubernetes 集群 的 配置文件

例如 --with-kubesphere v3.1.0

shell 复制代码
kk create config --with-kubesphere --name <clusterName>
创建一个部署了 KubeSphere 的 Kubernetes 集群

例如 --with-kubesphere v3.1.0

shell 复制代码
kk create cluster --with-kubesphere v3.1.0
从已有的集群创建配置文件
shell 复制代码
kk create config --from-cluster cluster1 -f cluster2.yaml
创建容器是指定 container runtime

可选:docker, crio, containerd and isula

shell 复制代码
kk create cluster --container-manager <container_runtime> -f <config>.yaml
查看kubekey支持的kubernetes的版本
shell 复制代码
kk version --show-supported-k8s
查看kubesphere安装
shell 复制代码
kubectl apply -f https://github.com/kubesphere/ks-installer/releases/download/v3.4.1/kubesphere-installer.yaml

kubectl apply -f https://github.com/kubesphere/ks-installer/releases/download/v3.4.1/cluster-configuration.yaml
查看kubesphere删除
shell 复制代码
kubectl delete --force -f https://github.com/kubesphere/ks-installer/releases/download/v3.4.1/kubesphere-installer.yaml

kubectl delete --force -f https://github.com/kubesphere/ks-installer/releases/download/v3.4.1/cluster-configuration.yaml
相关推荐
chuanauc4 小时前
Kubernets K8s 学习
java·学习·kubernetes
小张是铁粉4 小时前
docker学习二天之镜像操作与容器操作
学习·docker·容器
烟雨书信4 小时前
Docker文件操作、数据卷、挂载
运维·docker·容器
IT成长日记4 小时前
【Docker基础】Docker数据卷管理:docker volume prune及其参数详解
运维·docker·容器·volume·prune
这儿有一堆花4 小时前
Docker编译环境搭建与开发实战指南
运维·docker·容器
LuckyLay4 小时前
Compose 高级用法详解——AI教你学Docker
运维·docker·容器
Uluoyu4 小时前
redisSearch docker安装
运维·redis·docker·容器
IT成长日记8 小时前
【Docker基础】Docker数据持久化与卷(Volume)介绍
运维·docker·容器·数据持久化·volume·
疯子的模样13 小时前
Docker 安装 Neo4j 保姆级教程
docker·容器·neo4j
虚伪的空想家13 小时前
rook-ceph配置dashboard代理无法访问
ceph·云原生·k8s·存储·rook