本次安装kubernetes版本为1.24.0, 若需其他版本将本文章中涉及的版本变量按需修改即可
配置yum源
[root@master1 ~]# 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
安装docker
yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo yum list docker-ce.x86_64 --showduplicates |sort -r #默认安装最新版 yum -y install docker-ce
修改docker配置
创建/etc/docker/daemon.json,加入下面的内容:
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://ddcy8uhg.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}docker启动:
systemctl start docker
systemctl enable docker
选择安装的版本
yum list kubelet --showduplicates | sort -r 注意: master、node 节点 均需要安装kubeadm kubelet kubectlyum install -y kubelet-1.24.0 kubeadm-1.24.0 kubectl-1.24.0
systemctl enable kubelet
拉取安装所需镜像
#!/bin/bash version="1.24.0" images=`kubeadm config images list --kubernetes-version=${version} |awk -F'/' '{print $NF}'` for imageName in ${images[@]};do docker pull registry.aliyuncs.com/google_containers/$imageName # docker pull gcr.azk8s.cn/google-containers/$imageName docker tag registry.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName # docker rmi gcr.azk8s.cn/google-containers/$imageName done
关闭硬件设置:
关闭swap swapoff -a # 临时 sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久关闭selinux:
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
集群初始化
[root@master ~]# cat install.sh #!/bin/bash kubeadm init --kubernetes-version=v1.24.0 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --apiserver-advertise-address=192.168.96.140 --ignore-preflight-errors=NumCPU --image-repository registry.aliyuncs.com/google_containers 如果runtime是criokubeadm init --kubernetes-version=v1.24.0 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 \ --apiserver-advertise-address=192.168.3.121 --ignore-preflight-errors=NumCPU \ --image-repository registry.aliyuncs.com/google_containers \ --cri-socket unix:///var/run/cri-docker.sock
或
[root@master ~]# cat kubeadm-config.yaml
# api版本
apiVersion: kubeadm.k8s.io/v1beta2
# 资源类型
kind: ClusterConfiguration
# 资源版本
kubernetesVersion: v1.24.0
# 虚拟IP
controlPlaneEndpoint: masterIp:6443
imageRepository: registry.aliyuncs.com/google_containers
apiServer:
# 哪些IP生成证书
certSANs:
- masterIp
networking:
# pod 的网段
podSubnet: 10.244.0.0/16
serviceSubnet: 10.10.0.0/16
---
# api版本
apiVersion: kubeproxy.config.k8s.io/v1alpha1
# 资源类型
kind: KubeProxyConfiguration
# 模式
mode: ipvskubeadm init --config kubeadm-config.yaml
默认节点名称使用主机名命名,若想用ip地址命名:
kubeadm init --config kubeadm-config.yaml --node-name $masterIp
安装网络插件:
本次使用网络插件是flannel ,也可使用calico
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
把node加入到集群
如果token忘了/失效, 重新生成 join 命令 可用来join work节点 kubeadm token create --print-join-command 查看[root@master1 ~]# kubeadm token create --print-join-command
kubeadm join 192.168.3.121:6443 --token 6a4qb1.bfqsvxoburcrfe8f --discovery-token-ca-cert-hash sha256:803d49279922ae293e02b11f9e5a4a6ca86589e3d4cccbe83b57829b176fec8d若node节点加入集群使用ip地址,亦可用 --node-name 参数
kubeadm join 192.168.3.121:6443 --token 6a4qb1.bfqsvxoburcrfe8f --discovery-token-ca-cert-hash sha256:803d49279922ae293e02b11f9e5a4a6ca86589e3d4cccbe83b57829b176fec8d --node-name $nodeIP
如果需要添加master节点
重新生成certificate-key kubeadm init phase upload-certs --upload-certs 1.16 以下的老版本需要用以下命令生成 kubeadm init phase upload-certs --experimental-upload-certs
执行过程:
[root@master1 ~]# kubeadm init phase upload-certs --upload-certs I0728 17:25:02.945115 13362 version.go:255] remote version is much newer: v1.24.0; falling back to: stable-1.24 [upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace [upload-certs] Using certificate key: 189d848109dac424dadcaef814fffb1e5a9e344329f378927431434eadd76f04加入master节点使用ip地址亦可用 --node-name参数:
1.16以下老版本参数使用参数:
--experimental-control-plane --certificate-key
1.16以上版本加入使用参数:
--control-plane --certificate-key
$ kubeadm join 192.168.3.121:6443 --token 6a4qb1.bfqsvxoburcrfe8f --discovery-token-ca-cert-hash sha256:803d49279922ae293e02b11f9e5a4a6ca86589e3d4cccbe83b57829b176fec8d --control-plane --certificate-key 189d848109dac424dadcaef814fffb1e5a9e344329f378927431434eadd76f04 --node-name $nodeIP