系统 |
ip |
主机名 |
配置 |
centos 7.7 |
192.168.124.7 |
k8s-master |
4核4G |
centos 7.7 |
192.168.128.3 |
k8s-node01 |
4核4G |
centos 7.7 |
192.168.128.8 |
k8s-node02 |
4核4G |
如果各个主机启用了防火墙,需要开放Kubernetes各个组件所需要的端口,可以查看Installing kubeadm中的”Check required ports”一节。 这里简单起见在各节点禁用防火墙:
systemctl stop firewalld
systemctl disable firewalld
# 临时禁用
setenforce 0
# 永久禁用
vim /etc/selinux/config # 或者修改/etc/sysconfig/selinux
SELINUX=disabled
cat </etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
# 临时关闭
swapoff -a
修改/etc/fstab 文件,注释掉 SWAP 的自动挂载(永久关闭swap,重启后生效)
vi /etc/fstab
# 注释掉以下字段/dev/mapper/cl-swap swap swap defaults 0 0
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
然后执行:
yum makecache fast
接下来直接安装最新的Docker版本:
yum install docker-ce
开启docker:
systemctl start docker&&systemctl enable docker
hostnamectl set-hostname k8s-master
注意:主机名不能带下划线,只能带中划线 否则安装k8s会报错
在各节点安装kubeadm,kubelet,kubectl
cat </etc/yum.repos.d/kubernetes.repo
[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 makecache
目前最新版本是:1.18.2
yum install -y kubelet kubeadm kubectl kubernetes-cni
systemctl enable kubelet && systemctl start kubelet
以上,就是master和node都需要操作的部分。
kubeadm init --kubernetes-version=1.18.1 \--apiserver-advertise-address=192.168.124.7 \--image-repository registry.aliyuncs.com/google_containers \--service-cidr=10.1.0.0/16 \--pod-network-cidr=10.244.0.0/16
注意修改apiserver-advertise-address为master节点ip
参数解释:
–kubernetes-version: 用于指定k8s版本;
–apiserver-advertise-address:用于指定kube-apiserver监听的ip地址,就是 master本机IP地址。
–pod-network-cidr:用于指定Pod的网络范围; 10.244.0.0/16
–service-cidr:用于指定SVC的网络范围;
–image-repository: 指定阿里云镜像仓库地址
这一步很关键,由于kubeadm 默认从官网k8s.grc.io下载所需镜像,国内无法访问,因此需要通过–image-repository指定阿里云镜像仓库地址
集群初始化成功后返回如下信息: 记录生成的最后部分内容,此内容需要在其它节点加入Kubernetes集群时执行。 输出如下:
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.124.7:6443 --token 8k3uq2.my3wljoai0zleev4 \
--discovery-token-ca-cert-hash sha256:e48902fd88184c9733d4eabc8719b0a50eadae8433e1dbc8326884cebff27ddc
注意保持好kubeadm join,后面会用到的。
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
下载镜像
docker pull registry.cn-hangzhou.aliyuncs.com/daoh/flannel:v0.12.0-amd64
为镜像打tag,保持和yaml文件一样。
docker tag registry.cn-hangzhou.aliyuncs.com/daoh/flannel:v0.12.0-amd64 quay.io/coreos/flannel:v0.12.0-amd64
mkdir -p /etc/cni/net.d/
vi /etc/cni/net.d/10-flannel.conf
{"name":"cbr0","type":"flannel","delegate": {"isDefaultGateway": true}}
mkdir /usr/share/oci-umount/oci-umount.d -p
mkdir /run/flannel/
mkdir k8s
cd k8s
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
如果在node 节点上面执行上面的操作可能会报错
这是需要把master 节点上面的/etc/kubernetes/admin.conf 这个文件拷贝到node节点的相同路径下面,执行下面的命令。
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
注意:安装flannel 上面的这些步骤,也需要在node节点执行。
执行上面报错 “正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|::|:443... 失败:拒绝连接。”
sudo vim /etc/hosts
添加199.232.68.133 raw.githubusercontent.com 再重复执行
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
等待几分钟,确保所有的Pod都处于Running状态
[root@k8s-master home]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-7ff77c879f-97p2d 1/1 Running 0 108m
coredns-7ff77c879f-r5n5c 1/1 Running 0 108m
etcd-k8s-master 1/1 Running 0 108m
kube-apiserver-k8s-master 1/1 Running 0 108m
kube-controller-manager-k8s-master 1/1 Running 0 108m
kube-proxy-6qrg2 1/1 Running 0 77m
kube-proxy-fj9qm 1/1 Running 0 45m
kube-proxy-g7vz4 1/1 Running 0 108m
kube-scheduler-k8s-master 1/1 Running 0 108m
注意:coredns容器的网段是10.244.0.0/16
systemctl enable kubelet
必须退出一次,再次登录,就可以了
请查看上文中的准备工作,确保都执行了!!!
修改主机名部分,改为k8s-node01
hostnamectl set-hostname k8s-node01
登录到node节点,确保已经安装了docker和kubeadm,kubelet,kubectl
kubeadm join 192.168.124.7:6443 --token g74uzg.mkba7jzzv6s01f3c \
--discovery-token-ca-cert-hash sha256:4b77989dea5293ca765159c985f26eaed4bda103af912baac9e1c5f26a315e64
systemctl enable kubelet
kubectl get nodes
https://cloud.tencent.com/developer/article/1621722