一、安装要求
部署kubernetes需要满足一下几个条件:
1、三台虚拟机,centos7X系统,master:192.168.1.7 node1:192.168.1.8 node2:192.168.1.9
2、硬件配置cpu2核,内存2G以上,存储30G以上
3、所有主机可以访问公网
4、禁止swap分区
二、准备环境
1、关闭防火墙
#systemctl stop firewalld&&systemctl disable firewalld
2、将selinux文件权限系统调整为disabled状态
#vi /etc/selinux/config #sed -i 's/enforcing/disabled/' /etc/selinux/config #setenforce 0
3、关闭swap
#swapoff -a
4、在master节点上增加主机名称解析
#vi /etc/hosts 192.168.1.7 master 192.168.1.8 node1 192.168.1.9 node2
5、将桥接ipv4流量传递到iptables链路
临时修改 #cat << EOF > /etc/sysctl.d/k8s.conf > net.bridge.bridge-cf-call-ip6tables = 1 > net.bridge.bridge-cf-call-iptables = 1 > EOF #sysctl --system 永久修改 #vi /usr/lib/sysctl.d/00-system.conf
#sysctl --system #systemctl daemon-reload
三、所有节点安装docker/kubeadm/kubelet/kubectl
1、安装docker
#wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #yum -y install docker-ce-18.06.1.ce-3.el7 #systemctl enable docker&&systemctl start docker
2、添加阿里云kubernetes yum源
#cat << EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg EOF #yum repolist
3、所有节点安装kubeadm/kubelet/kubelet
#yum -y install kubeadm-1.15.0 kubelet-1.15.0 kubectl-1.15.0 #systemctl enable kubelet
四、部署kubernetes master
1、初始化kubeadm init
#date //查看服务器时间是否与互联网一致 #ntpdate time.windows.com //同步互联网时间 #yum -y install chrony #systemctl enable chrony&&systemctl start chrony #kubeadm init --apiserver-advertise-address=192.168.1.7 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.0 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
2、建立kubernetes 用户 //这里我们使用root用户
#mkdir -p $HOME/.kube #cp -i /etc/kubernetes/admin.conf $HOME/.kube/config #chown $(id -u):$(id -g) $HOME/.kube/config #kubectl get node
五、部署pod 网络插件flannel
#curl -O https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml #kubectl apply -f kube-flannel.yml #docker pull lizhenliang/flannel:v0.11.0-amd64 #kubectl get pods -n kube-system //查看kube-system空间中的pod状态
#kubectl get node //查看master节点的状态
六、部署node节点,join到master
1、两个node节点下载 flannel网络插件
#kubeadm join 192.168.1.7:6443 --token 3q3jqh.r2cfnltsu0lplnae --discovery-token-ca-cert-hash sha256:e70e5fe8ad1b55548928a02839776f42feed243cf51c956a2700996c015e7d4d
2、master节点上查看node加入状态
#kubectl get node
七、测试kubernetes集群
1、在kubernetes 建立一个pod,验证是否可以正常工作
#kubectl create deployment nginx --image=nginx //在吗master节点上创建一个deployment控制器,镜像为nginx #kubectl expose deployment nginx --port=80 --type=NodePort //将nginx应用端口80暴露出来方便访问,以节点ip形式登陆 #kubectl get pod,svc //查看pod状态
2、将nginx pod 扩容为三个副本
#kubectl scale deployment nginx --replicas=3 //将nginx pod 扩容为三个副本,并发性变为三倍,三个pod提供服务
八、部署kubernetes dashboard UI界面
1、在node1和node2节点上下载dashboard镜像
#docker pull lizhenliang/kubernetes-dashboard-amd64:v1.10.1
2、master节点上下载dashboard yaml
#wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml #vim kubernetes-dashboard.yaml
1)在deployment模块中,修改镜像仓库地址
2)在service模块中增加type和端口号,只有node节点上可以访问
注意格式和字符大小写
3)修改后,执行命令生效
#kubectl apply -f kubernetes-dashboard.yaml
#kubectl get pods -n kube-system //查看命名空间,发现dashboard已经运行
#kubectl get pods,svc -n kube-system //查看dashboard pod 端口号
3、输入node节点IP+30001端口号,在浏览器中访问,默认启用是https访问
https://$node ip+30001
九、为k8s UI界面创建账号
1、应用层面账号绑定,创建serviceaccount,绑定默认admin管理员集群角色,进程访问api时用到
#kubectl create serviceaccount dashboard-admin -n kube-system
2、集群角色绑定dashboard-admin,超级管理员权限
#kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
3、获取kogen,输入令牌登陆UI界面
#kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tOXhrZjUiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiMTE4ZTVlNTMtMzkwOC00NmUyLWIwZWEtNmNmMTUzMzExZmRjIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.yoaG8zJks4CzF068D_OKETMo-r4iJR76_RKPr95sPvLyP6kODH7MUSLT0574U5XW0uDkE95zZdVSTrvmHl-WF5qTgqjP2-9LweVP1EGaZKESzd15ICHa9SBlnP_QlwcxPo5F3gCKKBkU-XNtJik1B2-u4_pxdFeHPibzmjBzEEEf3DDl_nX9-OZi0tmmeLwnmggYb2u71Pbs_q8cInX0Y3pDoculuhAymgfnEj7j4QrfqmKl_M2i-He2F_wjCCljbkxzmjdiJCwMLdNtOik212jUrKnmxVY7DuNDASx5m-5WPWDqOSjxeSrHLupctCi_uGPihIc_TfdSOq1CpG2seQ
4、输入令牌登陆成功