1,从 master01 节点上拷贝证书文件、各master组件的配置文件和服务管理文件到 master02 节点
scp -r /opt/etcd/ [email protected]:/opt/
scp -r /opt/kubernetes/ [email protected]:/opt
scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}[email protected]:/usr/lib/systemd/system/
2,修改配置文件kube-apiserver中的IP
vim /opt/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.226.70:2379,https://192.168.226.40:2379,https://192.168.226.50:2379 \
--bind-address=192.168.226.80 \ #修改为本master节点的IP
--secure-port=6443 \
--advertise-address=192.168.226.80 \ #修改
3,在 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
4,查看node节点状态
ln -s /opt/kubernetes/bin/* /usr/local/bin/ #路径优化
kubectl get nodes
kubectl get nodes -o wide #-o=wide:输出额外信息;对于Pod,将输出Pod所在的Node名
配置load balancer集群双机热备负载均衡(nginx实现负载均衡,keepalived实现双机热备)
,配置nginx的官方在线yum源,配置本地nginx的yum源
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
EOF
yum install nginx -y #安装nginx
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.226.70:6443; #配置upstream地址池,供反向代理使用
server 192.168.226.80:6443;
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
http {
......
//检查配置文件语法
nginx -t
//启动nginx服务,查看已监听6443端口
systemctl start nginx
systemctl enable nginx
netstat -natp | grep nginx
yum install keepalived -y #yum安装Keepalived服务
1,修改keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
# 接收邮件地址
notification_email {
[email protected]
[email protected]
[email protected]
}
# 邮件发送地址
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_MASTER #nginx01节点的为 NGINX_MASTER,nginx02节点的为 NGINX_BACKUP
}
#添加一个周期性执行的脚本
vrrp_script check_nginx {
script "/etc/nginx/check_nginx.sh" #指定检查nginx存活的脚本路径
}
vrrp_instance VI_1 {
state MASTER #nginx01节点的为 MASTER,nginx02节点的为 BACKUP
interface ens33 #指定网卡名称 ens33
virtual_router_id 51 #指定vrid,两个节点要一致
priority 100 #nginx01节点的为 100,nginx02节点的为 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.226.10/24 #指定 VIP
}
track_script {
check_nginx #指定vrrp_script配置的脚本
}
}
3,创建nginx状态检查脚本
vim /etc/nginx/check_nginx.sh
#!/bin/bash
#egrep -cv "grep|$$" 用于过滤掉包含grep 或者 $$ 表示的当前Shell进程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 #启动脚本
4,启动keepalived服务(一定要先启动了nginx服务,再启动keepalived服务)
systemctl start keepalived
systemctl enable keepalived
5,修改node节点上的bootstrap.kubeconfig,kubelet.kubeconfig配置文件为VIP
cd /opt/kubernetes/cfg/
vim bootstrap.kubeconfig
server: https://192.168.226.10:6443
vim kubelet.kubeconfig
server: https://192.168.226.10:6443
vim kube-proxy.kubeconfig
server: https://192.168.226.10:6443
6,重启kubelet和kube-proxy服务
systemctl restart kubelet.service
systemctl restart kube-proxy.service
1,测试创建pod
#创建一个pod
kubectl run nginx --image=nginx
#查看这pods,发现在创建中
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-gkgh7 0/1 ContainerCreating 0 12s
再次发现已经ok了
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-gkgh7 1/1 Running 0 27s
#查看更详细的信息,如在那个node上,对外的IP等。
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-dbddb74b8-gkgh7 1/1 Running 0 109s 172.17.97.2 192.168.226.40 <none>
#我们去访问这个pod的日志发现不能访问。
kubectl logs nginx-dbddb74b8-gkgh7
Error from server (Forbidden): Forbidden (user=system:anonymous, verb=get, resource=nodes, subresource=proxy) ( pods/log nginx-dbddb74b8-gkgh7)server (Forbidden): Forbidden (user=system:anonymous, verb=get, resource=nodes, subresource=proxy) ( nginx-dbddb74b8-nf9sk)
#在master01节点上,将cluster-admin角色授予用户system:anonymous
kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
clusterrolebinding.rbac.authorization.k8s.io/cluster-system-anonymous created
#再次查看nginx日志
kubectl logs nginx-dbddb74b8-gkgh7
就可以查看日志文件了。
在nginx01节点查看nginx服务的端口有哪些
netstat -natp | grep nginx
可以看到,在监听我们的6443和80端口,以及我们的由虚拟IP10地址监听的6443端口
现在我们关闭这个节点的nginx服务,验证一下Keepalived服务是否正常。
systemctl stop nginx
systemctl start nginx
断掉服务后我们再去nginx02节点上去查询虚拟ip是否漂移到了这个节点,并查看nginx监听的服务
可以看到虚拟ip已经漂移到了我们的100主机