使用RKE部署Kubernetes集群

一、简介

Rancher Kubernetes Engine(RKE)是一个用Golang编写的Kubernetes安装程序,相对于其它的工具来说,更加简单易用和上手。

二、主机规划

主机名称 系统镜像 IP 配置 角色
rancher-master ubuntu18.04 192.168.0.26 4C8G50G controlplane,etcd
rancher-node1 ubuntu18.04 192.168.0.27 8C16G50G node
rancher-node2 ubuntu18.04 192.168.0.28 8C16G50G node

三、环境准备

2.1 使用国内源

1sed  -i 's/nova.clouds.archive.ubuntu.com/mirrors.ustc.edu.cn/' /etc/apt/sources.list
2apt update

2.2 配置时间同步

1timedatectl set-timezone Asia/Shanghai
2
3apt -y install chrony
4systemctl enable --now chronyd

2.3 关闭swap

1swapoff -a                  # 临时关闭swap
2sed -i '/swap/d' /etc/fstab # 永久关闭swap

2.4 ssh免密码登录

使用普通用户,这里使用ubuntu用户

1$ ssh-keygen -t rsa -b 2048 -P '' -f ~/.ssh/id_rsa
2$ ssh-copy-id 192.168.0.26
3$ ssh-copy-id 192.168.0.27
4$ ssh-copy-id 192.168.0.28

2.5 开启tcp转发

1sed -i '/#AllowTcpForwarding/s/#//' /etc/ssh/sshd_config
2sed -i '/^AllowTcpForwarding/s/no/yes/' /etc/ssh/sshd_config
3systemctl restart sshd

2.6 安装docker-ce

 1curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
 2sudo add-apt-repository \
 3    "deb https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
 4    $(lsb_release -cs) \
 5    stable"
 6    
 7sudo apt-get update
 8sudo apt-get -y install docker-ce
 9# 将当前用户加入docker组
10sudo usermod -aG docker $USER
11# 更新用户组
12newgrp docker 

2.7 docker镜像加速

 1cat > /etc/docker/daemon.json <<EOF
 2{
 3  "registry-mirrors": [
 4    "https://mciwm180.mirror.aliyuncs.com",
 5    "https://docker.mirrors.ustc.edu.cn/",
 6    "https://registry.docker-cn.com"
 7  ],
 8  "exec-opts": ["native.cgroupdriver=systemd"]
 9}
10EOF
11
12systemctl daemon-reload
13systemctl restart docker

四、安装Kubernetes

4.1 安装rke

从rancher官方GitHub仓库下载RKE

1wget https://github.com/rancher/rke/releases/download/v1.1.4/rke_linux-amd64
2chmod +x rke_linux-amd64
3mv rke_linux-amd64 /usr/local/bin/rke

4.2 生成配置文件

 1rke config
 2[+] Cluster Level SSH Private Key Path [~/.ssh/id_rsa]:       # <-- 集群ssh私钥
 3[+] Number of Hosts [1]: 3                                    # <-- 集群规模
 4[+] SSH Address of host (1) [none]: 192.168.0.26              # <-- 第一台主机地址
 5[+] SSH Port of host (1) [22]:                                # <-- 第一台主机端口
 6[+] SSH Private Key Path of host (192.168.0.26) [none]: ~/.ssh/id_rsa # <-- ssh私钥路径
 7[+] SSH User of host (192.168.0.26) [ubuntu]:                 # <-- ssh用户,默认为ubuntu
 8[+] Is host (192.168.0.26) a Control Plane host (y/n)? [y]: y # <-- 是否为控制平面节点
 9[+] Is host (192.168.0.26) a Worker host (y/n)? [n]: n        # <-- 是否为工作节点
10[+] Is host (192.168.0.26) an etcd host (y/n)? [n]: y         # <-- 是否为etcd节点
11[+] Override Hostname of host (192.168.0.26) [none]:          # <-- 修改主机名
12[+] Internal IP of host (192.168.0.26) [none]:                # <-- 内网地址
13[+] Docker socket path on host (192.168.0.26) [/var/run/docker.sock]: # <-- docker socker路径
14[+] SSH Address of host (2) [none]: 192.168.0.27
15[+] SSH Port of host (2) [22]:
16[+] SSH Private Key Path of host (192.168.0.27) [none]: ~/.ssh/id_rsa
17[+] SSH User of host (192.168.0.27) [ubuntu]:
18[+] Is host (192.168.0.27) a Control Plane host (y/n)? [y]: n
19[+] Is host (192.168.0.27) a Worker host (y/n)? [n]: y
20[+] Is host (192.168.0.27) an etcd host (y/n)? [n]: n
21[+] Override Hostname of host (192.168.0.27) [none]:
22[+] Internal IP of host (192.168.0.27) [none]:
23[+] Docker socket path on host (192.168.0.27) [/var/run/docker.sock]:
24[+] SSH Address of host (3) [none]: 192.168.0.28
25[+] SSH Port of host (3) [22]:
26[+] SSH Private Key Path of host (192.168.0.28) [none]: ~/.ssh/id_rsa
27[+] SSH User of host (192.168.0.28) [ubuntu]:
28[+] Is host (192.168.0.28) a Control Plane host (y/n)? [y]: n
29[+] Is host (192.168.0.28) a Worker host (y/n)? [n]: y
30[+] Is host (192.168.0.28) an etcd host (y/n)? [n]: n
31[+] Override Hostname of host (192.168.0.28) [none]:
32[+] Internal IP of host (192.168.0.28) [none]:
33[+] Docker socket path on host (192.168.0.28) [/var/run/docker.sock]:
34[+] Network Plugin Type (flannel, calico, weave, canal) [canal]:  # <-- 网络插件类型
35[+] Authentication Strategy [x509]:                               # <-- 认证策略
36[+] Authorization Mode (rbac, none) [rbac]:                       # <-- 认证模式
37[+] Kubernetes Docker image [rancher/hyperkube:v1.18.6-rancher1]: # <-- 部署kubernetes docker镜像
38[+] Cluster domain [cluster.local]:                               # <-- 集群域名
39[+] Service Cluster IP Range [10.43.0.0/16]:                      # <-- service集群地址
40[+] Enable PodSecurityPolicy [n]:                                 # <-- 是否启用Pod安全策略
41[+] Cluster Network CIDR [10.42.0.0/16]:                          # <-- Pod地址范围
42[+] Cluster DNS Service IP [10.43.0.10]:                          # <-- Service地址范围
43[+] Add addon manifest URLs or YAML files [no]:                   # <-- 添加附加组件

执行完之后会在当前目录生成cluster.yml文件。

4.3 安装Kubernetes

1rke up

在安装过程中需要下载镜像,需要等待一段时间。

五、管理集群

5.1 安装kubectl

1apt -y install snapd
2snap install kubectl --classic
3
4# 或者直接使用wget下载
5wget https://storage.googleapis.com/kubernetes-release/release/v1.18.6/bin/linux/amd64/kubectl -O /usr/local/bin/kubectl
6chmod +x /usr/local/bin/kubectl

5.2 配置kubeconfig

1mkdir ~/.kube
2mv kube_config_cluster.yml ~/.kube/config

5.3 查看集群状态

1kubectl get node
2NAME           STATUS     ROLES               AGE   VERSION
3192.168.0.26   Ready      controlplane,etcd   46s   v1.18.6
4192.168.0.27   Ready      worker              31s   v1.18.6
5192.168.0.28   Ready      worker              14s   v1.18.6

六、测试

6.1 部署一个nginx

1kubectl create deploy nginx --image=nginx:1.16.1
2deployment.apps/nginx created

6.2 创建service

1kubectl expose deploy nginx --port=80 --type=NodePort
2service/nginx exposed

6.3 查看端口

1kubectl get svc
2NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
3kubernetes   ClusterIP   10.43.0.1       <none>        443/TCP        51m
4nginx        NodePort    10.43.139.184   <none>        80:30894/TCP   34s

6.4 测试访问

 1curl 192.168.0.26:30894
 2<!DOCTYPE html>
 3<html>
 4<head>
 5<title>Welcome to nginx!</title>
 6<style>
 7    body {
 8        width: 35em;
 9        margin: 0 auto;
10        font-family: Tahoma, Verdana, Arial, sans-serif;
11    }
12</style>
13</head>
14<body>
15<h1>Welcome to nginx!</h1>
16<p>If you see this page, the nginx web server is successfully installed and
17working. Further configuration is required.</p>
18
19<p>For online documentation and support please refer to
20<a href="http://nginx.org/">nginx.org</a>.<br/>
21Commercial support is available at
22<a href="http://nginx.com/">nginx.com</a>.</p>
23
24<p><em>Thank you for using nginx.</em></p>
25</body>
26</html>

好了,RKE部署Kubernetes集群就到这里了。更多高级用法可以上Rancher官网查看。