当前位置:   article > 正文

快速部署k8s

快速部署k8s

一、前提

 服务器规划:
角色IP地址
master192.168.140.128
node1192.168.140.129
node2192.168.140.131

服务器要求:
  • 建议最小硬件配置: 2核CPU、2G内存、20G硬盘。
  • 服务器可以访问互联网,能够联网下载镜像。
  • 时间进行同步!!!
软件环境;
软件版本
操作系统CentOS 7
Docker26.0.1(CE)
Kubernetes1.28

二、初始化设置

  1. ##安装ntp
  2. yum -y install chrony
  3. systemctl enable chronyd --now && chronyc sources
  4. ##设置主机名称解析
  5. tee -a /etc/hosts << EOF
  6. 192.168.140.128 master
  7. 192.168.140.129 node1
  8. 192.168.140.130 node2
  9. EOF
  10. ##关闭selinux
  11. sed -i 's/enforcing/disbaled/' /etc/selinux/config #永久
  12. setenforce 0 #临时
  13. ##关闭防火墙
  14. systemctl disable firewalld --now
  15. ##关闭Swap
  16. swapoff -a #临时
  17. sed -ri 's/.*swap.*/#&/' /etc/fstab #永久
  18. ##根据规划设置主机名
  19. hostnamectl set-hostname <hostname>
  20. bash #生效
  21. ##确保网络桥接的数据包经过Iptables处理,启用相关内核参数
  22. cat > /etc/sysctl.d/k8s.conf << EOF
  23. net.bridge.bridge-nf-call-ip6tables = 1
  24. net.bridge.bridge-nf-call-iptables = 1
  25. net.ipv4.ip_forward=1
  26. vm.swappiness=0
  27. EOF
  28. sysctl --system #生效
  29. ##加载br_betfilter模块
  30. cat <<EOF | tee /etc/modules-load.d/k8s.conf
  31. overlay
  32. br_netfilter
  33. nf_conntrack
  34. EOF
  35. ##加载ipvs网络转发模块
  36. yum -y install ipset ipvsadm
  37. cat <<EOF | tee /etc/modules-load.d/ipvs.conf
  38. ip_vs
  39. ip_vs_rr
  40. ip_vs_wrr
  41. ip_vs_sh
  42. EOF
  43. ##重启服务器,使配置生效
  44. reboot

三、安装Docker

安装Docker: 
  1. wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
  2. yum install -y docker-ce
  3. systemctl enable docker && systemctl start docker
测试验证 

配置镜像下载加速器和设置Cgroup驱动(systemd)
  1. cat > /etc/docker/daemon.json << EOF
  2. {
  3. "registry-mirrors": ["https://https://8d6xmv42.mirror.aliyuncs.com"],
  4. "exec-opts": ["native.cgroupdriver=systemd"]
  5. }
  6. EOF
  7. systemctl restart docker
  8. docker --version

注意:镜像加速器地址链接每个人的不太,注意甄别。可登录容器镜像服务控制台,在左侧导航栏选择镜像工具 > 镜像加速器,在镜像加速器页面获取加速器地址

安装cri-dockerd (Docker与Kubernetes通信的中间程序)---1.23版本之后需要安装
  1. wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.12/cri-dockerd-0.3.12-3.el7.x86_64.rpm
  2. rpm -ivh cri-dockerd-0.3.12-3.el7.x86_64.rpm
指定依赖镜像地址为国内镜像地址:
  1. ##修改cri-docker.service
  2. ~]# vim /usr/lib/systemd/system/cri-docker.service
  3. ExecStart=/usr/bin/cri-dockerd --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9 --container-runtime-endpoint fd://
  1. systemctl daemon-reload
  2. systemctl enable cri-docker && systemctl start cri-docker

 四、部署Kubernetes集群

添加yum源
  1. cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
  2. [kubernetes]
  3. name=Kubernetes
  4. baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
  5. enabled=1
  6. gpgcheck=0
  7. gpgkey=https://mirrors.aliyun.com/kubernetesnew/core/stable/v1.28/rpm/repodata/repomd.xml.key
  8. EOF
 安装kubeadm,kubelet和kubectl
  1. ## master节点
  2. yum install -y kubelet kubeadm kubectl
  3. ## node节点
  4. yum install -y kubelet kubeadm
  5. ## all
  6. systemctl enable kubelet
 配置 cgroup 驱动与docker一致
  1. cp /etc/sysconfig/kubelet{,.bak}
  2. cat > /etc/sysconfig/kubelet <<EOF
  3. KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
  4. EOF
  5. systemctl enable kubelet --now
初始化master节点
  1. ##生成默认配置文件
  2. ~]# kubeadm config print init-defaults > k8s_init.yaml
  3. ~]# cat /root/k8s_init.yaml
  4. apiVersion: kubeadm.k8s.io/v1beta3
  5. bootstrapTokens:
  6. - groups:
  7. - system:bootstrappers:kubeadm:default-node-token
  8. token: abcdef.0123456789abcdef
  9. ttl: 24h0m0s
  10. usages:
  11. - signing
  12. - authentication
  13. kind: InitConfiguration
  14. localAPIEndpoint:
  15. advertiseAddress: 192.168.140.128 ##设置为master地址
  16. bindPort: 6443
  17. nodeRegistration:
  18. criSocket: unix:///var/run/cri-dockerd.sock
  19. imagePullPolicy: IfNotPresent
  20. name: master
  21. taints: null
  22. ---
  23. apiServer:
  24. timeoutForControlPlane: 4m0s
  25. apiVersion: kubeadm.k8s.io/v1beta3
  26. certificatesDir: /etc/kubernetes/pki
  27. clusterName: k8s
  28. controllerManager: {}
  29. dns: {}
  30. etcd:
  31. local:
  32. dataDir: /var/lib/etcd
  33. imageRepository: registry.aliyuncs.com/google_containers #使用国内镜像
  34. kind: ClusterConfiguration
  35. kubernetesVersion: 1.29.0
  36. networking:
  37. dnsDomain: k8s.local
  38. serviceSubnet: 10.96.0.0/12
  39. podSubnet: 10.244.0.0/16 #网络插件flannel
  40. scheduler: {}
kubeadm init --config k8s_init.yaml

初始化完成后,根据提示信息,拷贝kubectl工具认证文件到默认路径

  1. ~]# mkdir -p $HOME/.kube
  2. ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config

 如果需要重置

kubeadm reset  --cri-socket unix:///var/run/cri-dockerd.sock

使用kubectl工具查看节点状态

~]# kubectl get nodes

因为网络插件还没有部署,所以节点处于“NotReady”状态

将node节点加入集群(需指定连接docker的中间程序的socket)

  1. ## node节点执行
  2. ~]# kubeadm join 192.168.140.128:6443 --token bagja3.vvjfveyyms1cywhn --discovery-token-ca-cert-hash sha256:1a93ded43dff7134dc05a3f327c2cd3a82d6102da066b1a28c167e0f7758daf3 --cri-socket=unix:///var/run/cri-dockerd.sock

安装网络插件fannel(master节点)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
运行完成,等待几分钟,查看node运行状态

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/729293
推荐阅读
相关标签
  

闽ICP备14008679号