k8s部署安装
本次搭建了三台虚拟机(系统centos),一台master节点,两台worker节点
master节点主要是控制平台,不跑任务;worker节点主要用来跑任务,可以扩缩容
所需组件
docker(也可以是其他容器运行时) kubectl 集群命令行交互工具 kubeadm 集群初始化工具 kubelet 管理 Pod 和容器 kube-proxy 网络代理,负责网络相关的工作 安装前的准备(所有节点)
安装前的准备(所有节点)
修改文件,永久设置(每台服务器修改各自主机名,:wq!保存)
vim /etc/hostname
永久关闭(注释掉最后一行 包含swap的一行)
vim /etc/fstab
关闭 SELinux
setenforce 0 sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
关闭防火墙(关闭,自启关闭)
systemctl stop firewalld systemctl disable firewalld
开始安装
添加安装源(所有节点)
# 添加 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 install -y docker # 安装k8s yum install -y kubelet-1.21.5 kubectl-1.21.5 kubeadm-1.21.5
设置开机启动(所有节点)
systemctl enable kubelet systemctl start kubelet systemctl enable docker systemctl start docker
修改 Docker 的 Cgroup Driver
vim /etc/docker/daemon.json # 添加如下启动项参数 { "exec-opts": ["native.cgroupdriver=systemd"] } # 重启生效 systemctl daemon-reload systemctl restart docker
用kubeadm初始化集群(仅在主节点跑)
kubeadm init --kubernetes-version=v1.21.5 --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
如果安装失败,需要重新安装,使用这个命令重置(所有节点,不论主节点创建失败还是从节点加入失败)
# 重置命令 kubeadm reset # 清理cni环境配置 rm -rf /etc/cni/net.d ipvsadm --clear
如果忘记了保存kubeadm join ,可以用下面命令重新获取
kubeadm token create --print-join-command
复制授权文件并修改端口,让 kubectl 可以有权限访问集群
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config
修改kube-scheduler.yaml文件,将 - --port= 修改为 - --port=10251,否则kubectl命令会报错
vim /etc/kubernetes/manifests/kube-scheduler.yaml
修改kube-controller-manager.yaml文件,将 - --port= 0注释掉,否则Unhealthy
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
工作节点加入集群(只在工作节点运行),运行上面保存的kubeadm join命令行
kubeadm join 192.168.217.185:6443 --token vi2k0d.zrafww05zwpl0dt0 \ --discovery-token-ca-cert-hash sha256:3b4bdd10e8639785f9e6432809f7b5e103b66014dc217d34c1b1f089219cafd7
安装网络插件flannel(主节点跑),如果不安装 node 是 NotReady 状态
# 国内一般访问不到这个资源 kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml # 这里我给出kube-flannel.yml的文件,大家直接运行命令就可以安装 kubectl create -f kube-flannel.yaml
查看是否安装成功,查看各节点是否运行起来(全是ready)
kubectl get nodes
查看健康(全是healthy)
kubectl get cs
查看各组件运行状态(都是running)
kubectl get pod --all-namespaces -o wide
至此,恭喜你,k8s安装成功。
附录:kube-flannel.yaml
--- 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