您好,登錄后才能下訂單哦!
以下所有操作均在單master群集已完成部署的情況下進行。
所有服務器均保證防火墻常閉,核心功能selinux關閉。
角色 | 地址 | 安裝組件 |
---|---|---|
master | 192.168.142.220 | kube-apiserver kube-controller-manager kube-scheduler etcd |
master02 | 192.168.142.120 | kube-apiserver kube-controller-manager kube-scheduler |
node1 | 192.168.142.136 | kubelet kube-proxy docker flannel etcd |
node2 | 192.168.142.132 | kubelet kube-proxy docker flannel etcd |
nginx1 | 192.168.142.130 | nginx keepalived |
nginx2 | 192.168.142.140 | nginx keepalived |
VIP | 192.168.142.20 | 虛擬地址 |
[root@lb-ma ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx-repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0
EOF
[root@lb-ma ~]# yum install nginx -y
#添加stream模塊實現四層轉發
[root@lb-ma ~]# vim /etc/nginx/nginx.conf
###添加
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.142.220:6443;
server 192.168.142.120:6443;
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
#開啟服務
[root@lb-ma ~]# systemctl start nginx
[root@lb-ma ~]# systemctl enable nginx
[root@lb-ma ~]# yum -y install keepalived
##修改keepalived配置文件
[root@lb-ma ~]# 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
}
vrrp_script check_nginx {
script "/usr/local/nginx/sbin/check_nginx.sh"
}
vrrp_instance VI_1 {
state MASTER #備服務器改為BACKUP
interface ens33 #監控ens33網卡
virtual_router_id 51 # VRRP 路由 ID實例,每個實例是唯一的
priority 100 # 優先級,備服務器設置 90
advert_int 1 # 指定VRRP 心跳包通告間隔時間,默認1秒
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.142.20/24 #VIP地址
}
track_script {
check_nginx
}
}
一旦nginx處于down狀態,將會自動關閉keeplived
[root@lb-ma ~]# mkdir -p /usr/local/nginx/sbin/
[root@lb-ma ~]# vim /usr/local/nginx/sbin/check_nginx.sh
##手動進行編寫
count=$(ps -ef |grep nginx |egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
systemctl stop keepalived
fi
[root@lb-ma ~]# chmod +x /usr/local/nginx/sbin/check_nginx.sh
#開啟keeplived
[root@lb-ma ~]# systemctl start keepalived
[root@lb-ma ~]# systemctl enable keepalived
此時,前面的負載均衡已經配置完畢,但是并不能起到實際的作用,原因就是后方node節點中負責進行身份識別的kubeconfig文件中的地址沒有改變,無法識別。
[root@node1 ~]# vim /opt/kubernetes/cfg/bootstrap.kubeconfig
[root@node1 ~]# vim /opt/kubernetes/cfg/kube-proxy.kubeconfig
[root@node1 ~]# vim /opt/kubernetes/cfg/kubelet.kubeconfig
##三個文件全部改為
server: https://192.168.142.20:6443 #指向VIP地址
[root@node1 ~]# systemctl restart kubelet
[root@node1 ~]# systemctl restart kube-proxy
[root@master ~]# kubectl run nginx --image=nginx
##建立pod
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created
#查看建立打的pod
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-7tdvp 1/1 Running 0 21s
此時,pod只能進行簡單的查看,一旦查看日志會報錯。為了解決這個問題,可采用下面的辦法解決。
#注意日志問題
[root@master ~]# kubectl logs nginx-dbddb74b8-7tdvp
Error from server (Forbidden): Forbidden (user=system:anonymous, verb=get, resource=nodes, subresource=proxy) ( pods/log nginx-dbddb74b8-7tdvp)
###解決辦法:
[root@master ~]# kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
clusterrolebinding.rbac.authorization.k8s.io/cluster-system-anonymous created
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。