多master集群架构图

时间必须同步、关闭防火墙、Firewalld、selinux

《二》Kubernetes集群部署-搭建集群

《二》Kubernetes集群部署-搭建集群

1、拷贝master01 中的kubernetes目录到master02上
[root@docker ~]# scp -r /opt/kubernetes/ 192.168.1.16:/opt/

2、启动脚本
[root@docker kubeconfig]# scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service 192.168.1.16:/usr/lib/systemd/system/

3、拷贝master01 中的etcd目录到master02上
[root@docker ~]# scp -r /opt/etcd/ 192.168.1.16:/opt/

4、登录到master02(192.168.1.16)上修改IP配置信息
只需要修改vi /opt/kubernetes/cfg/kube-apiserver 更改为192.168.1.16

5、启动
systemctl start kube-apiserver
systemctl start kube-scheduler
systemctl start kube-controller-manager
systemctl enable kube-apiserver
systemctl enable kube-scheduler
systemctl enable kube-controller-manager

验证:
《二》Kubernetes集群部署-搭建集群

《二》Kubernetes集群部署-搭建集群

Load Balancer 安装nginx/keepalived

192.168.1.20
[root@docker nginx]# vi /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0

[root@docker nginx]# yum install nginx -y
[root@docker nginx]# yum install keepalived -y

192.168.1.26
[root@docker nginx]# vi /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0

[root@docker nginx]# yum install nginx -y
[root@docker nginx]# yum install keepalived -y

修改nginx配置文件
新增:
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.1.13:6443;
    server 192.168.1.16:6443;
}

server {
listen 6443;
proxy_pass k8s-apiserver;

      }

}

《二》Kubernetes集群部署-搭建集群

vi /etc/keepalived/keepalived.conf
[root@docker ~]# cat /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 "/etc/nginx/check_nginx.sh"
}

vrrp_instance VI_1 {
state MASTER
interface 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.1.30/24
}
track_script {
check_nginx
}
}

[root@docker ~]# vi /etc/nginx/check_nginx.sh

count=$(ps -ef |grep nginx |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
systemctl stop keepalived
fi

实验:
2台机都启动 nginx、keepalived服务后,在主节点上会有一个vip(192.168.1.30)
192.168.1.20
echo 'nginx master' > /usr/share/nginx/html/index.html

192.168.1.26
echo 'nginx backup' > /usr/share/nginx/html/index.html

验证:
访问192.168.1.30 ---》nginx master
/etc/nginx/check_nginx.sh
当把主节点的nginx关闭,pkill nginx时,keepalived会每隔1秒检测check_nginx,会执行脚本/etc/nginx/check_nginx.sh,停止掉keepalived服务,这样vip会转移到backup上去,当当把主节点的nginx,keepalived开启,vip又会回到主节点上

node节点设置

现在将node节点(192.168.1.23、192.168.1.24)的apiserver 更改为vip

更改这三个文件(bootstrap.kubeconfig、kube-proxy.kubeconfig、kubelet.kubeconfig)
《二》Kubernetes集群部署-搭建集群

重启
systemctl restart kubelet
systemctl restart kube-proxy

验证:
《二》Kubernetes集群部署-搭建集群

创建pod

[root@docker ~]# kubectl run nginx --image=nginx
结果:
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created

查看:
[root@docker ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-n4dk8 1/1 Running 0 53s

查看日志需要授权才可以
kubectl logs pod-name 无法查看日志,需要执行以下命令授权:
执行:
kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous

查看日志:
[root@docker ~]# kubectl logs nginx-dbddb74b8-n4dk8

验证:
必须在node节点上访问,因为是flannel 网络才能通信
[root@docker cfg]# curl 172.17.41.2

查看日志:
《二》Kubernetes集群部署-搭建集群

部署Web UI(Dashboard)

下载地址:
https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dashboard

《二》Kubernetes集群部署-搭建集群

[root@docker dashboard]# pwd
/data/k8s/dashboard
[root@docker dashboard]# ls
dashboard-configmap.yaml dashboard-rbac.yaml dashboard-service.yaml
dashboard-controller.yaml dashboard-secret.yaml k8s-admin.yaml

执行顺序:
[root@docker dashboard]# kubectl create -f dashboard-rbac.yaml
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created

[root@docker dashboard]# kubectl create -f dashboard-secret.yaml
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-key-holder created

[root@docker dashboard]# kubectl create -f dashboard-configmap.yaml
configmap/kubernetes-dashboard-settings created

[root@docker dashboard]# kubectl create -f dashboard-controller.yaml
serviceaccount/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created

[root@docker dashboard]# kubectl create -f dashboard-service.yaml
service/kubernetes-dashboard created

[root@docker dashboard]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
kubernetes-dashboard-65f974f565-2zg4v 0/1 Running 0 50s

《二》Kubernetes集群部署-搭建集群

获取token
kubectl create -f k8s-admin.yaml
kubectl get secret -n kube-system
[root@docker dashboard]# kubectl describe secret dashboard-admin-token-dmk7t -n kube-system
《二》Kubernetes集群部署-搭建集群

复制这个token到浏览器就可以登录:
《二》Kubernetes集群部署-搭建集群

解决:谷歌浏览器不能访问,因为证书不受信任

[root@docker dashboard]# cat dashboard-cert.sh
cat > dashboard-csr.json << EOF
{
"CN": "Dashboard",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF

K8S_CA=$1
cfssl gencert -ca=$K8S_CA/ca.pem -ca-key=$K8S_CA/ca-key.pem -config=$K8S_CA/ca-config.json -profile=kubernetes dashboard-csr.json | cfssljson -bare dashboard
kubectl delete secret kubernetes-dashboard-certs -n kube-system
kubectl create secret generic kubernetes-dashboard-certs --from-file=./ -n kube-system

#dashboard-controller.yaml 增加证书两行,然后apply
#args:
##PLATFORM-SPECIFIC ARGS HERE
#- --auto-generate-certificates
#- --tls-key-file=dashboard-key.pem
#- --tls-cert-file=dashboard.pem

#------------------------------end-----------------------------------------------------------------------------------------

执行脚本生成证书:
[root@docker dashboard]# pwd
/data/k8s/dashboard

[root@docker dashboard]# bash dashboard-cert.sh /data/k8s/master-ca

《二》Kubernetes集群部署-搭建集群

新增2行信息:
root@docker dashboard]# vi dashboard-controller.yaml

  • --tls-key-file=dashboard-key.pem
  • --tls-cert-file=dashboard.pem

注意不要用tab,不然会报错信息:
《二》Kubernetes集群部署-搭建集群

《二》Kubernetes集群部署-搭建集群

最后执行:
[root@docker dashboard]# kubectl apply -f dashboard-controller.yaml

然后再谷歌浏览器访问:
用node访问
https://192.168.1.23:30001

输入token
《二》Kubernetes集群部署-搭建集群