安装要求,准备环境
#系统centos7.6 #硬件:2个CPU,2G内存 #可以访问外网,禁用swap#提前安装docker环境
准备三台服务器systemctl stop firewalldsystemctl disable firewalld
1.2 关闭selinux和关闭swapsed -i 's/enforcing/disabled' /etc/selinux/configsetenforce 0swapoff -a #临时关闭,永久关闭需要写入fatab
1.3 修改主机名,并写入三台服务器的host中hostnamectl set-hostname K8S-master 102hostnamectl set-hostname K8S-node01 103hostnamectl set-hostname K8S-mast02 104cat >> /etc/hosts << EOF192.168.106.102 K8S-master192.168.106.103 K8S-node01192.168.106.104 K8S-mast02
1.4 将桥接的IPV4流量传递到iptables的链:cat > /etc/sysctl.d/k8s.conf << EOFnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1EOFsysctl --system #配置生效
1.5 同步时间ntpdate time.windows.com#如果时区不对执行下面命令,然后在同步cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
1.6 配置阿里源,下载相应的软件包cat <<EOF > /etc/yum.repos.d/kubernetes.repo[kubernetes]name=Kubernetesbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/enabled=1gpgcheck=1repo_gpgcheck=1gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOFyum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0#kubectl-1.18.0命令行管理工具,kubeadm-1.18.0是引导K8S集群,kubelet-1.18.0管理容器systemctl enable kubelet
2.0部署kubernetes Master节点(master节点上执行)kubeadm init \--apiserver-advertise-address=192.168.106.102 \ #指定master监听的地址--image-repository registry.aliyuncs.com/google_containers \ #指定下载源--kubernetes-version v1.18.0 \ #指定kubernetes版本--service-cidr=10.96.0.0/12 #设置集群内部的网络--pod-network-cidr=10.244.0.0/16 #设置pod的网络
以下命令是加入master的命令需要复制出来
kubeadm join 192.168.106.102:6443 --token 7w0oxu.drdkjuzirow3dvj7 \ --discovery-token-ca-cert-hash sha256:e30452be8217affa2f11229e45cb2ed9dfa4424c82d5f354d9813ce789f58fdb````bashmkdir -p $HOME/.kubecp -i /etc/kubernetes/admin.conf $HOME/.kube/configchown $(id -u):$(id -g) $HOME/.kube/config
kubectl get node #查看版本
kubectl get pods -n kube-system #查看所有组件
2.1 安装Pod网络插件 wget http://120.78.77.38/file/kube-flannel.yaml #下载镜像,国外源,我已经下载到本地 kubectl apply -f kube-flannel.yaml #原始镜像有问题改成如下图所示的镜像名称
启动镜像,启动完查看镜像,会增加一个flannel镜像。支持多主机容器网络通信的
docker pull lizhenliang/flannel:v0.11.0-amd64 #建议先下载镜像kubectl apply -f kube-flannel.yamlkubectl get pods -n kube-system
如图所示说明K8Spod正常启动
如果之前的环境配置有问题,执行下面命令清理K8S环境
kubectl log kube-flannel-ds-sjs4p -n kube-system -f 启动有问题查看日志详细信息``kubectl delete -f kube-flannel.yaml #删除pod````bashkubeadm reset -fmodprobe -r ipiplsmodrm -rf ~/.kube/rm -rf /etc/kubernetes/rm -rf /etc/systemd/system/kubelet.service.drm -rf /etc/systemd/system/kubelet.servicerm -rf /usr/bin/kube*rm -rf /etc/cnirm -rf /opt/cnirm -rf /var/lib/etcdrm -rf /var/etcd
3. k8s-node节点加入master节点(两台主机分别执行)kubeadm join 192.168.106.102:6443 --token 7w0oxu.drdkjuzirow3dvj7 \ --discovery-token-ca-cert-hash sha256:e30452be8217affa2f11229e45cb2ed9dfa4424c82d5f354d9813ce789f58fdb
kubectl get nodes #master节点运行
至此K8S集群部署完成
4. master节点安装管理页面dashboardwget http://120.78.77.38/file/kubernetes-dashboard.yamlkubectl apply -f kubernetes-dashboard.yaml kubectl get pods -n kubernetes-dashboard #下图位置需要修改
kubectl get pod -n kubernetes-dashboard -o wide #查看pod在哪个节点
https://192.168.106.104:30001 #需要用火狐浏览器打开访问#创建token
kubectl create serviceaccount dashboard-admin -n kube-systemkubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-adminkubectl describe secrets -n kube-system $(kubectl -n kube-system get secret |awk '/dashboard-admin/{pirnt $1}')
创建完成将token复制到页面token后进入web管理页面,可以看到如图所示的页面三个节点
https://192.168.106.104:30001/#/node?namespace=default