服务器配置
准备三台服务器,分别为master、node1、node2
master配置如下
- 4核,2G,centos 7.x
node1配置如下
- 2核,2G,centos 7.x
node2配置如下
- 2核,2G,centos 7.x
linux环境准备
#以下步骤若为特殊说明代表需要在master、node1、node2中都需要执行
#关闭防火墙(临时或永久)
systemctl disable firewalld
systemctl stop firewalld
#关闭selinux(临时或永久)
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
#关闭swap分区(临时或永久)
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapoff -a
#给服务器起一个别称
hostnamectl set-hostname <hostname>
#在master(主节点)中添加hosts,node节点不需要
cat >> /etc/hosts << EOF
192.168.140.10 master
192.168.140.11 node1
192.168.140.12 node2
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
安装kubeadm、kubectl、kubelet
#以下步骤若为特殊说明代表需要在master、node1、node2中都需要执行
#安装docker(已安装docker可以跳过)
yum -y install docker
# 启动docker(已安装docker可以跳过)
systemctl start docker
# 安装阿里云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、kubectl、kubelet
yum -y install kubeadm-1.18.0 kubectl-1.18.0 kubelet-1.18.0
systemctl enable kubelet
部署kubernetes Master节点
#以下步骤若为特殊说明代表需要在master、node1、node2中都需要执行
#部署kubernetes Master节点(master中执行)
kubeadm init
--apiserver-advertise-address=192.168.140.10 #自己master主机的地址
--image-repository registry.aliyuncs.com/google_containers #使用阿里云镜像拉
--kubernetes-version v1.18.0
--service-cidr=10.96.0.0/12 #ip随便写不冲突就好
--pod-network-cidr=10.244.0.0/16 #ip随便写不冲突就好
#成功后会返回
Your Kubernetes control-plane has initialized successfully! #代表安装成功
To start using your cluster, you need to run the following as a regular user:
#复制这个在master中执行
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:
#复制这个在node1、node2中执行,把node加入master
kubeadm join 192.168.140.10:6443 --token d4kxip.ca23crvbor4iti98 \
--discovery-token-ca-cert-hash sha256:748a64101cf69dd9c7fbec977e3ab96556d26aca2c6467401bc60e44e22f6005
部署CNI网络插件
#部署CNI网络插件(master中执行)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#查看pods状态,如果全部为ready则代表成功(master中执行)
kubectl get pods -n kube-system
#查看node状态(master中执行)
kubectl get node #如果node1,node2均为ready状态,代表集群搭建成功
测试kubernetes
#测试kubernetes(master中执行)
kubectl create deployment nginx --image=nginx #创建一个nginx镜像
kubectl expose deployment nginx --port=80 --type=NodePort #映射nginx端口
kubectl get pod,svc #查看端口,通过master、node1、node2的ip+端口进行访问,如果看到nginx界面则代表成功