部署k8s 1.22.2 集群 && Euler部署k8s 1.22.2 集群

部署k8s 1.22.2 集群

Euler部署k8s 1.22.2 集群

一、基础环境

主机名 IP地址 角色 系统
master 192.168.10.20 master Euler-21.03
node1 192.168.10.21 node1 Euler-21.03
node2 192.168.10.22 node2 Euler-21.03

二、配置基础环境。

• 以下命令在三台主机上均需运行

1、添加yum源

vim /etc/yum.repos.d/docker-ce.repo

[docker-ce-stable]
name=Docker CE Stable - x86_64
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/x86_64/stable
enabled=1
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo x86_64
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-x86_64/stable
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/stable
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - x86_64
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/x86_64/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo x86_64
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-x86_64/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

[docker-ce-nightly]
name=Docker CE Nightly - x86_64
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/x86_64/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo x86_64
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/debug-x86_64/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/8/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg

2、安装相关依赖包

yum install -y vim bash-completion lrzsz conntrack ipvsadm ipset jq sysstat curl iptables

3、关闭防火墙

systemctl stop firewalld && systemctl disable firewalld
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT

4、关闭SELinux

setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

5、关闭sawp分区

swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

6、加载内核模块

cat > /etc/sysconfig/modules/ipvs.modules <
  • modprobe ip_vs lvs基于4层的负载均很
  • modprobe ip_vs_rr 轮询
  • modprobe ip_vs_wrr 加权轮询
  • modprobe ip_vs_sh 源地址散列调度算法
  • modprobe nf_conntrack_ipv4 连接跟踪模块
  • modprobe br_netfilter 遍历桥的数据包由iptables进行处理以进行过滤和端口转发

7、设置内核参数

cat << EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
sysctl -p /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
sysctl: cannot stat /proc/sys/net/ipv4/tcp_tw_recycle: No such file or directory
vm.swappiness = 0
vm.overcommit_memory = 1
vm.panic_on_oom = 0
fs.inotify.max_user_watches = 89100
fs.file-max = 52706963
fs.nr_open = 52706963
net.ipv6.conf.all.disable_ipv6 = 1
net.netfilter.nf_conntrack_max = 2310720
  • overcommit_memory是一个内核对内存分配的一种策略,取值又三种分别为0, 1, 2

  • overcommit_memory=0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。

  • overcommit_memory=1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。

  • overcommit_memory=2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

  • net.bridge.bridge-nf-call-iptables 设置网桥iptables网络过滤通告

  • net.ipv4.tcp_tw_recycle 设置 IP_TW 回收

  • vm.swappiness 禁用swap

  • vm.panic_on_oom 设置系统oom(内存溢出)

  • fs.inotify.max_user_watches 允许用户最大监控目录数

  • fs.file-max 允许系统打开的最大文件数

  • fs.nr_open 允许单个进程打开的最大文件数

  • net.ipv6.conf.all.disable_ipv6 禁用ipv6

  • net.netfilter.nf_conntrack_max 系统的最大连接数

8、安装 Docker

8.1首先卸载旧版

yum remove  docker \
            docker-client \
            docker-client-latest \
            docker-common \
            docker-latest \
            docker-latest-logrotate \
            docker-logrotate \
            docker-selinux \
            docker-engine-selinux \
            docker-engine

8.2安装依赖包

yum install -y yum-utils device-mapper-persistent-data lvm2

8.3安装

yum makecache fast
yum list docker-ce --showduplicates | sort -r
yum -y install docker-ce-3:20.10.8-3.el8.x86_64

8.3设置docker开机自启

systemctl enable docker

8.4启动docker

systemctl start docker

8.5修改docker镜像源和daemon.json

sed -i "13i ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT" /usr/lib/systemd/system/docker.service
tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": ["https://v0rjmu9s.mirror.aliyuncs.com"],
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
    "max-size": "100m"
},
    "storage-driver": "overlay2",
    "storage-opts": [
        "overlay2.override_kernel_check=true"
]
}
EOF
systemctl daemon-reload && systemctl restart docker

9、安装 kubeadm 和 kubelet

9.1配置安装源

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=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缓存,输入y添加证书认证
    yum makecache fast

9.2 安装k8s

yum -y install  kubeadm-1.22.2-0.x86_64 kubelet-1.22.2-0.x86_64 kubectl-1.22.2-0.x86_64
  • 设置kubectl与kubeadm命令补全,下次login生效
    kubectl completion bash > /etc/bash_completion.d/kubectl
    kubeadm completion bash > /etc/bash_completion.d/kubeadm

10、拉取所需镜像

  • 由于国内网络因素,镜像只能拉去dockerhub的镜像,有条件的同学自己拉取,这边也准备了镜像。
kubeadm config images list --kubernetes-version v1.22.2

k8s.gcr.io/kube-apiserver:v1.22.2
k8s.gcr.io/kube-controller-manager:v1.22.2
k8s.gcr.io/kube-scheduler:v1.22.2
k8s.gcr.io/kube-proxy:v1.22.2
k8s.gcr.io/pause:3.5
k8s.gcr.io/etcd:3.5.0-0
k8s.gcr.io/coredns/coredns:v1.8.4
准备了所有的镜像
链接:https://pan.baidu.com/s/10yKcDMDCqnAG1kqNruBm6w        l8h4 
  • 注意三个节点都要导入镜像。

    tar -zxvf k8s-1-22.tar.gz

    cd k8s-1.22

    ls
    total 1.1G
    -rw-r--r-- 1 root root 198K Sep 25 21:41 calico.yaml
    -rw------- 1 root root 1.1G Sep 25 22:06 k8s-1-22-all.tar.gz

    docker image load -i k8s-1.22-all.tar.gz

三、初始化集群

  • 主节点执行以下命令

1、使用kubeadm init初始化集群(注意修 apiserver 地址为本机IP)

kubeadm init  --kubernetes-version=v1.22.2 --apiserver-advertise-address=192.168.10.20 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.1.0.0/16
  • 初始化成功后会输出类似下面的加入命令,暂时无需运行,先记录。

    kubeadm join 192.168.10.20:6443 --token xsdadasatjr4 --discovery-token-ca-cert-hash sha256:4622asdksdjalklaksdl5efe2b35f169ccc2c2a43df11cbc2af5f5473

2、为需要使用kubectl的用户进行配置

  • 每次启动自动加载$HOME/.kube/config下的密钥配置文件(K8S自动行为)
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config

3、 集群网络配置安装 calico

  • 在刚才的解压目录里面有calico.yaml,执行以下命令。
    kubectl apply -f calico.yaml

4、添加计算节点

  • 在node1,node2节点执行一下命令

    kubeadm join 192.168.10.20:6443 --token xsdadasatjr4 --discovery-token-ca-cert-hash sha256:4622asdksdjalklaksdl5efe2b35f169ccc2c2a43df11cbc2af5f5473

  • 注意:没有记录集群 join 命令的可以通过以下方式重新获取

    kubeadm token create --print-join-command --ttl=0

  • 查看集群中的节点状态,可能要等等许久才Ready

    kubectl get nodes
    NAME         STATUS   ROLES                  AGE   VERSION
    k8s-master   Ready    control-plane,master   16d   v1.22.2
    k8s-node1    Ready                     16d   v1.22.2
    k8s-node2    Ready                     16d   v1.22.2

你可能感兴趣的:(部署k8s 1.22.2 集群 && Euler部署k8s 1.22.2 集群)