6.kubeadm高可用

kubeadm高可用

##节点设置

master01:192.168.242.66
master02:192.168.242.67
master03:192.168.242.68
node01:192.168.242.69
node02:192.168.242.70

环境初始化

###所有节点,关闭防火墙规则,关闭selinux,关闭swap交换
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config

iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
###修改主机名
hostnamectl set-hostname master01
hostnamectl set-hostname master02
hostnamectl set-hostname master03
hostnamectl set-hostname node01
hostnamectl set-hostname node02
###所有节点修改hosts文件
vim /etc/hosts
192.168.242.66 master01
192.168.242.67 master02
192.168.242.68 master03
192.168.242.69 node01
192.168.242.70 node02
###所有节点时间同步

yum -y install ntpdate

ntpdate ntp.aliyun.com

systemctl enable --now crond
 
crontab -e
*/30 * * * * /usr/sbin/ntpdate ntp.aliyun.com &> /dev/null
###所有节点实现Linux的资源限制

vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 65535
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
更改内核启动方式
grub2-set-default 0 && grub2-mkconfig -o /etc/grub2.cfg

grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"

grubby --default-kernel
reboot
###调整内核参数

cat > /etc/sysctl.d/k8s.conf <<EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720

net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF

#生效参数
sysctl --system  
###加载 ip_vs 模块

for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done

所有节点安装Docker

###安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2 
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
yum install -y docker-ce docker-ce-cli containerd.io
 
systemctl start docker.service
systemctl enable docker.service 
##修改配置文件和镜像加速

mkdir -p /etc/docker


tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://ysmprsek.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
      "max-size": "500m", "max-file": "3"
  }
}
EOF

systemctl daemon-reload
systemctl restart docker

所有节点安装kubeadm,kubelet和kubectl

###定义kubernetes源

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.20.15 kubeadm-1.20.15 kubectl-1.20.15
#配置Kubelet使用阿里云的pause镜像
cat > /etc/sysconfig/kubelet <<EOF
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.2"
EOF
###开机自启kubelet
systemctl enable kubelet.service
#K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启

高可用组件安装、配置

###所有 master 节点部署 Haproxy
yum -y install haproxy keepalived
cat > /etc/haproxy/haproxy.cfg << EOF
global
    log         127.0.0.1 local0 info
    log         127.0.0.1 local1 warning
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    #stats socket /var/lib/haproxy/stats

defaults
    mode                    tcp
    log                     global
    option                  tcplog
    option                  dontlognull
    option                  redispatch
    retries                 3
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout check           10s
    maxconn                 3000

frontend monitor-in
    bind *:33305
    mode http
    option httplog
    monitor-uri /monitor

frontend k8s-master
    bind *:16443
    mode tcp
    option tcplog
    default_backend k8s-master

backend k8s-master
    mode tcp
    option tcplog
    option tcp-check
    balance roundrobin
    server k8s-master1 192.168.242.66:6443  check inter 10000 fall 2 rise 2 weight 1
    server k8s-master2 192.168.242.67:6443  check inter 10000 fall 2 rise 2 weight 1
    server k8s-master3 192.168.242.68:6443  check inter 10000 fall 2 rise 2 weight 1
EOF
###所有 master 节点部署 keepalived
yum -y install keepalived

cd /etc/keepalived/
vim keepalived.conf


! Configuration File for keepalived
global_defs {
    router_id k8s-01			#路由标识符,每个节点配置不同
}

vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER				#本机实例状态,MASTER/BACKUP,备机配置文件中设置BACKUP
    interface ens32
    virtual_router_id 51
    priority 100				#本机初始权重,备机设置小于主机的值
    advert_int 1
    virtual_ipaddress {
        192.168.242.100          #设置VIP地址
    }
    track_script {
        chk_haproxy
    }
}


###监控脚本
vim check_haproxy.sh
#!/bin/bash
if ! killall -0 haproxy; then
    systemctl stop keepalived
fi

chmod +x check_haproxy.sh
systemctl enable --now haproxy
systemctl enable --now keepalived

部署k8s集群

###在 master01 节点上设置集群初始化配置文件
kubeadm config print init-defaults > /opt/kubeadm-config.yaml

cd /opt/
vim kubeadm-config.yaml
11 localAPIEndpoint:
12   advertiseAddress: 192.168.242.66		#指定当前master节点的IP地址
13   bindPort: 6443

21 apiServer:
22   certSANs:								
#在apiServer属性下面添加一个certsSANs的列表,添加所有master节点的IP地址和集群VIP地址
23   - 192.168.242.100
24   - 192.168.242.66
25   - 192.168.242.67
26   - 192.168.242.68

30 clusterName: kubernetes
31 controlPlaneEndpoint: "192.168.242.100:6443"		#指定集群VIP地址
32 controllerManager: {}

38 imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers			#指定镜像下载地址
39 kind: ClusterConfiguration
40 kubernetesVersion: v1.20.15				#指定kubernetes版本号
41 networking:
42   dnsDomain: cluster.local
43   podSubnet: "10.244.0.0/16"				#指定pod网段,10.244.0.0/16用于匹配flannel默认网段
44   serviceSubnet: 10.96.0.0/16			#指定service网段
45 scheduler: {}
#末尾再添加以下内容
--- 
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs									#把默认的kube-proxy调度方式改为ipvs模式
#更新集群初始化配置文件
kubeadm config migrate --old-config kubeadm-config.yaml --new-config new.yaml
###所有节点拉取镜像
###拷贝yaml配置文件给其他主机,通过配置文件进行拉取镜像
for i in master02 master03 node01 node02; do scp /opt/new.yaml $i:/opt/; done

###所有的节点拉取镜像
kubeadm config images pull --config /opt/new.yaml
###master01节点进行初始化
kubeadm init --config new.yaml --upload-certs | tee kubeadm-init.log


##获取master加入节点命令
kubeadm join 192.168.242.100:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:d02b1e6552ac56f579a6f943ea54d134faa90dcce09121fd3ecb0233a9c2ea6b \
    --control-plane --certificate-key e3f16b2c757f08a9f59eb80d126441cafd68ce387051d4d6fc966652b316a739


##获取node节点加入命令
kubeadm join 192.168.242.100:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:d02b1e6552ac56f579a6f943ea54d134faa90dcce09121fd3ecb0233a9c2ea6b
## kubectl的配置文件

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#若初始化失败,进行的操作
kubeadm reset -f
ipvsadm --clear 
rm -rf ~/.kube
再次进行初始化
修改controller-manager和scheduler配置文件
vim /etc/kubernetes/manifests/kube-scheduler.yaml 
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
......
    #- --port=0					#搜索port=0,把这一行注释掉

systemctl restart kubelet
###安装 CNI网络插件
##上传  flannel-v0.21.5.zip  到  /opt/k8s

unzip flannel-v0.21.5.zip
docker load -i flannel.tar
docker load -i flannel-cni-plugin.tar

##移动系统创建的cni目录,并手动创建
cd /opt/
mv cni/ cni_bak
mkdir -p /opt/cni/bin

##解压配置文件
tar xf /opt/k8s/cni-plugins-linux-amd64-v1.3.0.tgz -C /opt/cni/bin/

##安装插件
cd /opt/k8s
kubectl apply -f kube-flannel.yml
所有节点加入集群
#master 节点加入集群


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

#node 节点加入集群
查看节点
kubectl get pods
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜海赤竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值