最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

k8s 添加 node

IT圈 admin 7浏览 0评论

k8s 添加 node

当微服务不断增加时,资源不够用,需要增加新的node

系统:centos 8

修改hostname

hostnamectl set-hostname xxxx

重启生效

安装必要的软件

 yum -y install wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip  libaio-devel wget vim ncurses-devel autoconf automake zlib-devel  epel-release lrzsz openssh-server socat  ipvsadm conntrack bind-utils epel-release libffi-devel libaio-devel libxml2-devel cmake  device-mapper-persistent-data lvm2 yum-utils

其中yum-utils可以方便地管理软件源

关闭防火墙

systemctl stop firewalld  && systemctl  disable  firewalld
yum install iptables-services -y
iptables -F && service iptables save
service iptables stop && systemctl disable iptables

关闭selinux

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

关闭swap

swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab

设置系统参数【所有节点】
(1)针对于linux7以上,设置允许路由转发,不对bridge的数据进行处理
创建 /etc/sysctl.d/k8s.conf 文件
vim /etc/sysctl.d/k8s.conf
加入下面内容:
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
(2)挂载br_nebrtfilter
modprobe br_netfilter
(3)生效配置文件
sysctl -p /etc/sysctl.d/k8s.conf
sysctl命令:用于运行时配置内核参数
查看是否生成相关文件
ls /proc/sys/net/bridge
(4)资源配置文件
vim /etc/security/limits.conf 是 Linux 资源使用配置文件,用来限制用户对系统资源的使用,末尾加入

echo “* soft nofile 65536” >> /etc/security/limits.conf
echo “* hard nofile 65536” >> /etc/security/limits.conf
echo “* soft nproc 65536” >> /etc/security/limits.conf
echo “* hard nproc 65536” >> /etc/security/limits.conf
echo “* soft memlock unlimited” >> /etc/security/limits.conf
echo “* hard memlock unlimited” >> /etc/security/limits.conf

添加软件源

配置安装k8s需要的yum源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=
enabled=1
gpgcheck=0
EOF

配置docker yum源,如果你的很慢,可以选择下方的阿里云docker yum源

#yum-config-manager --add-repo .repo
yum-config-manager --add-repo .repo

清理缓存

yum clean all
yum makecache fast

安装docker

由于CentOS 8 默认使用podman代替Docker,执行
centos8 先运行:

yum erase podman buildah
yum install -y docker-cesystemctl enable docker && systemctl start dockersystemctl status docker

注意查看docker的状态,是否有异常信息

如提示:

Centos /var/log/messages Unknown lvalue ‘TasksMax’ in section ‘Service’

解决方案
#yum install systemd-* -y
#systemctl status docker.service验证方案
# systemctl restart docker.service
# systemctl status docker.service

“Not using native diff for overlay2, this may cause degraded performance for building images: opaque flag erroneously copied up, consider update to kernel 4.8 or later to fix” storage-driver=overlay2

表示要升级内核

修改docker配置文件

cat > /etc/docker/daemon.json <<EOF
{"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

重启docker

systemctl daemon-reload && systemctl restart docker

安装k8s

开启k8s 网络桥接相关内核配置
设置网桥包经IPTables,core文件生成路径,配置永久生效

echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 >/proc/sys/net/bridge/bridge-nf-call-ip6tablescat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOFsysctl -p

安装kubeadm,kubelet和kubectl

 yum install kubeadm-1.18.0 kubelet-1.18.0 kubectl-1.18.0 -y systemctl enable kubelet && systemctl start kubelet

配置k8s服务启动顺序

cat /etc/systemd/system/multi-user.target.wants/kubelet.service
[Unit]
Description=kubelet: The Kubernetes Node Agent
Documentation=/
Wants=network-online.target
After=network-online.target
After=containerd.service
After=docker.service

添加了最后两行

加入k8s集群

在master节点运行以下命令,查询加入集群的指令:

kubeadm token create --print-join-command

输出类似下面:

kubeadm join 192.168.1.199:6443 --token 7dwluq.x6nypje7h55rnrhl \--discovery-token-ca-cert-hash sha256:fa75619ab0bb6273126350a9dbda9aa6c89828c2c4650299fe1647ab510a7e6c

在新节点运行上面的命令即可。

node提示:

untime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized

表示插件kube-flannel未安装成功,通过是镜像下载过慢,如果手工下载:

docker pull quay.io/coreos/flannel:v0.12.0-amd64

配置节点保留资源

在节点的 /var/lib/kubelet/config.yaml 加入下列內容:

systemReserved:memory: 500Micpu: 250m
kubeReserved:memory: 500Micpu: 500m
evictionHard:memory.available: 200Minodefs.available: 20Gi

重启kubelet载入新设定

sudo systemctl restart kubelet

常用命令

删除节点

(1)卸载节点(drain 翻译排出,此时卸载节点,但是没有删除)

kubectl drain < node name > --delete-local-data --force --ignore-daemonsets

(2)删除节点

kubectl delete node < node name >

(3)清空init配置,需要删除的节点上执行

kubeadm reset

要重新加入再次执行kubeadm join命令即可。

批量删除失败pod

kubectl get pods -n dev | grep OutOfmemory | cut -d' ' -f 1 | xargs kubectl delete pod -n dev

删除所有因内存不足失败的pod

k8s 添加 node

当微服务不断增加时,资源不够用,需要增加新的node

系统:centos 8

修改hostname

hostnamectl set-hostname xxxx

重启生效

安装必要的软件

 yum -y install wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip  libaio-devel wget vim ncurses-devel autoconf automake zlib-devel  epel-release lrzsz openssh-server socat  ipvsadm conntrack bind-utils epel-release libffi-devel libaio-devel libxml2-devel cmake  device-mapper-persistent-data lvm2 yum-utils

其中yum-utils可以方便地管理软件源

关闭防火墙

systemctl stop firewalld  && systemctl  disable  firewalld
yum install iptables-services -y
iptables -F && service iptables save
service iptables stop && systemctl disable iptables

关闭selinux

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

关闭swap

swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab

设置系统参数【所有节点】
(1)针对于linux7以上,设置允许路由转发,不对bridge的数据进行处理
创建 /etc/sysctl.d/k8s.conf 文件
vim /etc/sysctl.d/k8s.conf
加入下面内容:
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
(2)挂载br_nebrtfilter
modprobe br_netfilter
(3)生效配置文件
sysctl -p /etc/sysctl.d/k8s.conf
sysctl命令:用于运行时配置内核参数
查看是否生成相关文件
ls /proc/sys/net/bridge
(4)资源配置文件
vim /etc/security/limits.conf 是 Linux 资源使用配置文件,用来限制用户对系统资源的使用,末尾加入

echo “* soft nofile 65536” >> /etc/security/limits.conf
echo “* hard nofile 65536” >> /etc/security/limits.conf
echo “* soft nproc 65536” >> /etc/security/limits.conf
echo “* hard nproc 65536” >> /etc/security/limits.conf
echo “* soft memlock unlimited” >> /etc/security/limits.conf
echo “* hard memlock unlimited” >> /etc/security/limits.conf

添加软件源

配置安装k8s需要的yum源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=
enabled=1
gpgcheck=0
EOF

配置docker yum源,如果你的很慢,可以选择下方的阿里云docker yum源

#yum-config-manager --add-repo .repo
yum-config-manager --add-repo .repo

清理缓存

yum clean all
yum makecache fast

安装docker

由于CentOS 8 默认使用podman代替Docker,执行
centos8 先运行:

yum erase podman buildah
yum install -y docker-cesystemctl enable docker && systemctl start dockersystemctl status docker

注意查看docker的状态,是否有异常信息

如提示:

Centos /var/log/messages Unknown lvalue ‘TasksMax’ in section ‘Service’

解决方案
#yum install systemd-* -y
#systemctl status docker.service验证方案
# systemctl restart docker.service
# systemctl status docker.service

“Not using native diff for overlay2, this may cause degraded performance for building images: opaque flag erroneously copied up, consider update to kernel 4.8 or later to fix” storage-driver=overlay2

表示要升级内核

修改docker配置文件

cat > /etc/docker/daemon.json <<EOF
{"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

重启docker

systemctl daemon-reload && systemctl restart docker

安装k8s

开启k8s 网络桥接相关内核配置
设置网桥包经IPTables,core文件生成路径,配置永久生效

echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 >/proc/sys/net/bridge/bridge-nf-call-ip6tablescat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOFsysctl -p

安装kubeadm,kubelet和kubectl

 yum install kubeadm-1.18.0 kubelet-1.18.0 kubectl-1.18.0 -y systemctl enable kubelet && systemctl start kubelet

配置k8s服务启动顺序

cat /etc/systemd/system/multi-user.target.wants/kubelet.service
[Unit]
Description=kubelet: The Kubernetes Node Agent
Documentation=/
Wants=network-online.target
After=network-online.target
After=containerd.service
After=docker.service

添加了最后两行

加入k8s集群

在master节点运行以下命令,查询加入集群的指令:

kubeadm token create --print-join-command

输出类似下面:

kubeadm join 192.168.1.199:6443 --token 7dwluq.x6nypje7h55rnrhl \--discovery-token-ca-cert-hash sha256:fa75619ab0bb6273126350a9dbda9aa6c89828c2c4650299fe1647ab510a7e6c

在新节点运行上面的命令即可。

node提示:

untime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized

表示插件kube-flannel未安装成功,通过是镜像下载过慢,如果手工下载:

docker pull quay.io/coreos/flannel:v0.12.0-amd64

配置节点保留资源

在节点的 /var/lib/kubelet/config.yaml 加入下列內容:

systemReserved:memory: 500Micpu: 250m
kubeReserved:memory: 500Micpu: 500m
evictionHard:memory.available: 200Minodefs.available: 20Gi

重启kubelet载入新设定

sudo systemctl restart kubelet

常用命令

删除节点

(1)卸载节点(drain 翻译排出,此时卸载节点,但是没有删除)

kubectl drain < node name > --delete-local-data --force --ignore-daemonsets

(2)删除节点

kubectl delete node < node name >

(3)清空init配置,需要删除的节点上执行

kubeadm reset

要重新加入再次执行kubeadm join命令即可。

批量删除失败pod

kubectl get pods -n dev | grep OutOfmemory | cut -d' ' -f 1 | xargs kubectl delete pod -n dev

删除所有因内存不足失败的pod

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论