机器 | IP | OS | 资源 |
---|---|---|---|
master | 172.31.1.100 | CentOS Linux release 7.9.2009 (Core) | 2C4G |
node1 | 172.30.1.100 | CentOS Linux release 7.5.1804 (Core) | 1C2G |
以上环境为个人学习环境,企业生产环境建议最低配置在8C16G master且master高可用,至少2台8C16G的工作节点
systemctl stop firewalld
setenforce 0
# 临时关闭
swapoff -a
# 永久关闭,注释掉swap配置行,重启reboot
vim /etc/fstab
[root@node1 ~]# cat /etc/hosts
172.31.1.100 master
172.30.1.100 node1
[root@master ~]# ping node1
PING node1 (172.30.1.100) 56(84) bytes of data.
64 bytes from node1 (172.30.1.100): icmp_seq=1 ttl=64 time=1.72 ms
64 bytes from node1 (172.30.1.100): icmp_seq=2 ttl=64 time=1.66 ms
[root@node1 ~]# ping master
PING master (172.31.1.100) 56(84) bytes of data.
64 bytes from master (172.31.1.100): icmp_seq=1 ttl=64 time=1.72 ms
64 bytes from master (172.31.1.100): icmp_seq=2 ttl=64 time=1.69 ms
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
# 获取阿里云的docker源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O/etc/yum.repos.d/docker-ce.repo
# yum 安装docker
yum -y install docker-ce
# 设置开机自启动
systemctl enable docker
systemctl start docker
# 查看docker版本
docker --version
Docker version 20.10.7, build f0df350
kubelet
,然后安装命令行工具kubectl
,再安装k8s集群安装工具kubeadm
(还有其他的安装工具,如minikube
等)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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
然后执行安装命令(不指定版本会默认安装最新版本)
yum install -y kubelet kubectl kubeadm
安装完成后,设置开机自启动 kubelet
systemctl enable kubelet
kubeadm init
初始化集群kubeadm init --apiserver-advertise-address=172.31.1.100 --image-repository=registry.aliyuncs.com/google\_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.21.0
当出现如下输出,说明初始化成功!
按要求执行第一个红框中的3条命令后,执行kubectl get nodes
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 13d v1.21.1
到这里master节点就部署完成了。(目前是单节点,master也能做集群,使其高可用)
如果status是Not Ready,后面会安装网络组建,等几分钟就会变成Ready了
kubeadm join
命令加入k8s集群kubeadm join 172.31.1.100:6443 --token 4ex7v0.micj5oc8pd9ldnj8 \\
\--discovery-token-ca-cert-hash sha256:57dd07de79741f66e29ae4371618ffa100e7dcc9272689a92708aef69ef1e157
如果出现如下输出,恭喜你node节点部署成功。
This node has joined the cluster:
Certificate signing request was sent to apiserver and a response was received.
The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
k8s内部资源间的通信,是通过自己的网络组件实现的。而k8s网络组建有多种实现,如flannel
和calico
等。(网络组件安装一种即可,推荐calico。)
wget https://docs.projectcalico.org/v3.10/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
修改IP配置
## 将192.168.0.0/16修改ip地址为10.244.0.0/16
sed -i 's/192.168.0.0/10.244.0.0/g' calico.yaml
安装calico到k8s中
kubectl apply -f calico.yaml
安装完成后,查看集群状态,都是Ready,到此k8s集群安装成功。
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 13d v1.21.1
node1 Ready <none> 13d v1.21.1