一. 安装K8s集群
1. 准备工作,2台服务器
① 192.168.5.140 - 做为master节点 # 在该节点运行命令设置主机名: hostnamectl set-hostname master ② 192.168.5.141 - 做为node1节点,在该节点运行命令设置主机名: # 在该节点运行命令设置主机名: hostnamectl set-hostname node1
# 所有节点配置hosts
cat >>/etc/hosts <<EOF 192.168.5.140 master
192.168.5.141 node1 EOF
# 所有节点关闭 SELinux
# 临时关闭 setenforce 0
# 永久关闭 sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
# 所有节点关闭 swap分区
# 临时关闭 swapoff -a
# 永久关闭 sed -ri 's/.*swap.*/#&/' /etc/fstab
# 所有节点关闭防火墙 systemctl stop firewalld systemctl disable firewalld
# 所有节点时间同步 # 安装 ntp yum install -y ntp # 同步时间 ntpdate ntp.aliyun.com # 查看时间 date
# 设置每小时的第一分钟执行一次同步
vi /etc/crontab
# 新增
1 * * * * root /usr/sbin/ntpdate ntp1.aliyun.com && /sbin/hwclock -w
2. 所有节点安装软件源
# 添加k8s安装源 cat <<EOF > kubernetes.repo [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 # mv kubernetes.repo /etc/yum.repos.d/
# 添加 Docker 安装源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 查看k8s可用版本
yum list kubelet --showduplicates | sort -r
# 查看docker可用版本
yum list docker-ce --showduplicates | sort -r
3. 所有节点安装k8s、docker
# 这里指定k8s版本为1.22.9,docker版本为20.10.9
yum install -y kubelet-1.22.9 kubectl-1.22.9 kubeadm-1.22.9 docker-ce-20.10.9 docker-ce-cli-20.10.9
4. 所有节点启动 kubelet、docker,并设置开机启动
systemctl enable kubelet systemctl start kubelet
systemctl enable docker
systemctl start docker
5. 所有节点修改docker配置
# kubernetes 官方推荐 docker 等使用 systemd 作为 cgroupdriver,否则 kubelet 启动不了 cat <<EOF > daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": ["https://ud6340vz.mirror.aliyuncs.com"] } EOF # mv daemon.json /etc/docker/ # 重启 systemctl daemon-reload systemctl restart docker
6. master节点kubeadm初始化
kubeadm init \ --kubernetes-version=1.22.9 \ --apiserver-advertise-address=192.168.5.140 \ --image-repository registry.aliyuncs.com/google_containers \ --pod-network-cidr=10.244.0.0/16
# 如果init失败,使用下面命令重置后再init
kubeadm reset
# 解释 # 指定K8s的版本,刚才安装的 --kubernetes-version=1.22.9 \ # 指定API Server组件的地址 --apiserver-advertise-address=192.168.5.140 \ # 指定阿里的镜像仓库地址,因为管理节点和工作节点的部分组件是封装到kubeadm工具中的,等会启动的时候,它默认会从国外的源下载镜像,所以这里为了加快速度指定为国内源 --image-repository registry.aliyuncs.com/google_containers \ # pod网段,可以选择一个本机网络和service-cidr都没有用到的私网地址段 --pod-network-cidr=10.244.0.0/16
7. 看到下面的内容说明master节点初始化完成
如果关闭了窗口,可以通过下面的命令查看
kubeadm token create --print-join-command
8. 复制授权文件,以便 kubectl 可以有权限访问集群
# 如果其他节点或本地需要访问集群,需要从主节点复制这个文件过去其他节点或本地,这样其他节点或本地执行kubectl命令了 mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config export KUBECONFIG=/etc/kubernetes/admin.conf
9. 将子节点加入集群
# 将上面的内容在所有子节点运行 kubeadm join 192.168.5.140:6443 --token 65tl4q.mbzgy903o25b75g1 --discovery-token-ca-cert-hash sha256:fa7875e9420e44f87cbf702c91f54e6fb85bd01fa77288a32fb64f28ac10978f
10. 安装flannel网络插件,使用提供的 kube-flannel.yml文件,在master节点上执行命令
kubectl apply -f kube-flannel.yml
kube-flannel.yml内容如下
--- 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.1 for ppc64le and mips64le (dockerhub limitations may apply) image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.1 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.1 for ppc64le and mips64le (dockerhub limitations may apply) image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.1 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
11. 查看节点状态和pod就绪状态
kubectl get node kubectl get pods -A kubectl get pods -n kube-system
标签:name,--,CentOS7,AnolisOS7.9,1.22,docker,kube,节点,flannel From: https://www.cnblogs.com/jf8710/p/18230842