文章目录
1. 所需前置环境
- vmware安装好三台虚拟机centos7(k8sMaster、k8snode1、k8snode2)
- 每台虚拟机的硬件配置 2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多
- 集群所有机器可以ping通
- 可以访问外网
2. kubeamd部署方式介绍
kubeamd可以通过两条命令完成kubernetes集群的部署
- kubeadm init创建Master Node
- kubeadm join创建Worker Node
3. 搭建过程
3.1 centos环境初始化
三台虚拟机全部操作环境初始化
- 关闭防火墙
(该方式为永久关闭)
systemctl disable firewalld
- 关闭 selinux
(该方式为永久关闭)
sed -i 's/enforcing/disabled/' /etc/selinux/config
- 关闭swap
(该方式为永久关闭)
sed -ri 's/.*swap.*/#&/' /etc/fstab
- 设置主机名
hostnamectl set-hostname <hostname>
- 在/etc/hosts中添加三台服务器ip的映射关系
cat >> /etc/hosts << EOF
192.168.0.80 k8sMaster
192.168.0.81 k8snode1
192.168.0.82 k8snode2
EOF
- 将桥接的 IPv4 流量传递到 iptables 的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
让上述命令生效
sysctl --system
- 三台虚拟机时间同步
yum install ntpdate -y
ntpdate time.windows.com
- 重启三台虚拟机
3.2 所有节点安装 Docker/kubeadm/kubelet
- 安装docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
开机启动docker
systemctl enable docker && systemctl start docker
docker安装完成版本查看
docker --version
设置docker镜像加速地址
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
- 添加k8s阿里云yum软件源设置仓库地址
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
- 安装 kubeadm,kubelet 和 kubectl
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
开机启动kubectl
systemctl enable kubelet
3.3 部署Kubernetes Master
- 在k8sMaster上执行
kubeadm init --apiserver-advertise-address=192.168.0.80 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
(1)apiserver-advertise-address为k8sMaster的IP
(2)kubernetes-version为对应的版本号
(3)service-cidr、pod-network-cidr参数不太了解
执行成功后:
......
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.0.80:6443 --token q24bkj.6rrn6xwjgvvjm2zp \
--discovery-token-ca-cert-hash sha256:b6ee62587447e5964d61d4fe499149b3067cad889dcd40739cbbd9f242b36733
- 根据提示执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
- 验证
kubectl get nodes
3.4 安装pod网络插件(CNI)
依然是在k8sMaster上操作
kubectl apply –f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
如果能顺利执行,可直接移步3.5
如果kube-flannel.yml不能正常获取,请修改/etc/hosts如下,然后再尝试执行上述命令
199.232.68.133 raw.githubusercontent.com
如果还是不行,可以从网上获取一个kube-flannel.yml上传到虚拟机中去执行
【kube-flannel.yml无法下载问题参考的https://blog.csdn.net/chen_haoren/article/details/108580338】
3.5 加入 Kubernetes Node
向集群添加新节点,在k8snode1和k8snode2上执行kubeadm init 输出的 kubeadm join 命令:
kubeadm join 192.168.0.80:6443 --token q24bkj.6rrn6xwjgvvjm2zp \
--discovery-token-ca-cert-hash sha256:b6ee62587447e5964d61d4fe499149b3067cad889dcd40739cbbd9f242b36733
此时节点查看
[root@k8sMaster ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8smaster Ready master 94m v1.18.0
k8snode1 Ready <none> 83m v1.18.0
k8snode2 Ready <none> 83m v1.18.0
【注意,如果3.4网络插件没有安装好,kubectl get nodes后节点的status是有问题的】
3.6 测试kubernetes集群
在 Kubernetes 集群中创建一个 pod,验证是否正常运行
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
[root@k8sMaster ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-f89759699-f6kvq 1/1 Running 0 50m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 98m
service/nginx NodePort 10.100.72.181 <none> 80:32524/TCP 49m
说明nginx暴露在了32524端口
在windows上使用http://192.168.0.80:32524/访问,返回nginx说明成功!