部署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 <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
modprobe -- br_netfilter
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /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 <<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=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    <none>                 16d   v1.22.2
    k8s-node2    Ready    <none>                 16d   v1.22.2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,045评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,114评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,120评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,902评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,828评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,132评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,590评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,258评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,408评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,335评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,385评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,068评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,660评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,747评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,967评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,406评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,970评论 2 341

推荐阅读更多精彩内容