安装docker

  1. 查看软件版本yum list docker-ce.x86_64 --showduplicates | sort -r
  2. 安装对应版本软件yum install -y docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io
 ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 ~]# yum install -y docker-ce
 ~]# mkdir /etc/docker/
 ~]# mkdir /data/docker
 ~]# systemctl start docker ; systemctl enable docker
 ~]# docker version # 检查版本
  • 账户不能用root账户
  • 所有节点的时间必须同步
  • 使用的非root账户要能正常使用docker
  • 支持docker版本为 19.03 18.09 18.06 17.09

    在各个节点建立自定义账户,使其可以用命令操作docker

    ``` useradd passwd usermod -aG docker su docker info
<a name="01ovE"></a>
## 配置主节点到各个节点用户的免密登录

ssh-keygen ssh-copy-id @<节点IP>

注意,应使用能够操作docker的用户(非root)做免密。在这个例子中使用rancher用户。

<a name="0TxpS"></a>
## RKE部署Kubernetes集群
以下操作都是root用户
<a name="UK9Mw"></a>
### 下载RKE
RKE[下载地址](http://mirror.cnrancher.com/),此处下载更快

wget https://github.com/rancher/rke/releases/download/v1.1.3/rke_linux-amd64

创建配置文件

rke config —name cluster.yml

```
./rke_linux-amd64 config

[+] Cluster Level SSH Private Key Path [~/.ssh/id_rsa]: 使用~/.ssh/id_rsa
[+] Number of Hosts [1]: 集群主机个数
[+] SSH Address of host (1) [none]: 主机1地址
[+] SSH Port of host (1) [22]: SSH端口
[+] SSH Private Key Path of host (10.180.210.237) [none]: 使用~/.ssh/id_rsa 这里是rancher用户的地址 
[+] SSH User of host (10.180.210.237) [ubuntu]: SSH用户名,这里使用rancher
[+] Is host (10.180.210.237) a Control Plane host (y/n)? [y]: 是否运行Control Plane
[+] Is host (10.180.210.237) a Worker host (y/n)? [n]: 是否是worker
[+] Is host (10.180.210.237) an etcd host (y/n)? [n]: 是否运行etcd
[+] Override Hostname of host (10.180.210.237) [none]: 是否重设hostname
[+] Internal IP of host (10.180.210.237) [none]: 主机内部IP
[+] Docker socket path on host (10.180.210.237) [/var/run/docker.sock]: docker sock路径,使用默认
[+] SSH Address of host (2) [none]: 主机2的配置,后续配置相同,不再赘述
......
[+] Network Plugin Type (flannel, calico, weave, canal) [canal]: 网络插件类型
[+] Authentication Strategy [x509]: 认证策略
[+] Authorization Mode (rbac, none) [rbac]: 认证模式
[+] Kubernetes Docker image [rancher/hyperkube:v1.15.5-rancher1]: k8s镜像名
[+] Cluster domain [cluster.local]: 集群域名
[+] Service Cluster IP Range [10.43.0.0/16]: 集群内服务IP的范围
[+] Enable PodSecurityPolicy [n]: pod安全策略
[+] Cluster Network CIDR [10.42.0.0/16]: 集群网络范围
[+] Cluster DNS Service IP [10.43.0.10]: 集群DNS的IP
[+] Add addon manifest URLs or YAML files [no]: 是否增加插件manifest URL或配置文件

创建cluster.yml完成后,可以使用简单的命令部署集群。此命令假定该cluster.yml文件与运行该命令的目录位于同一目录中。

./rke_linux-amd64 up
INFO[0000] Building Kubernetes cluster
INFO[0000] [dialer] Setup tunnel for host [10.0.0.1]
INFO[0000] [network] Deploying port listener containers
INFO[0000] [network] Pulling image [alpine:latest] on host [10.0.0.1]
...
INFO[0101] Finished building Kubernetes cluster successfully

遇到FATA[0337] Failed to get job complete status for job rke-network-plugin-deploy-job in namespace kube-system错误,重新执行一遍./rke_linux-amd64 up即可
如果遇到无法连接docker 试试root用户和rancher用户的免密是否可以正常使用

WARN[0000] Failed to set up SSH tunneling for host [10.60.158.144]: Can't retrieve Docker Info: error during connect: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/info": Unable to access node with address [10.60.158.144:22] using SSH. Please check if you are able to SSH to the node using the specified SSH Private Key and if you have configured the correct SSH username. Error: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

保存文件

警告 后期的故障排除和集群升级都需要以下文件

将以下文件的副本保存在安全位置:

mkdir $HOME/.kube/ cp kube_config_cluster.yml $HOME/.kube/config

测试您的连接,看看是否可以返回节点列表。

kubectl get nodes

<a name="IWLzV"></a>
### 启用 kubectl 自动补全

安装 bash-completion

apt-get install bash-completion yum install bash-completion

将 completion 脚本添加到你的 ~/.bashrc 文件

echo ‘source <(kubectl completion bash)’ >>~/.bashrc

将 completion 脚本添加到 /etc/bash_completion.d 目录

kubectl completion bash >/etc/bash_completion.d/kubectl bash 如果还不能 多bash 重写连接

<a name="DLiC2"></a>
## 增加节点
编辑cluster.yml文件 添加对应主机的地址和角色(红框韦新添加节点)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/2392831/1624500451099-60369de8-fb19-4463-bbfc-d8050f30ff0b.png#crop=0&crop=0&crop=1&crop=1&height=425&id=mZHvG&margin=%5Bobject%20Object%5D&name=image.png&originHeight=849&originWidth=798&originalType=binary&ratio=1&rotation=0&showTitle=false&size=137107&status=done&style=none&title=&width=399)

rke up —update-only

<a name="eHMea"></a>
## 删除节点
同理 删除对应节点的行<br />然后执行

rke up —update-only ```