一、安装multipass
1. 安装multipaas软件版本 brew install multipass
2. 创建三个虚拟机master(192.168.64.6)、node1(192.168.64.14)、node2(192.168.64.15),2G cpu+2G内存+40G硬盘,Qemu+桥连模式
$ multipass find 查找可用ubuntu版本,这里选择22.04这个版本
$ multipass launch -n master -c 2 -m 2G -d 50G --network bridged 22.04
$ multipass launch -n node1 -c 2 -m 2G -d 50G --network bridged 22.04
$ multipass launch -n node2 -c 2 -m 2G -d 50G --network bridged 22.04
3. 设置网络
$ multipass networks 查找可连接网络
$ multipass set local.bridged-network=en0
$ multipass set local.master.bridged=true
$ multipass set local.node1.bridged=true
$ multipass set local.node2.bridged=true
4. 每个实例设置mount,以master举例
$ multipass shell master
>ubuntu@master: sudo snap install multipass-sshfs
$ sudo snap install multipass-sshfs
$ multipass mount /Users/xxx/Documents/work/k8sdev/mutipaas/k8s-node1 node1:/root
-------------以下操作进入每个实例执行---------------------
二、安装kubernetes
0. 加hosts,sudo vi /etc/hosts
192.168.64.6 master
192.168.64.14 node1
192.168.64.15 node2
1. 关闭swap空间 swapoff -a, 通过free -h 查看swap是否关闭。
避免开机启动交换空间, $ vi /etc/fstab 注释掉swap开头的行
2. 关闭防火墙 sudo ufw disable (sudo -s 切换到root)
3. 设置时区,dpkg-reconfigure tzdata,Asia/shanghai
安装 ntpdate,apt-get update && apt-get install ntpdate
设置系统时间与网格同步,ntpdate edu.ntp.org.cn
apt install util-linux-extra
将系统时间写入硬件时间 $ hwclock --systohc
4. 安装docker
https://cloud.tencent.com/developer/article/2076354
保证老版本docker卸载了,sudo apt-get remove docker docker-engine docker.io containerd runc
安装docker依赖, sudo apt-get install ca-certificates curl gnupg lsb-release
添加Docker官方GPG密钥, curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
添加Docker软件源,#sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo add-apt-repository "deb [arch=arm64] https://download.docker.com/linux/ubuntu xenial stable"
查看可安装的Dokcer版本,apt-cache madison docker-ce
安装docker,[最新版本] apt-get install docker-ce docker-ce-cli containerd.io
[安装1.19.03版本] apt-get install docker-ce=5:19.03.15~3-0~ubuntu-xenial docker-ce-cli=5:19.03.15~3-0~ubuntu-xenial containerd.io
需要注意,docker版本要跟k8s版本对应
安装工具,apt-get -y install apt-transport-https ca-certificates curl software-properties-common运行docker,systemctl start docker
验证是否成功,sudo docker run hello-world
如果显示 cgroup mountpoint does not exist: unknown错误,执行如下脚本(cgroup文件特别容易出问题):
#$ mkdir /sys/fs/cgroup/systemd
$ mkdir /root/cgroup/systemd
$ mount -t cgroup -o none,name=systemd cgroup /root/cgroup/systemd/
设置为开机自启动,/usr/lib/systemd/systemd-sysv-install enable docker
5. 安装k8s工具 kubeadm kubectl kubelete
#确认已安装系统工具,apt-get update && apt-get install -y apt-transport-https
安装CPG证书,curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
写入软件源,vi /etc/apt/sources.list.d/kubernetes.list 加入:deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
安装,[安装最新版本] apt-get update && apt-get install -y kubeadm kubelet kubectl
[安装指定版本] apt-get update && apt-get install -y kubelet=1.19.9-00 kubeadm=1.19.9-00 kubectl=1.19.9-00 (具体版本可以查https://mirrors.aliyun.com/kubernetes/apt/)
[卸载] apt-get remove -y kubelet kubeadm kubectl
修改cloud.cfg,vi /etc/cloud/cloud.cfg,
# 修改成 true
preserve_hostname: true
6. 安装kubernetes
1) 执行脚本如下: /root/kubernetes/cluster
$ cd /root
$ mkdir kubernetes
$ cd kubernetes
$ mkdir cluster
$ cd cluster
$ kubeadm config print init-defaults --kubeconfig ClusterConfiguration > kubeadm.yml
(有个WARNNING提示,忽略)
$ vi kubeadm.yml,修改如下地方:
修改主机ip advertiseAddress: 192.168.64.6
修改镜像地址(k8s.gcr.io 为Google镜像地址) imageRepository:registry.aliyuncs.com/google_containers
注意: kubernetesVersion 版本
networking:
serviceSubnet:10.96.0.0/12
podSubnet: 10.224.0.0/24
2) 查看需要下载哪些镜像, kubeadm config images list --config kubeadm.yml
拉取镜像, kubeadm config images pull --config kubeadm.yml (忽略WARNNING报错)
查看镜像,docker images
3) 安装主节点(node节点不走此步骤):
kubeadm init --config=kubeadm.yml --upload-certs | tee kubeadm-init.log
如果执行失败,journalctl -xeu kubelet | grep error 查看kubelete启动失败原因, 或者 systemctl status kubelet 查看kubelete状态。
如果显示 cgroup mountpoint does not exist: unknown错误,执行如下脚本:
#$ mkdir /sys/fs/cgroup/systemd
$ mkdir /root/cgroup/systemd
$ mount -t cgroup -o none,name=systemd cgroup /root/cgroup/systemd/
如果执行失败后,重新执行,会显示prelight error, port in use,这个时候,kubeadm reset --force 后,再次执行 kubeadm init --config=kubeadm.yml --upload-certs | tee kubeadm-init.log
/etc/docker/daemon.json,将dockercgroup驱动程序设置为systemd,此时daemon.json如下:
{ "registry-mirrors": ["https://c8lfvm3n.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"] }
重新加载daemon.json,重启docker和kubelet:
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
$ sudo systemctl restart kubelet
kubeadm init失败了很多次,主要是卡在kubelet启动,来回systemctl status kubelet 查看报错,逐个解决后,kubeadm reset --force 之后再kubeadm init --config=kubeadm.yaml --uploadcerts.
一定要注意,kubeadm reset之后,需要执行手动清理脚本:
$ apt install ipvsadm iptables
$ ipvsadm --clear
$ rm -rf /etc/cni/net.d
# $ iptables -X
$ rm -f $HOME/.kube/config
kubeadm init 成功标志如下:
准备kubeconfig,执行如下脚本:
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ export KUBECONFIG=/etc/kubernetes/admin.conf
设置为开机自启动,systemctl enable kubelet && systemctl start kubelet
安装flannl:
$ sysctl net.bridge.bridge-nf-call-iptables=1
$ wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
$ kubeadm config view | grep podSubnet 查询--pod-network-cidr, 或者kubectl cluster-info dump | grep -m 1 cluster-cidr
$ vi kube-flannel.yml, 修改
net-conf.json: |
{
"Network": "10.244.0.0/24”,
"EnableNFTables": false,
"Backend": {
"Type": "vxlan"
}
}
$ kubectl create -f kube-flannel.yml
4) 安装node节点
/etc/docker/daemon.json,将dockercgroup驱动程序设置为systemd,此时daemon.json如下:
{ "registry-mirrors": ["https://c8lfvm3n.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"] }
重新加载daemon.json,重启docker和kubelet:
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
$ sudo systemctl restart kubelet
加入k8s集群,
kubeadm join 192.168.64.6:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:8fadf05a2dcf92a5e042ce0714788e486b82ab14d5fe64e46d80948fae352b3d
到master节点修改node1的podCird,kubectl patch node node1 -p '{"spec":{"podCIDR":"10.224.0.0/24"}}'
设置为开机自启动,systemctl enable kubelet && systemctl start kubelet
标签:kubernetes,--,sudo,apt,multipass,docker,kubeadm,亲手 From: https://www.cnblogs.com/yennie/p/18613212