k8s-master1 192.168.43.210 内存-4G,CPU-2核,硬盘-40G
k8s-node1 192.168.43.211 内存-4G,CPU-2核,硬盘-40G
k8s-node2 192.168.43.212 内存-4G,CPU-2核,硬盘-40G
修改主机名
# master节点,node节点省略
[root@k8s-master1 ~]# vi /etc/hostname
k8s-master1
修改主机名于ip对应关系
[root@k8s-master1 ~]# vi /etc/hosts
192.168.43.210 k8s-master1
192.168.43.211 k8s-node1
192.168.43.212 k8s-node2
关闭防火墙
systemctl stop firewalld # 关闭服务
systemctl disable firewalld # 禁用服务
关闭selinux
修改/etc/selinux/config, 设置SELINUX=disabled (需重启后生效)
[root@k8s-master1 ~]# sestatus # 查看SELinux状态
SELinux status: disabled
关闭swap
编辑/etc/fstab, 将swap注释掉.,重启生效
将桥接的IPv4流量传递到iptables的链
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
重启机器
[root@k8s-master1 ~]# reboot
安装docker
docker 版本:18.06.1
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
systemctl enable docker && systemctl start docker
可以docker镜像源配置为阿里云,提高网络速度,修改/etc/docker/daemon.json为如下内容:
registry-mirrors 替换为自己的阿里云镜像加速地址
{
"registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
添加阿里的kubernetes源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
安装kubeadm、kubelet、kubectl 版本1.19.0
yum install -y kubelet-1.19.0 kubeadm-1.19.0 kubectl-1.19.0
systemctl enable kubelet && systemctl start kubelet
再次强调以上的操作在所有节点都需要进行
初始化
kubeadm init --apiserver-advertise-address=192.168.43.210 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version v1.19.0 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
执行完上面的操作会得到下面的内容,保存最后两行,后期有用
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.43.210:6443 --token u754lh.r02z0ikyd2fvl2aj \
--discovery-token-ca-cert-hash sha256:d0aacb2faf89281842905f9b9072ce85826d545e2d46708218d661227218f08f
根据这些提示继续操作
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
安装pod网络插件(这里选择flannel)
#通过官方的yaml文件拉取镜像(这个方式比较慢,而且这个网址貌似失效了)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
#以下操作可加快速度
浏览器打开[https://github.com/mrlxxx/kube-flannel.yml],复制内容到 kube-flannel.yml
[root@k8s-master1 ~]# grep image kube-flannel.yml #查看yaml中定义的需要下载的镜像
image: quay.io/coreos/flannel:v0.12.0-amd64
image: quay.io/coreos/flannel:v0.12.0-amd64
image: quay.io/coreos/flannel:v0.12.0-arm64
image: quay.io/coreos/flannel:v0.12.0-arm64
image: quay.io/coreos/flannel:v0.12.0-arm
image: quay.io/coreos/flannel:v0.12.0-arm
image: quay.io/coreos/flannel:v0.12.0-ppc64le
image: quay.io/coreos/flannel:v0.12.0-ppc64le
image: quay.io/coreos/flannel:v0.12.0-s390x
image: quay.io/coreos/flannel:v0.12.0-s390x
#手动把镜像pull下来
docker pull quay.io/coreos/flannel:v0.12.0-amd64
docker pull quay.io/coreos/flannel:v0.12.0-arm
docker pull quay.io/coreos/flannel:v0.12.0-ppc64le
docker pull quay.io/coreos/flannel:v0.12.0-s390x
#查看镜是否拉取成功
docker images | grep coreos
#配置flannl
kubectl apply -f kube-flannel.yml
在node节点上操作,(准备好在master上执行kubeadm init输出的kubeadm join…的指令)
kubeadm join 192.168.43.210:6443 --token u754lh.r02z0ikyd2fvl2aj \
--discovery-token-ca-cert-hash sha256:d0aacb2faf89281842905f9b9072ce85826d545e2d46708218d661227218f08f
添加完毕完毕后,在master上查看节点状态
[root@k8s-master1 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master1 Ready control-plane,master 10d v1.21.0
k8s-node1 Ready <none> 10d v1.21.0
k8s-node2 Ready <none> 10d v1.21.0