C语言学习网

kubeadm如何部署单Master节点K8S集群

发表于:2022-10-04 作者:安全数据网编辑
编辑最后更新 2022年10月04日,今天就跟大家聊聊有关kubeadm如何部署单Master节点K8S集群,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1 环境Host Name

今天就跟大家聊聊有关kubeadm如何部署单Master节点K8S集群,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1 环境

Host NameRoleIP
master1master110.10.25.149
node1node110.10.25.150
node2node210.10.25.151

2 内核调优

vim /etc/sysctl.confnet.ipv6.conf.all.disable_ipv6 = 1net.ipv6.conf.default.disable_ipv6 = 1net.ipv6.conf.lo.disable_ipv6 = 1vm.swappiness = 0net.ipv4.neigh.default.gc_stale_time=120net.ipv4.ip_forward = 1# see details in https://help.aliyun.com/knowledge_detail/39428.htmlnet.ipv4.conf.all.rp_filter=0net.ipv4.conf.default.rp_filter=0net.ipv4.conf.default.arp_announce = 2net.ipv4.conf.lo.arp_announce=2net.ipv4.conf.all.arp_announce=2# see details in https://help.aliyun.com/knowledge_detail/41334.htmlnet.ipv4.tcp_max_tw_buckets = 5000net.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_syn_backlog = 1024net.ipv4.tcp_synack_retries = 2kernel.sysrq = 1net.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1net.bridge.bridge-nf-call-arptables = 1modprobe br_netfiltersysctl -p

3 设置文件最大描述符

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

4 配置yum 源

cat < /etc/yum.repos.d/kubernetes.repo[kubernetes]name=Kubernetesbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/enabled=1gpgcheck=1repo_gpgcheck=1gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOFcd /etc/yum.repos.dwget https://download.docker.com/linux/centos/docker-ce.repo

5 安装依赖和常用软件

yum install -y epel-releaseyum install -y yum-utils device-mapper-persistent-data lvm2 net-tools conntrack-tools wget vim  ntpdate libseccomp libtool-ltdl lrzsz wget

6 时间同步

一个集群内的时间同步必不可少

systemctl enable ntpdate.serviceecho '*/30 * * * * /usr/sbin/ntpdate time7.aliyun.com >/dev/null 2>&1' > /tmp/crontab2.tmpcrontab /tmp/crontab2.tmpsystemctl start ntpdate.servicentpdate -u ntp.api.bz

7 关闭SELinux、防火墙

systemctl stop firewalldsystemctl disable firewalldsetenforce 0sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

8 关闭系统的Swap

swapoff -ayes | cp /etc/fstab /etc/fstab_bakcat /etc/fstab_bak |grep -v swap > /etc/fstab

9 安装docker

yum list docker-ce --showduplicates | sort -r yum install docker-ce-systemctl daemon-reloadsystemctl enable dockersystemctl start docker

10 配置hosts 解析

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4::1         localhost localhost.localdomain localhost6 localhost6.localdomain610.10.25.151 node210.10.25.149 master-110.10.25.150 node1

11 配置节点免密登录

ssh-keygenssh-copy-id -i ~/.ssh/id_rsa.pub  用户名字@192.168.x.xxx

12 配置ipvs模块

cat > /etc/sysconfig/modules/ipvs.modules <

13 master 和node 节点安装 kubelet kubeadm kubectl

yum install -y kubelet kubeadm kubectl  mastersystemctl enable kubelet暂不启动 kubelet

14 master节点进行集群初始化

kubeadm init --kubernetes-version=v1.14.1 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12保存这段内容  mkdir -p $HOME/.kube  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config  sudo chown $(id -u):$(id -g) $HOME/.kube/configYou should now deploy a pod network to the cluster.Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:  https://kubernetes.io/docs/concepts/cluster-administration/addons/Then you can join any number of worker nodes by running the following on each as root:kubeadm join 10.10.25.149:6443 --token r03k6k.rhc8lh0bhjzuz7vx \    --discovery-token-ca-cert-hash sha256:b6b354ce28904600e9e38b4803ca5834061f1ffce0cde08ab9fd002756fcfc14

15 创建相关文件夹

 mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

16 初始化 flannel网络

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.ymlflannel 网络默认使用 vxlan 模式此外flannel 还有其他两种模式分别为 udp 模式和 host-gw 模式flannel 模式介绍:vxlan 模式: 属于隧道封装,豹纹会封装很多层增加了额外开销,vxlan 里面还有一种 "Directrouting": ture 模式效率相对很高host-gw模式: 属于三层网络,采用的是将宿主机作为网关不进行封装传输,其效果还优于calicoudp模式: 是由于flannel网络出现的时候linux 内核还不支持,所以采用了udp方式,此种方式的效率比vxlan的但是还要更加的底所以无需考虑,这种模式也是造成业界任务flannel 网络效率底愿意之一设计网络模式需要在部署k8s集群前规划好,以免中途改变费时费力cd /etc/kubernetes/manifestswget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.ymlvim kube-flannel.yml 修改网络模式 为host-gw模式data:  cni-conf.json: |    {      "name": "cbr0",      "plugins": [        {          "type": "flannel",          "delegate": {            "hairpinMode": true,            "isDefaultGateway": true          }        },        {          "type": "portmap",          "capabilities": {            "portMappings": true          }        }      ]    }  net-conf.json: |    {      "Network": "10.244.0.0/16",      "Backend": {        "Type": "host-gw"      }    }kubectl apply -f kube-flannel.yml查看路由信息是否已经改外host-gwip route showdefault via 10.10.25.254 dev ens192 proto static metric 100 10.10.25.0/24 dev ens192 proto kernel scope link src 10.10.25.149 metric 100 10.244.0.0/24 dev cni0 proto kernel scope link src 10.244.0.1 10.244.1.0/24 via 10.10.25.151 dev ens192 10.244.2.0/24 via 10.10.25.150 dev ens192 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1

17 拷贝kubelet 配置文件到node节点

scp /etc/sysconfig/kubelet  10.10.25.150:/etc/sysconfig/kubeletscp /etc/sysconfig/kubelet  10.10.25.150:/etc/sysconfig/kubelet

18 将node节点加入到集群

在node节点运行以下命令

kubeadm join 10.10.25.149:6443 --token r03k6k.rhc8lh0bhjzuz7vx     --discovery-token-ca-cert-hash sha256:b6b354ce28904600e9e38b4803ca5834061f1ffce0cde08ab9fd002756fcfc14

19 查看集群状态

kubectl get nodeNAME       STATUS   ROLES    AGE    VERSIONmaster-1   Ready    master   109m   v1.14.1node1      Ready       54m    v1.14.1node2      Ready       54m    v1.14.1

20 查看kube-system 下的pod

kubectl get pod -n kube-system -o wide因为使用kubeadm部署k8s集群默认pod组件pod运行在kebe-system命名空间内

21 启动ipvs

kube-proxy开启ipvs修改ConfigMap的kube-system/kube-proxy中的config.conf,`mode: "ipvs"`:kubectl edit cm kube-proxy -n kube-system kubectl get pod -n kube-system | grep kube-proxy | awk '{system("kubectl delete pod "$1" -n kube-system")}'

22 创建一个测试pod 验证集群

kubectl run net-test --image=alpine --replicas=2 sleep 3600

23 查看网卡信息

ifconfig

看完上述内容,你们对kubeadm如何部署单Master节点K8S集群有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0