使用kubekey快速部署kubernetes

Posted by 梁远鹏 on 2021-10-25 | 阅读 |,阅读约 3 分钟

TOC

前言

Kubekey是青云研发并开源的一款快速部署kubernetes的工具,并且它也是下一代安装kubesphere的工具,使用kubekey你可以很轻松的就部署一套kubernetes/kubespher环境,我的体验是简直不要太简单了!

本文主要讲述的内容是用kubekey快速部署kubernets单节点,多节点部署的文章会在下一篇出现,敬请期待吧.

编写部署配置文件走起

首先用kk命令创建一个默认的配置文件:

[[email protected] temp]# kk create config
[[email protected] temp]# ls
config-sample.yaml
[[email protected] temp]# cat config-sample.yaml 
apiVersion: kubekey.kubesphere.io/v1alpha1
kind: Cluster
metadata:
  name: sample
spec:
  hosts:
  - {name: kk1, address: 172.16.0.2, internalAddress: 172.16.0.2, user: ubuntu, password: [email protected]}
    etcd:
    - node1
    master: 
    - node1
    worker:
    - node1
  controlPlaneEndpoint:
    ##Internal loadbalancer for apiservers 
    #internalLoadbalancer: haproxy

    domain: lb.kubesphere.local
    address: ""
    port: 6443
  kubernetes:
    version: v1.19.8
    clusterName: cluster.local
  network:
    plugin: calico
    kubePodsCIDR: 10.233.64.0/18
    kubeServiceCIDR: 10.233.0.0/18
  registry:
    registryMirrors: []
    insecureRegistries: []
  addons: []

修改其中的节点相关信息为自己实际的节点信息,可以看一我修改后我的配置文件:

[[email protected] ~]# cat config-sample.yaml 
apiVersion: kubekey.kubesphere.io/v1alpha1
kind: Cluster
metadata:
  name: lan
spec:
  hosts:
  - {name: kk1, address: 192.168.3.152, internalAddress: 10.0.2.11, user: root, password: 123,port: 111}
  roleGroups:
    etcd:
    - kk1
    master: 
    - kk1
    worker:
    - kk1
  controlPlaneEndpoint:

    domain: lb.kubesphere.local
    address: ""
    port: 6443
  kubernetes:
    version: v1.19.8
    clusterName: cluster.local
  network:
    plugin: calico
    kubePodsCIDR: 10.233.64.0/18
    kubeServiceCIDR: 10.233.0.0/18
  registry:
    registryMirrors: []
    insecureRegistries: []

我部署的是1.19.8版本的kubernetes,CNI使用的是kubekey默认的calico.

开始部署

在部署前设置一下环境变量:

export KKZONE=cn

因为默认情况下kk是去github上下载二进制文件(kubectl/kubelet/kubeadm…)以及去dockerhub下载容器镜像,设置了上面这个环境变量之后呢下载二进制文件就会去青云下载二进制文件以及去阿里镜像仓库下载需要的镜像,在网络方面的使用国内的东西很是很有必要的.

接下来开始部署:

[[email protected] tmp]# kk create cluster -f config-sample.yaml -y
INFO[11:40:08 EDT] Downloading Installation Files               
INFO[11:40:08 EDT] Downloading kubeadm ...                      
INFO[11:40:10 EDT] Downloading kubelet ...                      
INFO[11:40:11 EDT] Downloading kubectl ...                      
INFO[11:40:11 EDT] Downloading helm ...                         
INFO[11:40:11 EDT] Downloading kubecni ...                      
INFO[11:40:12 EDT] Downloading etcd ...                         
INFO[11:40:12 EDT] Downloading docker ...                       
INFO[11:40:12 EDT] Downloading crictl ...                       
INFO[11:40:12 EDT] Configuring operating system ...             
[kk0 192.168.3.152] MSG:
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-arptables = 1
...
INFO[11:40:24 EDT] Installing Container Runtime ...             
INFO[11:40:26 EDT] Start to download images on all nodes        
[kk2] Downloading image: registry.cn-beijing.aliyuncs.com/kubesphereio/pause:3.2
[kk0] Downloading image: registry.cn-beijing.aliyuncs.com/kubesphereio/pause:3.2
[kk2] Downloading image: registry.cn-beijing.aliyuncs.com/kubesphereio/kube-proxy:v1.19.8
[kk0] Downloading image: registry.cn-beijing.aliyuncs.com/kubesphereio/kube-apiserver:v1.19.8
...
INFO[11:42:41 EDT] Creating etcd service                        
Push /root/repo/git/tmp/kubekey/v1.19.8/amd64/etcd-v3.4.13-linux-amd64.tar.gz to 192.168.3.152:/tmp/kubekey/etcd-v3.4.13-linux-amd64.tar.gz   Done
INFO[11:42:45 EDT] Starting etcd cluster   
...

kubekey就会完成安装docker/containerd,所有需要的二进制文件并且开始用kubeadm部署kubernetes,一键部署简直不要太舒服了!

接下来只需要耐心等待就可以了.

部署完成,检查pod情况

当你看到的时候已经成功部署完了,看一下Pod的情况:

[[email protected] ~]# kubectl get po -A
NAMESPACE     NAME                                      READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-d75c96f46-dqq4z   1/1     Running   0          37m
kube-system   calico-node-hkpq9                         1/1     Running   0          37m
kube-system   coredns-867b49865c-qtjrz                  1/1     Running   0          37m
kube-system   coredns-867b49865c-vmsgs                  1/1     Running   0          37m
kube-system   kube-apiserver-kk1                        1/1     Running   0          37m
kube-system   kube-controller-manager-kk1               1/1     Running   0          37m
kube-system   kube-proxy-x7722                          1/1     Running   0          37m
kube-system   kube-scheduler-kk1                        1/1     Running   0          37m
kube-system   nodelocaldns-8hh5d                        1/1     Running   0          37m

可以看到,所有Pod都正常运行了,可以愉快的用kubernetes了.

注意点

  1. internalAddress需要填绑定网卡的IP地址,etcd会用到这个地址,否则在部署etcd的时候会失败.

  2. 记得设置环境变量KKZONE=cn,否则在下载二进制文件的时候可能会非常慢!

  3. 不要太快享受,要慢慢享受.

微信公众号

扫描下面的二维码关注我们的微信公众号,第一时间查看最新内容。同时也可以关注我的Github,看看我都在了解什么技术,在页面底部可以找到我的Github。

wechat-account-qrcode