国内 yum源安装 kubelet 和 kubeadm

发布时间 2023-11-17 17:04:10作者: xiaoganghu

1.设置国内阿里源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
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

2.安装

yum install -y kubelet kubeadm kubectl 

3.需要安装指定版本,可以

yum install -y kubelet-1.23.4 kubeadm-1.23.4 kubectl-1.23.4
  1. 设置开机启动
systemctl enable kubelet && systemctl start kubelet

master 节点初始化

kubeadm config print init-defaults > kubeadm-config.yaml

编辑配置文件

vim kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 192.168.xx.xx #修改成master节点IP
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  imagePullPolicy: IfNotPresent
  name: k8s-master-1 #之前/etc/hosts里设置节点的别名
  taints: null
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers #改成国内源
kind: ClusterConfiguration
kubernetesVersion: 1.23.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
  podSubnet: 10.244.0.0/16
scheduler: {}

预先拉取所需镜像

kubeadm config images pull --config=kubeadm-config.yaml

初始化
加上 tee kubeadm-init.log,方便后续查看 token 和初始化信息

 kubeadm init --config=kubeadm-config.yaml | tee kubeadm-init.log

成功信息

[addons] Applied essential addon: kube-proxy

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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.31.170:6443 --token abcdef.0123456789abcdef \
        --discovery-token-ca-cert-hash sha256:a5d9c8d271fc1b3165fa8bbdcc9b092207a223509b8ae53aa0078d13f67b050f 

按照提示,root 身份简单设置

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
#启动生效
source ~/.bash_profile

master节点安装pod网络

curl  -o  kube-flannel.yml  https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#把yml文件中的所有的quay.io改为quay.mirrors.ustc.edu.cn 
sed  -i  's/quay.io/quay.mirrors.ustc.edu.cn/g'   kube-flannel.yml
#生成 flannel 插件pod
kubectl apply -f kube-flannel.yml
#确认所有的Pod都处于Running状态
kubectl get pod -n kube-system