1. 关闭swap、防火墙
# vim /etc/fstab,注释包含swap行并重启 # systemctl stop ufw && systemctl disable ufw
2. 配置阿里源
# echo "deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
# apt update
如果遇到以下问题: 记录提示的PUBKEY的最后8位,这里是BA07F4FB,然后执行:
# g#pg --keyserver keyserver.ubuntu.com --recv-keys BA07F4FB
# gpg --export --armor BA07F4FB | sudo apt-key add -
# apt update
3. 安装组件
# sudo apt install -y kubelet kubeadm kubectl
# apt-mark hold kubelet kubeadm kubectl
4. 安装docker
配置阿里源/etc/apt/sources.list,写入以下内容
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb 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 main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
# apt install docker.io -y
5. 配置kubelet的cgroup drive
需要确保kubelet的cgroup drive在docker的一致。 分别可以通过以下命令查看:
docker info | grep -i cgroup
cat /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
若显示不一样,则添加或修改Environment="KUBELET_CGROUP_ARGS=--cgroup-dirver=cgroupfs"
:
systemctl daemon-reload
systemctl restart kubelet
6. 启动kubelet
systemctl enable kubelet && systemctl start kubelet
7. 下载K8S的Docker镜像
使用kubeadm config images list
显示需要下载的镜像 k8s.gcr.io/kube-apiserver:v1.18.12 k8s.gcr.io/kube-controller-manager:v1.18.12 k8s.gcr.io/kube-scheduler:v1.18.12 k8s.gcr.io/kube-proxy:v1.18.12 k8s.gcr.io/pause:3.2 k8s.gcr.io/etcd:3.4.3-0 k8s.gcr.io/coredns:1.6.7 然后替换镜像源为registry.cn-hangzhou.aliyuncs.com/google_containers拉取镜像
# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.12
# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.12
# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.12
# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.12
# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7
然后修改tag 至此基础的组件安装完成,接下来要根据是master节点还是普通node节点来分别配置
8. master节点配置
# kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.18.12 --pod-network-cidr=10.0.0.0/24 --apiserver-advertise-address=192.168.10.105
注意: --image-repository 设置镜像源
--pod-network-cidr=10.0.0.0/24表示集群将使用网络的子网范围
--kubernetes-version=v1.18.12指定K8S版本
--apiserver-advertise-address表示绑定的网卡IP,这里是master所在节点
若执行kubeadm init出错或强制终止,则再需要执行该命令时,需要先执行kubeadm reset重置
执行成功后,它提示还需要执行:
# mkdir -p $HOME/.kube
# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# sudo chown $(id -u):$(id -g) $HOME/.kube/config
此外最后还会提示其他节点加入集群时的命令:
# kubeadm join 192.168.10.10:6443 --token xrlg63.d1h8o7an82qevjv6
--discovery-token-ca-cert-hash sha256:6085f8dc1d0e19f531e2a2feda136bd9aef28c840e62da636acc1b2ffb83a51d
不过需要注意的是这里的token值24小时后会失效,因此,当以后还想加入节点时,可以先用kubeadm token list
命令查看当前的token表,并在master节点上执行openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
来查看hash 此时我们查看网络状态kubectl get pod -n kube-system
,可以发现都处于Pending阻塞状态,此时,我们需要配置网络,直接使用Calico,即执行:
# kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
# kubectl apply -f https://docs.projectcalico.org/v3.8/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
(注意这里也是有版本号的,如果不是很清楚是多少,可以直接访问官网获取最新的进行尝试:https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/) 过段时间,再执行kubectl get pod -n kube-system
就会发现都处于running状态了。 最后将master设为工作节点:
# kubectl taint nodes --all node-role.kubernetes.io/master-
执行成功时会显示
node/xl-virtualbox untainted
对于mater至此配置成功,可以使用kubectl get nodes
命令查看节点状态。当然目前只有一个master节点。
9. node节点
对于普通的node节点,只需执行:
# kubeadm join 10.0.2.15:6443 --token zuhiop.bmxq2jofv1j68o9o
--discovery-token-ca-cert-hash sha256:b65ca09d1f18ef0af3ded2c831c609dfe48b19c5dc53a8398af5b735603828fb
如果此时在master节点上使用kubectl get nodes
查看节点的状态时'NotReady',请在对应主机上重启docker服务即可:
# systemctl restart docker