k8s二进制部署--多master、负载均衡、高可用
本地nginx监听端口为6443和80,6443负责负载均衡代理,80负责web展示服务。VIP的6443端口分别与nginx01/nginx02相连接。修改node节点上的bootstrap.kubeconfig、kubelet.kubeconfig和kube-proxy.kubeconfig配置文件中的server为VIP。//从 master01 节点上拷贝证书文件、各master组件的配置
目录
3. 部署keepalived服务(nginx主机,以nginx01为例)
3.4启动keepalived服务(一定要先启动了nginx服务,再启动keepalived服务)
3.5 修改node节点对接IP(node节点,以node1为例)
3.6 在nginx01 上查看 nginx 和 node 、 master 节点的连接状态
1、环境准备
1.1 服务器配置
服务器 | 主机名 | IP地址 | 主要组件/说明 |
---|---|---|---|
master01节点+etcd01节点 | master01 | 192.168.80.116 | kube-apiserver kube-controller-manager kube-schedular etcd |
master02节点 | master02 | 192.168.80.115 | kube-apiserver kube-controller-manager kube-schedular |
node01节点+etcd02节点 | node01 | 192.168.80.111 | kubelet kube-proxy docker flannel |
node02节点+etcd03节点 | node02 | 192.168.80.112 | kubelet kube-proxy docker flannel |
nginx01节点 | nginx01 | 192.168.80.113 | keepalived负载均衡(主) |
nginx02节点 | nginx02 | 192.168.80.114 | keepalived负载均衡(备) |
1.2 master02 节点部署
//从 master01 节点上拷贝证书文件、各master组件的配置文件和服务管理文件到 master02 节点
scp -r /opt/etcd/ root@192.168.80.115:/opt/
scp -r /opt/kubernetes/ root@192.168.80.115:/opt
scp -r /root/.kube root@192.168.80.115:/root
scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service root@192.168.80.115:/usr/lib/systemd/system/
//修改配置文件kube-apiserver中的IP
vim /opt/kubernetes/cfg/kube-apiserver KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.80.116:2379,https://192.168.80.111:2379,https://192.168.80.112:2379\
--bind-address=192.168.80.115 \ 修改
--secure-port=6443 \
--advertise-address=192.168.80.115 \ 修改
//在 master02 节点上启动各服务并设置开机自启
systemctl start kube-apiserver.service
systemctl enable kube-apiserver.service
systemctl start kube-controller-manager.service
systemctl enable kube-controller-manager.service
systemctl start kube-scheduler.service
systemctl enable kube-scheduler.service
//查看node节点状态
ln -s /opt/kubernetes/bin/* /usr/local/bin/
kubectl get nodes
kubectl get nodes -o wide
#-o=wide:输出额外信息;对于Pod,将输出Pod所在的Node名 //此时在master02节点查到的node节点状态仅是从etcd查询到的信息,而此时node节点实际上并未与master02节点建立通信连接,因此需要使用一个VIP把node节点与master节点都关联起来
2、负载均衡部署
- 配置load balancer集群双机热备负载均衡(nginx实现负载均衡,keepalived实现双机热备)
- 在nginx01和nginx02节点上操作
2.1 下载nginx
配置nginx的官方在线yum源,配置本地nginx的yum源
[root@nginx01 ~]# cat > /etc/yum.repos.d/nginx.repo << 'EOF'
> [nginx]
> name=nginx repo
> baseurl=http://nginx.org/packages/centos/7/$basearch/
> gpgcheck=0
> EOF
[root@nginx01 ~]# yum install -y nginx
2.2 修改nginx配置文件
配置四层反向代理负载均衡,指定k8s群集2台master的节点ip和6443端口
vim /etc/nginx/nginx.conf
events {
worker_connections 1024;
}
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
access_log /var/log/nginx/k8s-access.log main;
upstream k8s-apiserver {
server 192.168.80.115:6443;
server 192.168.80.116:6443;
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
http { ......
2.3 启动nginx
2.3.1 检查配置文件语法
nginx -t
2.3.2 启动nginx服务,查看已监听6443端口
systemctl start nginx
systemctl enable nginx
netstat -natp | grep nginx
3. 部署keepalived服务(nginx主机,以nginx01为例)
3.1 下载keepalived
yum install keepalived -y
3.2 修改keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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_MASTER #lb01节点的为 NGINX_MASTER,lb02节点的为 NGINX_BACKUP
}
#添加一个周期性执行的脚本
vrrp_script check_nginx {
script "/etc/nginx/check_nginx.sh" #指定检查nginx存活的脚本路径
}
vrrp_instance VI_1 {
state MASTER #lb01节点的为 MASTER,lb02节点的为 BACKUP
interface ens33 #指定网卡名称 ens33
virtual_router_id 51 #指定vrid,两个节点要一致
priority 100 #lb01节点的为 100,lb02节点的为 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.80.100/24 #指定 VIP
}
track_script {
check_nginx #指定vrrp_script配置的脚本
}
}
3.3 创建nginx状态检查脚本
vim /etc/nginx/check_nginx.sh
#!/bin/bash
#egrep -cv "grep|$$" 用于过滤掉包含grep 或者 $$ 表示的当前Shell进程ID,即脚本运行的当前进程ID号
count=$(ps -ef | grep nginx | egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
systemctl stop keepalived
fi
chmod +x /etc/nginx/check_nginx.sh
3.4启动keepalived服务(一定要先启动了nginx服务,再启动keepalived服务)
systemctl start keepalived
systemctl enable keepalived
ip a #查看VIP是否生成
3.5 修改node节点对接IP(node节点,以node1为例)
3.5.1 修改node节点配置文件
修改node节点上的bootstrap.kubeconfig、kubelet.kubeconfig和kube-proxy.kubeconfig配置文件中的server为VIP
cd /opt/kubernetes/cfg/
vim bootstrap.kubeconfig
server: https://192.168.80.100:6443
vim kubelet.kubeconfig
server: https://192.168.80.100:6443
vim kube-proxy.kubeconfig
server: https://192.168.80.100:6443
3.5.2 重启kubelet和kube-proxy服务
systemctl restart kubelet.service
systemctl restart kube-proxy.service
3.6 在nginx01 上查看 nginx 和 node 、 master 节点的连接状态
netstat -natp | grep nginx
本地nginx监听端口为6443和80,6443负责负载均衡代理,80负责web展示服务。 VIP的6443端口分别与nginx01/nginx02相连接。 master01/master02的6443端口分别与nginx01相连接。
自此,多节点负载均衡搭建完毕。
4、在 master01 节点上操作
4.1 查看pode、node列表
[root@master02 ~]# kubectl get node
[root@master02 ~]# kubectl get pod
4.2 创建pod
//测试创建pod
kubectl run nginx --image=nginx
//查看Pod的状态信息
[root@master01 k8s]#kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 80s #创建中
sh 1/1 Running 0 157m
[root@master01 k8s]#kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 3m7s #创建完成,运行中
sh 1/1 Running 0 159m
[root@master01 k8s]#kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 4m 172.16.70.132 192.168.80.112 <none> <none>
sh 1/1 Running 0 160m 172.16.110.2 192.168.80.111 <none> <none>
//READY为1/1,表示这个Pod中有1个容器
//在对应网段的node节点上操作,可以直接使用浏览器或者curl命令访问
curl 172.16.70.132
//这时在master01节点上查看nginx日志
kubectl logs nginx-dbddb74b8-nf9sk
更多推荐
所有评论(0)