美文网首页
Centos7.9 部署搭建k8s集群

Centos7.9 部署搭建k8s集群

作者: e02f7fdbb400 | 来源:发表于2022-09-15 16:24 被阅读0次

    部署环境说明

    • 两台centos7.9

    一、环境准备

    两台机器同时执行

    # 关闭防火墙
    systemctl stop firewalld
    systemctl disable firewalld
    
    # 关闭selinux
    sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
    setenforce 0  # 临时
    
    # 关闭swap
    swapoff -a  # 临时
    sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久
    
    # 在master添加hosts
    cat >> /etc/hosts << EOF
    192.168.1.11 k8smaster
    192.168.1.12 k8snode1
    EOF
    
    # 将桥接的IPv4流量传递到iptables的链
    cat > /etc/sysctl.d/k8s.conf << EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    sysctl --system  # 生效
    
    # 时间同步
    yum install ntpdate -y
    ntpdate time.windows.com
    

    两台机器分别执行

    # 根据规划设置主机名
    hostnamectl set-hostname <hostname>
    

    二、安装docker

    $ wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
    $ yum -y install docker-ce-18.06.1.ce-3.el7
    $ systemctl enable docker && systemctl start docker
    $ docker --version
    Docker version 18.06.1-ce, build e68fc7a
    

    三、安装kubeadm,kubelet和kubectl

    公网参考

    $ 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=0
    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
    
    $ yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
    $ systemctl enable kubelet
    

    内网可参考

    https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/Packages/?spm=a2c6h.25603864.0.0.38a8368fXNT5LI
    

    下载如下包

    image.png
    因内网源安装报错7/kubernetes/packages-kubectl-1.18.0-0.x86_64.rpm: header v4 rsa/sha512 signature, key id 3e1ba8d5: nokey不得不出此下策
    下载后安装(部分依赖内网源已经安装了)
    yum localinstall -y *.rpm

    四、部署Kubernetes Master

    公网参考

    $ kubeadm init \
      --apiserver-advertise-address=192.168.1.11 \
      --image-repository registry.aliyuncs.com/google_containers \
      --kubernetes-version v1.18.0 \
      --service-cidr=10.96.0.0/12 \
      --pod-network-cidr=10.244.0.0/16
    

    无法访问registry.aliyuncs.com可以参考如下方法。

    # kubeadm config images list
    k8s.gcr.io/kube-apiserver:v1.18.0
    k8s.gcr.io/kube-controller-manager:v1.18.0
    k8s.gcr.io/kube-scheduler:v1.18.0
    k8s.gcr.io/kube-proxy:v1.18.0
    k8s.gcr.io/pause:3.2
    k8s.gcr.io/etcd:3.4.3-0
    k8s.gcr.io/coredns:1.6.7
    

    sh批量下载导入镜像

    编写脚本,从阿里云下载镜像

    # cat pull-images.sh 
    #!/bin/bash
    images=(
        kube-apiserver:v1.18.0
        kube-controller-manager:v1.18.0
        kube-scheduler:v1.18.0
        kube-proxy:v1.18.0
        pause:3.2
        etcd:3.4.3-0
        coredns:1.6.7
    )
    for imageName in ${images[@]};
    do
        docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName}
        docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName} k8s.gcr.io/${imageName}
        docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName}
    done
    

    查看下载镜像

    # docker images
    REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
    k8s.gcr.io/kube-proxy                v1.18.0             43940c34f24f        7 days ago          117MB
    k8s.gcr.io/kube-apiserver            v1.18.0             74060cea7f70        7 days ago          173MB
    k8s.gcr.io/kube-controller-manager   v1.18.0             d3e55153f52f        7 days ago          162MB
    k8s.gcr.io/kube-scheduler            v1.18.0             a31f78c7c8ce        7 days ago          95.3MB
    k8s.gcr.io/pause                     3.2                 80d28bedfe5d        6 weeks ago         683kB
    k8s.gcr.io/coredns                   1.6.7               67da37a9a360        2 months ago        43.8MB
    k8s.gcr.io/etcd                      3.4.3-0             303ce5db0e90        5 months ago        288MB
    

    打包镜像

    编写脚本打包镜像

    # cat save-images.sh 
    #!/bin/bash
    images=(
        kube-apiserver:v1.18.0
        kube-controller-manager:v1.18.0
        kube-scheduler:v1.18.0
        kube-proxy:v1.18.0
        pause:3.2
        etcd:3.4.3-0
        coredns:1.6.7
    )
    for imageName in ${images[@]};
    do
        docker save -o `echo ${imageName}|awk -F ':' '{print $1}'`.tar k8s.gcr.io/${imageName}
    done
    

    # tar czvf kubeadm-images-1.18.0.tar.gz *.tar

    导入镜像
    在安装节点分别导入离线镜像或者放入私有仓库使用

    # cat load-image.sh 
    #!/bin/bash
    ls /root/kubeadm-images-1.18.0 > /root/images-list.txt
    cd /root/kubeadm-images-1.18.0
    for i in $(cat /root/images-list.txt)
    do
         docker load -i $i
    done
    

    导入镜像
    # ./load-image.sh

    五、kubeadm init

    kubeadm init \
      --apiserver-advertise-address=192.168.117.58 \
      --kubernetes-version v1.18.0 \
      --service-cidr=10.96.0.0/12 \
      --pod-network-cidr=10.244.0.0/16 \
      --ignore-preflight-errors=Swap
    

    成功后执行

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    $ kubectl get nodes
    

    在节点上执行提示的命令kubeadm join xxxxxx

    节点执行k8s命令需要拷贝设置配置文件

    image.png

    引用参考
    https://www.i4k.xyz/article/curry10086/107579113
    https://www.cnblogs.com/lei0213/p/15521526.html
    https://blog.51cto.com/hatech/2484192

    相关文章

      网友评论

          本文标题:Centos7.9 部署搭建k8s集群

          本文链接:https://www.haomeiwen.com/subject/iwyrortx.html