栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

k8s 1.21.12 安装教程

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

k8s 1.21.12 安装教程

  1. 网络

    1. 负载均衡

      1. 二层负载均衡:基于 MAC 地址的二层负载均衡。

      2. 三层负载均衡:基于 IP 地址的负载均衡。

      3. 四层负载均衡:基于 IP+端口 的负载均衡。

      4. 七层负载均衡:基于 URL 等应用层信息的负载均衡。

      5. 这里用一张图来说说四层和七层负载均衡的区别:

  2. 搭建

    1. 环境

      1. 主机IPdocker版本k8s版本备注
        master1172.16.4.12420.10.71.21.12
        node1172.16.4.12520.10.71.21.12
        node2172.16.4.12620.10.71.21.12
    2. #修改主机名
      hostnamectl set-hostname master1
      hostnamectl set-hostname node1
      hostnamectl set-hostname node2
      #修改hosts文件
      cat >> /etc/hosts << EOF
      172.16.4.124   master1
      172.16.4.125   node1 
      172.16.4.126   node2
      EOF
      
      #备份系统源
      mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
      #下载aliyun源
      wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
      yum clean all
      yum makecache
      #配置时间服务器,同步时间
      #安装ntp服务
      yum install ntp -y
      #查看时间
      clock
      #手动同步时间
      ntpdate ntp.aliyun.com && hwclock -w
      
      # 关闭防火墙
      systemctl disable firewalld
      systemctl stop firewalld
      # 关闭selinux
      # 临时禁用selinux
      setenforce 0
      # 永久关闭 修改/etc/sysconfig/selinux文件设置
      sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
      sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
      # 禁用交换分区
      swapoff -a
      # 永久禁用,打开/etc/fstab注释掉swap那一行。
      sed -i 's/.*swap.*/#&/' /etc/fstab
      #开启网络转发
      cat << EOF | tee /etc/sysctl.conf
      net.ipv4.ip_forward=1
      EOF
      echo 1 > /proc/sys/net/ipv4/ip_forward
      systemctl restart network
      
      # 修改内核参数
      cat <  /etc/sysctl.d/k8s.conf
      net.bridge.bridge-nf-call-ip6tables = 1
      net.bridge.bridge-nf-call-iptables = 1
      EOF
      sysctl --system
      
      # 添加aliyun kubernetes源 需要改参数repo_gpgcheck=0
      cat < /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=0
      gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
      EOF
      
      #添加清华镜像源
      cat < 
    3. 因为k8s这些镜像都是国外的,直接访问下载镜像可能会有问题。

      编写拉取镜像脚本,从阿里云镜像仓库下载所需版本。

      #!/bin/bash
      url=registry.cn-hangzhou.aliyuncs.com/google_containers
      # 安装指定的kubectl版本
      version=v1.21.12
      # 上面查出来的coredns版本号
      coredns=v1.8.0
      images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`)
      for imagename in ${images[@]} ; do
         if [ $imagename = "coredns" ]
         then
            docker pull $url/coredns:$coredns
            docker tag $url/coredns:$coredns k8s.gcr.io/coredns/coredns:v1.8.0
            docker rmi -f $url/coredns:$coredns
         else
            docker pull $url/$imagename
            docker tag $url/$imagename k8s.gcr.io/$imagename
            docker rmi -f $url/$imagename
        fi
      done
      #赋予权限脚本
      chmod +x images.sh
      #运行权限
      ./images.sh
      #镜像列表
      docker images
      #初始化k8s集群
          #–kubernetes-version # 指定的版本
          #–apiserver-advertise-address # K8S主节点的地址
          #–pod-network-cidr # pod的网络IP范围
      kubeadm init --kubernetes-version=1.21.12 --apiserver-advertise-address=172.16.4.124 --pod-network-cidr=10.244.0.0/16
      #指定镜像仓库
      kubeadm init --apiserver-advertise-address=172.16.4.124 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
      
      #执行k8s集群初始化给你生成的创建目录和复制配置文件命令。
      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
      
      #root 执行快捷方式
      cat << EOF | tee /etc/profile.d/k8s_profile
      export KUBECONFIG=/etc/kubernetes/admin.conf
      EOF
      
      # master 查看节点检查token是否有效
      kubeadm token list
      # 生成新的token和命令。然后在node重新执行
      kubeadm token create --print-join-command
      
      #查看节点状态
      kubectl get nodes
      #下载flannel.yml 文件
      wget https://github.com/flannel-io/flannel/blob/v0.17.0/Documentation/kube-flannel.yml
      #安装flanel网络插件(在master上执行)
      kubectl apply -f https://github.com/flannel-io/flannel/blob/v0.17.0/Documentation/kube-flannel.yml
      #安装Calico网络插件
      curl https://docs.projectcalico.org/manifests/calico.yaml -O
      kubectl apply -f calico.yaml
      kubectl get pods -o wide -n kube-system
      
      #安装 rancher
      docker run -itd -p 4480:80 -p 4443:443 
          --restart=unless-stopped 
          -e CATTLE_AGENT_IMAGE="registry.cn-hangzhou.aliyuncs.com/rancher/rancher-agent:v2.4.2" 
          --name rancher 
          registry.cn-hangzhou.aliyuncs.com/rancher/rancher:v2.4.2
          
      #修改以上两个组件的静态pod的配置文件
      #两个文件,删除 --port=0
      /etc/kubernetes/manifests/kube-controller-manager.yaml
      /etc/kubernetes/manifests/kube-scheduler.yaml
      service kubelet restart
      
      
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942021.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号