编辑
2024-05-15
云原生
00
请注意,本文编写于 191 天前,最后修改于 191 天前,其中某些信息可能已经过时。

目录

环境准备
安装docker
部署k8s

环境准备

本文档部署采用 kubeadm 部署方法

系统CPU内存硬盘
centos 78核32G1024G
sh
# 1、关闭防火墙 systemctl stop firewalld systemctl disable firewalld # 2、关闭selinux(临时关闭) setenforce 0 # 3、关闭swap(临时关闭) swapoff -a # 4、设置网桥参数 cat << EOF > /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 # 5、修改主机名 hostnamectl set-hostname teny # 6、添加主机域名信息 echo "10.108.110.60 teny" >> /etc/hosts

安装docker

docker版本可以看自己需求下载对应的版本

docker版本镜像源
26.1.2阿里云
sh
# 1、安装依赖 yum install -y yum-utils device-mapper-persistent-data lvm2 # 2、配置阿里云 yum 源 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 3、下载docker yum -y install docker-ce # 4、创建 /etc/docker/daemon.json 文件 mkdir -p /etc/docker/daemon.json #设置国内阿里云docker源,地址改为自己在阿里云容器镜像服务申请的地址即可。 #设置cgroupdriver为systemd,这步尤为重要,如果不设置,后续k8s部署时会报错 sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://40d9jgnm.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"] } EOF # 5、重启docker systemctl deamon-reload systemctl restart docker systemctl enable docker

部署k8s

部署版本为 v1.27.6

1、配置 k8s 阿里云 yum 源

sh
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=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF

2、安装 kubeadm kubelet kubectl

sh
yum install -y --nogpgcheck kubelet-1.27.6 kubeadm-1.27.6 kubectl-1.27.6
  • kubelet :运行在cluster,负责启动pod管理容器
  • kubeadm :k8s快速构建工具,用于初始化cluster
  • kubectl :k8s命令工具,部署和管理应用,维护组件

3、启动 kubelet

sh
systemctl daemon-reload systemctl start kubelet systemctl enable kubelet

4、拉取init-config 配置并修改配置

  • init-config 主要由 api server、etcd、scheduler、controller-manager、coredns等镜像构成
sh
kubeadm config print init-defaults > init-config.yaml # 修改刚才拉取的 init-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: 1.2.3.4 #修改1 本机ip地址 bindPort: 6443 nodeRegistration: criSocket: /var/run/dockershim.sock imagePullPolicy: IfNotPresent name: master #修改2 主机名 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 #修改3 修改为阿里云地址 kind: ClusterConfiguration kubernetesVersion: 1.27.6 networking: dnsDomain: cluster.local serviceSubnet: 10.96.0.0/12 scheduler: {}

5、拉取 k8s 相关镜像

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

如果拉取失败,可以通过命令列出需要的镜像后逐个拉取

sh
kubeadm config images list --config init-config.yaml

image-20240515173406316


6、修改 /etc/containerd/config.toml 文件

sh
vim /etc/containerd/config.toml # 1、删除这一行 disabled_plugins = ["cri"] # 2、修改 sandbox_image的值为 "registry.aliyuncs.com/google_containers/pause:3.6" sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.6" # 重启 containerd systemctl restart containerd

7、初始化 master 节点

  • 10.106.107.248 修改为你自己的 ip 地址
sh
kubeadm init --apiserver-advertise-address=10.106.107.248 --apiserver-bind-port=6443 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --kubernetes-version=1.27.6 --image-repository registry.aliyuncs.com/google_containers --v=6

如果安装报错终止了

sh
# 重置集群 kubeamd reset # 排错之后再重新执行 init

8、在master节点运行以下三行命令 执行完成后可以通过 kubeadm token list获取token

sh
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config

9、部署网络插件kube-flannel

sh
# 下载 wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml # 应用 kubectl apply -f kube-flannel.yml
  • 如果下载不下来,新建 yaml 手动复杂粘贴
sh
--- kind: Namespace apiVersion: v1 metadata: name: kube-flannel labels: pod-security.kubernetes.io/enforce: privileged --- kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: flannel rules: - apiGroups: - "" resources: - pods verbs: - get - apiGroups: - "" resources: - nodes verbs: - list - watch - apiGroups: - "" resources: - nodes/status verbs: - patch --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: flannel roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: flannel subjects: - kind: ServiceAccount name: flannel namespace: kube-flannel --- apiVersion: v1 kind: ServiceAccount metadata: name: flannel namespace: kube-flannel --- kind: ConfigMap apiVersion: v1 metadata: name: kube-flannel-cfg namespace: kube-flannel labels: tier: node app: flannel data: cni-conf.json: | { "name": "cbr0", "cniVersion": "0.3.1", "plugins": [ { "type": "flannel", "delegate": { "hairpinMode": true, "isDefaultGateway": true } }, { "type": "portmap", "capabilities": { "portMappings": true } } ] } net-conf.json: | { "Network": "10.244.0.0/16", "Backend": { "Type": "vxlan" } } --- apiVersion: apps/v1 kind: DaemonSet metadata: name: kube-flannel-ds namespace: kube-flannel labels: tier: node app: flannel spec: selector: matchLabels: app: flannel template: metadata: labels: tier: node app: flannel spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/os operator: In values: - linux hostNetwork: true priorityClassName: system-node-critical tolerations: - operator: Exists effect: NoSchedule serviceAccountName: flannel initContainers: - name: install-cni-plugin #image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply) image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0 command: - cp args: - -f - /flannel - /opt/cni/bin/flannel volumeMounts: - name: cni-plugin mountPath: /opt/cni/bin - name: install-cni #image: flannelcni/flannel:v0.20.0 for ppc64le and mips64le (dockerhub limitations may apply) image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.0 command: - cp args: - -f - /etc/kube-flannel/cni-conf.json - /etc/cni/net.d/10-flannel.conflist volumeMounts: - name: cni mountPath: /etc/cni/net.d - name: flannel-cfg mountPath: /etc/kube-flannel/ containers: - name: kube-flannel #image: flannelcni/flannel:v0.20.0 for ppc64le and mips64le (dockerhub limitations may apply) image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.0 command: - /opt/bin/flanneld args: - --ip-masq - --kube-subnet-mgr resources: requests: cpu: "100m" memory: "50Mi" limits: cpu: "100m" memory: "50Mi" securityContext: privileged: false capabilities: add: ["NET_ADMIN", "NET_RAW"] env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace - name: EVENT_QUEUE_DEPTH value: "5000" volumeMounts: - name: run mountPath: /run/flannel - name: flannel-cfg mountPath: /etc/kube-flannel/ - name: xtables-lock mountPath: /run/xtables.lock volumes: - name: run hostPath: path: /run/flannel - name: cni-plugin hostPath: path: /opt/cni/bin - name: cni hostPath: path: /etc/cni/net.d - name: flannel-cfg configMap: name: kube-flannel-cfg - name: xtables-lock hostPath: path: /run/xtables.lock type: FileOrCreate

10、移除 master 污点

  • master会被设置污点,无法在master节点调度pod
sh
# 查看污点测列 kubectl describe node|grep -E "Name:|Taints:" # 删除污点 kubectl taint node teny node-role.kubernetes.io/control-plane:NoSchedule-

本文作者:春天y

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!