k8s部署
准备:3台node服务器IP分别为(192.168.2.18-192.168.2.20),1台master服务器IP为192.168.2.21,并安装好docker。
步骤:
配置docker使用systemd作为默认驱动
- 安装docker
yum install yum-utils -y
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
- 修改主机名
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2
hostnamectl set-hostname node3
- 配置docker使用systemd作为默认驱动
cat <<EOF> /etc/docker/daemon.json
{
"exec-opts":["native.cgroupdriver=systemd"]
}
EOF
#设置开机自启动
systemctl enable docker
#重启docker
systemctl restart docker
#查看驱动
docker info | grep Cgroup
- 关闭swap分区
#临时关闭所有swap分区
swapoff -a
#永久关闭swap分区,注释/ext/fstab/的/dev/mapper/centos-swap
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
#查看是否注释
cat /etc/fstab
- 修改host文件
cat >>/etc/hosts <<EOF
192.168.2.21 master
192.168.2.18 node1
192.168.2.19 node2
192.168.2.20 node3
EOF
- 配置Kubernetes的yum仓库
cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
- 安装kubelet,kubeadm,kubectl
yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
systemctl enable kubelet
- 部署Kubernetes Master
#拉取coredns镜像并改名
docker pull coredns/coredns
docker tag coredns/coredns registry.aliyuncs.com/google_containers/coredns
kubeadm init \
--apiserver-advertise-address 192.168.2.21 \
--image-repository registry.aliyuncs.com/google_containers \
--pod-network-cidr=10.244.0.0/16 \
--service-cidr=10.1.0.0/16
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.2.21:6443 --token 4dfgko.5lxgvyzrrqe4nocg \
--discovery-token-ca-cert-hash sha256:9465be0cc03d900c6ef26ab8271c428b9ffc42939e12114e0da7a8703ee457d5
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
- node节点加入集群
建议不要在node节点运行kublet服务,不然会导致join失败,如果已经运行,建议执行kubeadm reset然后删除提示的文件和目录。
所有node运行命令:
kubeadm join 192.168.2.21:6443 --token 4dfgko.5lxgvyzrrqe4nocg \
--discovery-token-ca-cert-hash sha256:9465be0cc03d900c6ef26ab8271c428b9ffc42939e12114e0da7a8703ee457d5
默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,可以直接使用命令快捷生成:
kubeadm token create --print-join-command
- 安装网络插件flannel
kubectl apply -f kube-flannel.yml
查看nodes信息
```shell
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 122m v1.23.6
node1 Ready <none> 107m v1.23.6
node2 Ready <none> 100m v1.23.6
node3 Ready <none> 2m40s v1.23.6