本文参考原文:https://www.colabug.com/4082258.html
- 下载项目:
项目地址:
https://github.com/dengwubogit/docker-1/tree/master/No.10/deployk8s-master
-
集群架构:
- 文件结构:
- 把Vagrantfile中的box名与box镜像关联起来
# 手动下载: https://vagrantcloud.com/centos/boxes/7/versions/1809.01/providers/virtualbox.box
# vagrant box add centos7 path_to_your_centos7
# 例如:
vagrant box add centos7 /Users/dengwubo/Downloads/CentOS-7-x86_64-Vagrant-1809_01.VirtualBox.box
- 开发机(dev)的 vagrantfile 避免主机网段冲突
Vagrant.configure(2) do |config|
# 设置dev虚机环境(下面还要写dev.vm,好扯淡)
config.vm.define "dev" do |dev|
# 设置虚拟机的Box
dev.vm.box = "centos7"
# 设置虚拟机的主机名
dev.vm.hostname = "dev"
# 设置虚拟机的IP,与主机网段发生冲突时请更换网段
dev.vm.network "private_network", ip: "192.168.0.2"
# VirtaulBox相关配置
dev.vm.provider "virtualbox" do |vb|
# 设置虚拟机的内存大小
vb.memory = 512
# 设置虚拟机的CPU个数
vb.cpus = 1
end
# 挂载失败了,mount: unknown filesystem type 'vboxsf'
# config.vm.synced_folder ".", "/home/vagrant/deployk8s"
# 使用默认挂载
# 使用shell脚本进行软件安装和配置
dev.vm.provision "shell", path: "dev.sh"
end
# ssh配置
config.ssh.username = "vagrant"
config.ssh.private_key_path = "~/.vagrant.d/insecure_private_key"
config.ssh.insert_key = false
end
- 启动dev:
vagrant up dev
# 图方便我已经把不安全的公钥添加到集群机器的/root/.ssh/authorized_keys中
# 为了让root能从dev远程登录到集群机器,需要把insecure_private_key弄到dev的~/.ssh/id_rsa中,可以scp
scp -i ~/.vagrant.d/insecure_private_key ~/.vagrant.d/insecure_private_key vagrant@192.168.0.2:~/.ssh/id_rsa
#这个id_rsa的权限是644,需要改为600,在dev机中执行:
chmod 600 ~/.ssh/id_rsa
- 集群的 vagrantfile 避免主机网段冲突
Vagrant.configure(2) do |config|
(1..3).each do |i|
# 配置master虚机
config.vm.define "master#{i}" do |master|
# 设置虚拟机的Box
master.vm.box = "centos7"
# 设置虚拟机的主机名
master.vm.hostname = "kube-master#{i}"
# 设置虚拟机的IP
master.vm.network "private_network", ip: "192.168.0.#{i+10}"
# VirtaulBox相关配置
master.vm.provider "virtualbox" do |vb|
# 设置虚拟机的内存大小
vb.memory = 512
# 设置虚拟机的CPU个数
vb.cpus = 1
end
# master上shell脚本
master.vm.provision "shell", path: "master.sh"
end
# 配置node虚机
config.vm.define "node#{i}" do |node|
# 设置虚拟机的Box
node.vm.box = "centos7"
# 设置虚拟机的主机名
node.vm.hostname = "kube-node#{i}"
# 设置虚拟机的IP
node.vm.network "private_network", ip: "192.168.0.#{i+20}"
# VirtaulBox相关配置
node.vm.provider "virtualbox" do |vb|
# 设置虚拟机的内存大小
vb.memory = 512
# 设置虚拟机的CPU个数
vb.cpus = 1
end
# node上shell脚本
node.vm.provision "shell", path: "node.sh"
end
end
# master和node公共脚本
config.vm.provision "shell", path: "common.sh"
# ssh配置
config.ssh.username = "vagrant"
config.ssh.private_key_path = "~/.vagrant.d/insecure_private_key"
config.ssh.insert_key = false
end
- 启动集群:
cd vagrant-cluster
vagrant up master1
vagrant up master2
vagrant up master3
vagrant up node1
vagrant up node2
vagrant up node3
# 可以直接vagrant up来启动所有机器
- 使用finallshell XShell 等工具 连接dev机器:
9.1: 查看私钥位置: vagrant ssh-config
Host dev
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/dengwubo/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
9.2: 使用公钥方式连接虚拟机
- 进入dev虚拟主机:
vagrant ssh dev
#这一步的目的是把压缩包直接放在vagrant共享目录下解压,
#结果在最后报软连接错误,直接放入虚拟机的自身目录就不会爆这个错误这个很重要
cp -r deployk8s-master deployk8s
cd deployk8s
- 配置k8s软件环境: 全部放到 dev 的 deployk8s 目录下
# 有些需要翻墙,不过所有的软件我都放在百度云了
# 链接:https://pan.baidu.com/s/1WUyg6Wi5hvu1dzIk4XvQDA 密码:3z7t
# 下载cfssl
curl -O https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
curl -O https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
curl -O https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
# 下载keepalived v2.0.6
curl -O http://www.keepalived.org/software/keepalived-2.0.6.tar.gz
# 下载etcd v3.3.8
curl -O https://github.com/coreos/etcd/releases/download/v3.3.8/etcd-v3.3.8-linux-amd64.tar.gz
# 下载flannel v0.10.0
curl -O https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
# 下载kubernetes v1.11.0
curl -O https://dl.k8s.io/v1.11.0/kubernetes-server-linux-amd64.tar.gz
# PS: 国内镜像获取方式
https://blog.csdn.net/nklinsirui/article/details/80581286
- 检查是否有docker服务:
检查:
[vagrant@dev ~]$ docker -v
Docker version 18.06.1-ce, build e68fc7a
无服务 -> 执行脚本 -> install_docker.sh
PS: 需要检查所有节点是否有docker服务(重要!!!)
------------------------------------------------------------------
|install_docker.sh|
-------------------
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum makecache fast
sudo yum -y install docker-ce
sudo systemctl start docker
------------------------------------------------------------------
- 修改env.sh 文件主要是修改网段,与vagrantefile 配置的网段一致就好
- 还是在 dev 的 deployk8s 目录下: 赋予权限,并执行部署脚本
sudo chmod +x `pwd`\* #暴力一点,毕竟是学习为主
./deployk8s.sh 2>&1 | tee deployk8s.log
-
如果没有告诉你哪里失败,你只需要等待.....直到成功!