之前在旧博客上写过但还没写完,不过当时是在VMware上搭建的,前段时间我在闲置的神船上面搭建了一个Proxmox VE,就趁这个机会在PVE上重新搭建一遍,只不过用的是KVM。
虚拟机配置:
设置hostname:
hostnamectl set-hostname k8s-master
为了方便国内环境的配置,先把apt的源换为国内镜像,我这里用的是阿里的,直接把/etc/apt/sources.list文件替换成如下内容:
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
update之后,添加阿里的docker源并安装:
sudo apt-get update sudo apt-get install -y apt-transport-https curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" sudo apt-get install docker-ce=5:18.09.0~3-0~ubuntu-bionic
在/etc/apt/sources.list.d目录下创建一个kubernetes.list文件,内容如下:
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
安装k8s:
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add - sudo apt-get install kubelet=1.15.0-00 kubeadm=1.15.0-00 kubectl=1.15.0-00
用free查看内存状态,可以看到有一行swap,因此需要编辑/etc/fstab,把swap所在的这一行注释掉,关闭swap。
克隆两个worker虚拟机,分别设置hostname为k8s-worker1和k8s-worker2。
初始化k8s:
sudo kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.15.0
为了方便在非root用户下使用,可以在$HOME下创建一个.kube目录:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
安装flannel:
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/v0.12.0/Documentation/kube-flannel.yml
在master初始化k8s时,输出结果的最下面给出了一个带token的join命令,复制下来,在两个worker虚拟机中分别执行:
在master中可以看到两个worker: