发布时间: 2023-10-30 文章作者: myluzh 分类名称: Kubernetes 朗读文章
0x00 介绍
虚拟ip 172.16.10.20 master.k8s.io k8s-vip
master01 172.16.10.21 master01.k8s.io master01
master02 172.16.10.22 master02.k8s.io master02
node01 172.16.10.23 node01.k8s.io node01
0x01 初始化(所有节点)
# 关闭防火墙 systemctl stop firewalld systemctl disable firewalld # 关闭selinux setenforce 0 sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux # 关闭swap swapoff -a # 永久关闭swap sed -ri 's/.*swap.*/#&/' /etc/fstab # 根据规划设置master、worker节点主机名 hostnamectl set-hostname <hostname> # 添加 hosts(所有节点) cat >> /etc/hosts << EOF 172.16.10.20 master.k8s.io k8s-vip 172.16.10.21 master01.k8s.io k8s-master01 172.16.10.22 master02.k8s.io k8s-master02 172.16.10.23 node01.k8s.io ks8-node01 EOF # 将桥接的 IPv4 流量传递到 iptables 的链 cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system # 时间同步 yum install ntpdate -y ntpdate time.windows.com # 重启系统 reboot
0x02 部署keepalived(两个master节点)
1、在master节点中安装keepalived相关包
yum install -y conntrack-tools libseccomp libtool-ltdl psmisc yum install -y keepalived
2、配置master节点keepalived配置
可以把master01 priority 设置为101,master02 priority 设置为100,这样master01的优先级就会高于02。默认情况下vip也会在01上。
# master01 master02 配置是一样的 cat > /etc/keepalived/keepalived.conf <<EOF ! Configuration File for keepalived global_defs { router_id k8s } vrrp_script check_haproxy { script "killall -0 haproxy" interval 3 weight -2 fall 10 rise 2 } vrrp_instance VI_1 { state MASTER # 网卡接口 interface ens192 virtual_router_id 51 priority 250 advert_int 1 authentication { auth_type PASS auth_pass ceb163ec013d66163d6ab } virtual_ipaddress { # vip地址 172.16.10.20 } track_script { check_haproxy } } EOF
3、keepalived启动与检查
# 启动自启keepalived systemctl start keepalived systemctl enable keepalived systemctl status keepalived # 查看master网卡信息 ip address show ens192
0x03 部署haproxy
1、安装haproxy
yum install -y haproxy
2、配置haproxy
两台master节点的配置一样的,配置中声明了后端代理的两个master节点服务器,指定了haproxy运行的端口为16443等,因此16443端口为集群的入口。
cat > /etc/haproxy/haproxy.cfg << EOF #--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 #--------------------------------------------------------------------- # kubernetes apiserver frontend which proxys to the backends #--------------------------------------------------------------------- frontend kubernetes-apiserver mode tcp bind *:16443 option tcplog default_backend kubernetes-apiserver #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend kubernetes-apiserver mode tcp balance roundrobin server master01.k8s.io 172.16.10.21:6443 check server master02.k8s.io 172.16.10.22:6443 check #--------------------------------------------------------------------- # collection haproxy statistics message #--------------------------------------------------------------------- listen stats bind *:1080 stats auth admin:awesomePassword stats refresh 5s stats realm HAProxy\ Statistics stats uri /admin?stats EOF
3、启动与检查
# 启动与开机自启 systemctl enable haproxy systemctl start haproxy systemctl status haproxy # 检查端口 netstat -lntup | grep haproxy
0x04 安装docker、kubeadm、kubelet(所有节点)
1、安装docker
yum -y install yum-utils yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum -y install docker-ce-18.06.3.ce-3.el7 docker --version cat <<EOF > daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"] } EOF mv daemon.json /etc/docker/ systemctl daemon-reload systemctl enable docker systemctl restart docker systemctl status docker
2、安装k8s组件
# 添加k8s阿里云yum软件源 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.16.3 kubeadm-1.16.3 kubectl-1.16.3 systemctl enable kubelet
0x05 部署k8s master
1、在具有vip的master节点上创建kubeadm配置文件(我这里的master01)
mkdir /usr/local/kubernetes/manifests -p cd /usr/local/kubernetes/manifests/ # 编辑配置文件 vi kubeadm-config.yaml apiServer: certSANs: - master01 - master02 - master.k8s.io - 172.16.10.20 - 172.16.10.21 - 172.16.10.22 - 127.0.0.1 extraArgs: authorization-mode: Node,RBAC timeoutForControlPlane: 4m0s apiVersion: kubeadm.k8s.io/v1beta1 certificatesDir: /etc/kubernetes/pki clusterName: kubernetes controlPlaneEndpoint: "master.k8s.io:16443" controllerManager: {} dns: type: CoreDNS etcd: local: dataDir: /var/lib/etcd imageRepository: registry.aliyuncs.com/google_containers kind: ClusterConfiguration kubernetesVersion: v1.16.3 networking: dnsDomain: cluster.local podSubnet: 10.244.0.0/16 serviceSubnet: 10.1.0.0/16 scheduler: {}
2、在具有vip的master节点运行(我这里是master01)
kubeadm init --config kubeadm-config.yaml
3、根据提示配置环境变量
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config # 验证节点状态是否正常 kubectl get nodes kubectl get pods -n kube-system
4、按照提示保存下面内容,一会需要用到
kubeadm join master.k8s.io:16443 --token g57ri1.wci79ladrai0hw2s \ --discovery-token-ca-cert-hash sha256:adf6bfdd443b970a6db50c05882c2316a7d71b7f4df763a863d819cc6d3d3d70 \ --control-plane
0x06 master02节点加入集群
1、复制密钥及相关文件
# 从master1复制密钥及相关文件到master02 ssh root@172.16.10.22 mkdir -p /etc/kubernetes/pki/etcd scp /etc/kubernetes/admin.conf root@172.16.10.22:/etc/kubernetes scp /etc/kubernetes/pki/{ca.*,sa.*,front-proxy-ca.*} root@172.16.10.22:/etc/kubernetes/pki scp /etc/kubernetes/pki/etcd/ca.* root@172.16.10.22:/etc/kubernetes/pki/etcd
2、master02加入集群
执行在master1上init后输出的join命令,需要带上参数--control-plane表示把master控制节点加入集群。kubeadm join master.k8s.io:16443 --token g57ri1.wci79ladrai0hw2s \ --discovery-token-ca-cert-hash sha256:adf6bfdd443b970a6db50c05882c2316a7d71b7f4df763a863d819cc6d3d3d70 \ --control-plane \
3、检查
kubectl get node kubectl get pods --all-namespaces
0x07 加入k8s node节点
1、在node01上执行join
kubeadm join master.k8s.io:16443 --token g57ri1.wci79ladrai0hw2s \ --discovery-token-ca-cert-hash sha256:adf6bfdd443b970a6db50c05882c2316a7d71b7f4df763a863d819cc6d3d3d70
2、在master上检查
kubectl get node kubectl get pods --all-namespaces
0x08 安装集群网络
# 从官方地址获取到flannel的yaml,在master1上执行 mkdir flannel cd flannel wget -c https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml # 安装flannel网络 kubectl apply -f kube-flannel.yml # 检查 kubectl get pods -n kube-system
标签: k8s 高可用 keepalived haproxy
发表评论