1.多节点k8s二进制部署

1.1部署思路

1.增加k8s管理节点master02
2.使用nginx负载均衡架构

1.2实验环境

master01:192.168.58.10
master02:192.168.58.60
node1:192.168.58.40
node2:192.168.58.50
nginx1:192.168.58.70
nginx2:192.168.58.80
nginx机器的IP需为部署k8s证书时设置的代理节点1和2的IP
已存在一个单节点的k8s架构,部署可参考以前博客

1.3部署master02

将master01的k8s工作目录、etcd证书、服务启动脚本拷贝给master02

scp -r /k8s/ root@192.168.58.60:/k8s/
scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service root@192.168.58.60:/usr/lib/systemd/system/
##无需增加etcd集群节点,master02无需启动etcd服务,但需要etcd证书文件,用于和etcd通信认证

修改apiserver配置文件中的IP地址

vi /k8s/kubernetes/cfg/kube-apiserver
--bind-address=192.168.58.60 \
--advertise-address=192.168.58.60 \

启动服务

systemctl enable kube-apiserver
systemctl enable kube-controller-manager
systemctl enable kube-scheduler 
systemctl start kube-apiserver 
systemctl start kube-controller-manager 
systemctl start kube-scheduler

添加k8s命令到环境变量

echo export PATH=$PATH:/k8s/kubernetes/bin >> /etc/profile
source /etc/profile

查看节点状态

kubectl get node
##两个节点状态为ready,即master02部署成功

1.4部署nginx负载均衡

两台nginx机器安装nginx且修改配置文件,开启四层转发

##这里只显示配置文件需要添加的部分
##由于是四层转发,所以不在http模块内,单独创建stream模块
##若配置文件stream报错,原因是nginx未装stream模块,需在源码目录./configure --with-stream然后重新编译安装make && make install
##日志目录不存在,需手动创建。。8j'm
stream {
    log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
    access_log  /var/log/nginx/k8s-access.log  main;

        upstream kube-apiserver {
                      server 192.168.58.10:6443;
                      server 192.168.58.60:6443;
      }
        server {
                      listen 6443;
                      proxy_pass kube-apiserver;
      }
}

mkdir -p /var/log/nginx/
systemctl restart nginx
netstat -napt | grep nginx
##发现多了一个6443端口的nginx监听进程

两台nginx机器部署keepalived
虚拟IP需设置在部署k8s证书时里面认证的虚拟IP地址

yum -y install keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script check_nginx {                           #定义一个函数check_nginx
    script "/usr/local/nginx/sbin/check_nginx.sh"       #函数内容为一个检测nginx服务是否存活的脚本
}

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id nginx_1         #定义该主机在群集中的id,nginx2机器需要命名为不一样的
}

vrrp_instance VI_1 {
    state MASTER              #nginx2节点命名为BACKUP
    interface ens33             #修改网卡名,centos7开始为ens33,centos6为eth0
    virtual_router_id 51
    priority 105                    #nginx2节点优先级设为95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {      #设置VIP
        192.168.58.200
    }
    track_script {             #该vrrp实例VI_1调用上面定义的函数check_nginx
        check_nginx
     }
}

两台nginx创建nginx服务检测脚本并启动keepalived

vi /usr/local/nginx/sbin/check_nginx.sh
count=$(ps -ef |grep nginx |egrep -cv "grep|$$")    #查看nginx进程
if [ "$count" -eq 0 ];then                 #如果nginx进程关闭了,则关闭keepalived服务
    systemctl stop keepalived
fi

chmod +x /usr/local/nginx/sbin/check_nginx.sh
systemctl start keepalived

检查虚拟IP

ip addr

nginx1机器上虚拟IP生效,nginx2没有
在这里插入图片描述
修改两个k8s-node节点配置文件,指定服务端IP为虚拟IP

vi /k8s/kubernetes/cfg/bootstrap.kubeconfig
server: https://192.168.58.200:6443

vi /k8s/kubernetes/cfg/kubelet.kubeconfig
server: https://192.168.58.200:6443

vi /k8s/kubernetes/cfg/kube-proxy.kubeconfig
server: https://192.168.58.200:6443

systemctl restart kubelet
systemctl restart kube-proxy

在nginx1查看日志
k8s-node成功访问虚拟IP,且负载均衡已生效
在这里插入图片描述

1.5测试

1.5.1keepalived测试

nginx1服务器上

pkill nginx
ip addr
##此时虚拟IP已不存在

nginx2服务器上

ip addr

虚拟IP已漂移到nginx2上,keepalived服务正常
在这里插入图片描述
恢复
nginx1重启服务,虚拟IP漂移回来

#nginx1
systemctl start nginx
systemctl start keepalived
ip addr

1.5.2创建pod测试

创建nginx的pod

#master节点上
kubectl run nginx --image=nginx
kubectl get pods	#查看pod名,状态,状态需为running,不能为ContainerCreating(容器创建中)

查看服务的pod的日志

kubectl logs nginx-6db489d4b7-7v2fb

报错:
在这里插入图片描述
因为使用了system:anonymous(匿名)用户进行操作,没有权限

解决:

kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
#将集群中的匿名用户绑定到管理员用户,使其拥有权限

再次查看服务的pod的日志

kubectl logs nginx-6db489d4b7-7v2fb

此时可正常查看,但暂时没有日志产生

查看pod完整信息并访问pod

kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE            NOMINATED NODE   READINESS GATES
nginx-6db489d4b7-7v2fb   1/1     Running   0          8m59s   172.17.0.2   192.168.58.50   <none>           <none>

##在启动该pod的node上访问
curl 172.17.0.2

再次查看日志

kubectl logs nginx-6db489d4b7-7v2fb

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐