赞
踩
在开始之前,部署Kubernetes集群的虚拟机需要满足以下几个条件:
mac 上安装虚拟机软件:VMware Fusion 12
centos7镜像:CentOS-7-x86_64-DVD-2207-02.iso
三台centos7虚拟机(配置静态 IP,防止 IP 的变化)
节点 | ip地址 |
master | 192.168.2.128 |
work node1 | 192.168.2.129 |
work node2 | 192.168.2.130 |
- 1 # 关闭防火墙
- 2 systemctl stop firewalld
- 3 systemctl disable firewalld
- 4
- 5 # 关闭selinux
- 6 # 永久关闭
- 7 sed -i 's/enforcing/disabled/' /etc/selinux/config
- 8 # 临时关闭
- 9 setenforce 0
- 10
- 11 # 关闭swap
- 12 # 临时
- 13 swapoff -a
- 14 # 永久关闭
- 15 sed -ri 's/.*swap.*/#&/' /etc/fstab
- 16 #修改主机名并且做域名解析
- 17 # 根据规划设置主机名【master节点上操作】
- 18 hostnamectl set-hostname k8smaster
- 19 # 根据规划设置主机名【node1节点操作】
- 20 hostnamectl set-hostname k8snode1
- 21 # 根据规划设置主机名【node2节点操作】
- 22 hostnamectl set-hostname k8snode2
- 23
- 24 # 在master添加hosts
- 25 cat >> /etc/hosts << EOF
- 26 192.168.2.128 k8smaster
- 27 192.168.2.129 k8snode1
- 28 EOF
- 29
- 30
- 31 # 将桥接的IPv4流量传递到iptables的链
- 32 cat > /etc/sysctl.d/k8s.conf << EOF
- 33 net.bridge.bridge-nf-call-ip6tables = 1
- 34 net.bridge.bridge-nf-call-iptables = 1
- 35 EOF
- 36 # 生效
- 37 sysctl --system
- 38
- 39 # 时间同步
- 40 yum install ntpdate -y
- 41 ntpdate time.windows.com
2、安装Docker/kubeadm/kubelet/kubectl
所有节点安装Docker/kubeadm/kubelet ,Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker
2.1、安装Docker
1)配置docker 的阿里yum源
- 1 cat >/etc/yum.repos.d/docker.repo<<EOF
- 2 [docker-ce-edge]
- 3 name=Docker CE Edge - \$basearch
- 4 baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/\$basearch/edge
- 5 enabled=1
- 6 gpgcheck=1
- 7 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
- 8 EOF
2)yum安装docker
- 1 # yum安装
- 2 yum -y install docker-ce
- 3
- 4 # 查看docker版本
- 5 docker --version
- 6
- 7 # 启动docker
- 8 systemctl enable docker
- 9 systemctl start docker
3)配置docker的镜像源
- 1 cat >> /etc/docker/daemon.json << EOF
- 2 {
- 3 "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
- 4 }
- 5 EOF
4)重启docker
1 systemctl restart docker
2.2、添加kubernetes软件yum源
- 1 cat > /etc/yum.repos.d/kubernetes.repo << EOF
- 2 [kubernetes]
- 3 name=Kubernetes
- 4 baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
- 5 enabled=1
- 6 gpgcheck=0
- 7 repo_gpgcheck=0
- 8 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
- 9 EOF
- 1 # 安装kubelet、kubeadm、kubectl,同时指定版本
- 2 yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
- 3 # 设置开机启动
- 4 systemctl enable kubelet
1)在master 节点上执行kubeadm init
- 1 # 参数解释:
- 2 # --image-repository 指定拉取kubernetes所需组件的镜像仓库
- 3 # --pod-network-cidr 设置pod的地址段
- 4 kubeadm init --apiserver-advertise-address=192.168.2.128 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=1.18.0 --pod-network-cidr=10.244.0.0/16
注意参数 --pod-network-cidr这个参数值的设置可以是192.168.0.0,或者是10.244.0.0,这个值会在后面的配置网络插件kube-flannel.yml中需要与之对应,很重要,否则会导致flannel pod启动失败,可能出现如下图错误
2)出现如下提示即代码 kubeadm init 执行成功
3)按照提示执行如下指令
- 1 mkdir -p $HOME/.kube
- 2 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
- 3 sudo chown $(id -u):$(id -g) $HOME/.kube/config
4)查看master节点状态,为NotReady
kubectl get nodes
1)在node1节点下输入kubeadm init 执行成功后输出的指令kubeadm join xxxxx
- 1 kubeadm join 192.168.2.128:6443 --token 2p3p1k.iqoylq7ysjzph6ch \
- 2 --discovery-token-ca-cert-hash sha256:ff50efea2f2a2679e7xxxxxxxxxxxxxxx7770931434ed5b2f9f1ea1
2)上述token具有有效期,到期后需要使用如下指令重新生成token
kubeadm token create --print-join-command
5、部署CNI网络插件Flannel
1)kube-flannel.yml文件下载
每个节点上都需要下载如下镜像到本地仓库!!
2)kube-flannel.yml文件中所用到的docker镜像均需要下载后导入到本地镜像仓库中(每一个节点都需要执行操作!!)
3)执行如下命令生成flannel pod,联通节点之间的网络
- 1 [root@k8smaster ~]# kubectl create -f kube-flannel.yml
- 2 namespace/kube-flannel created
- 3 clusterrole.rbac.authorization.k8s.io/flannel created
- 4 clusterrolebinding.rbac.authorization.k8s.io/flannel created
- 5 serviceaccount/flannel created
- 6 configmap/kube-flannel-cfg created
- 7 daemonset.apps/kube-flannel-ds created
4)执行上述命令后查看pods,nodes的状态(至此,集群搭建完成!)
- kubectl get nodes
- kubectl get pods
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。