利用vagrant快速部署k8s环境
环境依赖
windows 10
vagrant 2.2.19
virtualbox 6.1.34
通过vagrant创建三台虚拟
Vagrantfile
内容如下
# -*- mode: ruby -*-
# vi: set ft=ruby :
# author: qhh0205
$num_nodes = 2
Vagrant.configure("2") do |config|
# k8s 主节点定义及初始化配置
config.vm.define "k8s-master" do | k8s_master |
k8s_master.vm.box = "centos/7"
k8s_master.vm.hostname = "k8s-master"
k8s_master.vm.network "private_network", ip: "192.168.10.100"
k8s_master.vm.provider "virtualbox" do | v |
v.name = "k8s-master"
v.memory = "2048"
v.cpus = 2
end
node.vm.provision "shell", path: "init.sh", privileged: false
node.vm.provision "shell", privileged: true, path: "./setup.sh"
end
# k8s node 节点定义及初始化配置
(1..$num_nodes).each do |i|
config.vm.define "k8s-node-#{i}" do |node|
node.vm.box = "centos/7"
node.vm.hostname = "k8s-node-#{i}"
node.vm.network "private_network", ip: "192.168.10.#{i+100}"
node.vm.provider "virtualbox" do |v|
v.name = "k8s-node-#{i}"
v.memory = "2048"
v.cpus = 2
end
node.vm.provision "shell", path: "init.sh", privileged: false
node.vm.provision "shell", privileged: true, path: "./setup.sh"
end
end
end
#manage hosts file
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.manage_guest = true
此脚本用于安装k8s必要的组件
init.sh
sudo yum update -y
# install some tools
sudo yum install -y vim telnet bind-utils wget
sudo yum install -y epel-release
sudo yum install -y lrzsz.x86_64
sudo yum install -y nmap-ncat.x86_64
sudo yum install -y net-tools
sudo yum install -y vim-enhanced.x86_64
sudo yum install -y sshpass
setup.sh
此脚本
#/bin/sh
sudo yum update -y
# install some tools
sudo yum install -y vim telnet bind-utils wget
# install docker
#curl -fsSL get.docker.com -o get-docker.sh
#sh get-docker.sh
## 安装docker
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum-config-manager --disable docker-ce-edge
sudo yum-config-manager --disable docker-ce-test
# Step 3: 更新并安装 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start
# Step 5: 更改cgroup driver
sudo bash -c ' cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF'
if [ ! $(getent group docker) ];
then
sudo groupadd docker;
else
echo "docker user group already exists"
fi
sudo gpasswd -a $USER docker
sudo systemctl daemon-reload
sudo systemctl restart docker
#rm -rf get-docker.sh
# open password auth for backup if ssh key doesn't work, bydefault, username=vagrant password=vagrant
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo systemctl restart sshd
sudo bash -c 'cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF'
# 也可以尝试国内的源 http://ljchen.net/2018/10/23/%E5%9F%BA%E4%BA%8E%E9%98%BF%E9%87%8C%E4%BA%91%E9%95%9C%E5%83%8F%E7%AB%99%E5%AE%89%E8%A3%85kubernetes/
# 禁用 SELinux
sudo setenforce 0
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
# install kubeadm, kubectl, and kubelet.
sudo yum remove -y kubelet kubeadm kubectl
sudo yum install -y kubelet-1.18.5 kubeadm-1.18.5 kubectl-1.18.5 --disableexcludes=kubernetes
sudo systemctl enable docker && sudo systemctl start docker
sudo systemctl enable kubelet && sudo systemctl start kubelet
# 将桥接的 IPv4 流量传递到 iptables 的链
sudo bash -c 'cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
EOF'
sudo sysctl --system
sudo systemctl stop firewalld
sudo systemctl disable firewalld
# 禁用 swap 分区
sudo swapoff -a
sudo sed -i '/swap/s/^/#/g' /etc/fstab
sudo systemctl enable docker.service
sudo systemctl enable kubelet.service
# sudo cat ./pull.sh
#此处用于指定k8s版本
images=(
kube-apiserver:v1.18.5
kube-controller-manager:v1.18.5
kube-scheduler:v1.18.5
kube-proxy:v1.18.5
pause:3.2
etcd:3.4.3-0
coredns:1.6.7
)
for imageName in ${images[@]};
do
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName}
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName} k8s.gcr.io/${imageName}
sudo docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName}
done
执行命令创建虚拟就
vagrant up
主节点运行
登录到master节点并执行如下命令
sudo kubeadm init --pod-network-cidr 172.100.0.0/16 --apiserver-advertise-address 192.168.10.11 --kubernetes-version 1.18.5
用户具有操作 kubectl的权限
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
添加网络插件
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
node节点
登录到另外两个节点根据master 节点执行的 kubectl apply 命令中的注释信息执行类似如下命令
sudo kubeadm join 192.168.205.120:6443 --token cghpxe.p46xwix5gfhzlxqp \
--discovery-token-ca-cert-hash sha256:f1e9a39b4afe5e69e25a682192ad103ddf9ecd0fa301cd187a4b9ea898d38cf1
修改节点IP
https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fblog.csdn.net%2Fqianghaohao%2Farticle%2Fdetails%2F98588427
root用户下执行
此处是因为vagrant创建的虚拟机第一个网卡eth0默认的用于访问互联网的,故使用eth1网卡作为k8s node之间通信,在master节点和node节点都执行如下命令
echo KUBELET_EXTRA_ARGS=\"--node-ip=`ip addr show eth1 | grep inet | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}/" | tr -d '/'`\" > /etc/sysconfig/kubelet
sudo systemctl restart kubelet
本文由 vps主机对比评测网 刊发,转载请注明出处