阅读量:0
在三台机器上部署 Kubernetes 集群
- 1.环境准备
- 2.在所有节点上进行以下步骤
- 3.在 Master 节点 (172.16.103.206) 上进行以下步骤
- 4.在 Node 节点 (172.16.103.196 和 172.16.103.197) 上进行以下步骤
- 5.验证集群状态
- 6.部署应用
- 7.检查服务和 Pod 的状态:
- 8.访问nginx
1.环境准备
部署Dokcer查看:
链接: https://blog.csdn.net/qq_40914472/article/details/140693322
Master 节点:172.16.103.206(已安装 Docker)
Node节点:172.16.103.196 和 172.16.103.197 (已安装 Docker)
2.在所有节点上进行以下步骤
1. 更新系统和安装必要的软件包
sudo yum update -y sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2. 禁用交换分区
sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
3. 禁用防火墙和SElinux
sudo systemctl stop firewalld sudo systemctl disable firewalld sudo sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
4.系统主机名
master节点: hostnamectl set-hostname master node节点: hostnamectl set-hostname node1 hostnamectl set-hostname node2
5.设置主机名与IP地址解析
[root@master ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.103.206 master 172.16.103.197 node1 172.16.103.196 node2
6.配置内核转发及网桥过滤
cat >/etc/sysctl.d/k8s.conf <<EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 vm.swappiness = 0 EOF sysctl --system
7. 配置 Docker Cgroup 驱动
[root@master ~]# cat /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": [ "https://docker.registry.cyou", "https://docker-cf.registry.cyou", "https://dockercf.jsdelivr.fyi", "https://docker.jsdelivr.fyi", "https://dockertest.jsdelivr.fyi", "https://mirror.aliyuncs.com", "https://dockerproxy.com", "https://mirror.baidubce.com", "https://docker.m.daocloud.io", "https://docker.nju.edu.cn", "https://docker.mirrors.sjtug.sjtu.edu.cn", "https://docker.mirrors.ustc.edu.cn", "https://mirror.iscas.ac.cn", "https://dockerhub.icu", "https://docker.rainbond.cc" ], "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" }, "bip":"172.12.0.1/24", "storage-driver": "overlay2" }
8. 添加 Kubernetes 仓库并安装 kubeadm、kubelet 和 kubectl
cat << EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
9. 安装kubeadm、kubelet、kubectl
yum install -y --nogpgcheck kubelet-1.23.12 kubeadm-1.23.12 kubectl-1.23.12
10.设置开机启动
systemctl daemon-reload systemctl restart docker.service --now systemctl restart kubelet.service --now
3.在 Master 节点 (172.16.103.206) 上进行以下步骤
1. 初始化 Kubernetes 集群
kubeadm config images pull --kubernetes-version=1.23.12 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=1.23.12 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
成功初始化后,输出会显示一个 kubeadm join 命令,这个命令稍后会用来将 Worker 节点加入集群。
2. 配置 kubectl
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
3. 安装网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
4. 生成kubeadm join
[root@master ~]# kubeadm token create --print-join-command kubeadm join 172.16.103.206:6443 --token cufhmh.sc1l5q2va9ry4zx2 --discovery-token-ca-cert-hash sha256:e1d8b794e5a1ab7b9425211d405698916e3196477cb0163c4977a4bd667d625e
4.在 Node 节点 (172.16.103.196 和 172.16.103.197) 上进行以下步骤
1. 使用 kubeadm join 命令加入集群
使用在 Master 节点初始化集群后显示的 kubeadm join 命令将Node 节点加入集群。例如:
kubeadm join 172.16.103.206:6443 --token cufhmh.sc1l5q2va9ry4zx2 --discovery-token-ca-cert-hash sha256:e1d8b794e5a1ab7b9425211d405698916e3196477cb0163c4977a4bd667d625e kubectl get nodes
5.验证集群状态
在 Master 节点上,运行以下命令验证所有节点是否已成功加入集群并处于就绪状态:
[root@master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready control-plane,master 43m v1.23.12 node1 Ready <none> 40m v1.23.12 node2 Ready <none> 40m v1.23.12
如果所有节点状态为 Ready,说明集群配置成功。
6.部署应用
现在,你可以开始在 Kubernetes 集群中部署应用。例如,创建一个简单的 Nginx 部署:
kubectl create deployment nginx --image=nginx kubectl expose deployment nginx --port=80 --type=NodePort
7.检查服务和 Pod 的状态:
[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-85b98978db-ntg72 0/1 ContainerCreating 0 25s [root@master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 44m nginx NodePort 10.100.52.141 <none> 80:32516/TCP 12s
8.访问nginx
http://172.16.103.206:32516/