最详细的k8s安装部署教程

k8s部署安装

文章目录

  • k8s部署安装
    • 所需组件
    • 安装前的准备(所有节点)
    • 开始安装

本次搭建了三台虚拟机(系统centos),一台master节点,两台worker节点

master节点主要是控制平台,不跑任务;worker节点主要用来跑任务,可以扩缩容

所需组件

  • docker(也可以是其他容器运行时)
  • kubectl 集群命令行交互工具
  • kubeadm 集群初始化工具
  • kubelet 管理 Pod 和容器
  • kube-proxy 网络代理,负责网络相关的工作

安装前的准备(所有节点)

  • 设置主机名称

临时设置主机名称

# 每个节点分别设置对应主机名
hostname  master
hostname  node1
hostname  node2

修改文件,永久设置(每台服务器修改各自主机名,:wq!保存)

vim  /etc/hostname
  • 关闭swap

临时关闭

swapoff -a

永久关闭(注释掉最后一行 包含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

开始安装

    1. 添加安装源(所有节点)
# 添加 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/


    1. 安装所需组件(所有节点)
yum install -y docker
yum install -y kubelet-1.22.4 kubectl-1.22.4 kubeadm-1.22.4 

版本太高会出问题

    1. 设置开机启动(所有节点)
systemctl enable kubelet
systemctl start kubelet
systemctl enable docker
systemctl start docker
    1. 修改docker配置(所有节点)

这里网上有几种做法我全部放在这里,我第一种修改后,启动不了;所以采用第二种

第一种:(建议使用阿里云镜像,因为这些安装都是用的阿里云)

# kubernetes 官方推荐 docker 等使用 systemd 作为 cgroupdriver,否则 kubelet 启动不了
cat <<EOF > daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF
mv daemon.json /etc/docker/

# 重启生效
systemctl daemon-reload
systemctl restart docker

第二种:

查看docker详情,是不是 cgroupdriver=systemd

docker info

如果不是,利用下面命令修改(一般不用改可以直接启动),找到修改为systemd
–exec-opt native.cgroupdriver=systemd \

vim /lib/systemd/system/docker.service

添加docekr国内镜像源(建议使用阿里云镜像,因为这些安装都是用的阿里云))

cat <<EOF > daemon.json
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF
mv daemon.json /etc/docker/

重启

# 重启生效
systemctl daemon-reload
systemctl restart docker
    1. 用 kubeadm 初始化集群(仅在主节点跑)
kubeadm init  --kubernetes-version=v1.22.4  --image-repository=registry.aliyuncs.com/google_containers  --pod-network-cidr=10.244.0.0/16

其中–pod-network-cidr=10.244.0.0/16 集群的网关必须配置,要不然后面需要一个一个改节点的配置文件,比较麻烦。

记得把kubeadm join 保存起来,用来从节点加入。

运行成功后,会有后续安装说明,也可以继续往下看

如果安装失败,需要重新安装,使用这个命令重置(所有节点,不论主节点创建失败还是从节点加入失败)

kubeadm reset

如果忘记了保存kubeadm join ,可以用下面命令重新获取

kubeadm token create --print-join-command
    1. 复制授权文件并修改端口

让 kubectl 可以有权限访问集群。

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

如果其他节点需要访问集群,需要从主节点复制这个文件过去其他节点。(初次搭建不推荐复制)

在其他机器上创建 ~/.kube/config 文件也能通过 kubectl 访问到集群。

修改端口:

将 - --port= 修改为 - --port=10251(:wq!)

vim /etc/kubernetes/manifests/kube-scheduler.yaml

否则kubectl命令会报错

    1. 工作节点加入集群(只在工作节点运行)

运行上面保存的kubeadm join命令行

kubeadm join 192.168.48.130:6443 --token xxx --discovery-token-ca-cert-hash xxx

    1. 安装网络插件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.yml

kube-flannel.yml 文件见后面的附录

    1. 查看是否安装成功

其他节点有安装 kubectl 也可以查看

查看各节点是否运行起来(全是ready)

kubectl get nodes

查看健康(全是healthy)

kubectl get cs

查看各组件运行状态(都是running)

kubectl get pod --all-namespaces -o wide

至此,恭喜你,k8s安装成功。

附录:
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

分享一篇最简洁,最快速入门的k8s教程,我也是参考up主的视频搭建的:

文档:[k8s教程文档](kubectl get pod --all-namespaces -o wide)

视频:k8s视频B站

你可能感兴趣的:(k8s,kubernetes,docker,运维)