Ubuntu22.04部署Kubernetes集群(亲测可用)

本文将使用kubeadm在Ubuntu22.04上部署k8s集群,kubeadm 是官方社区推出的一个用于快速部署kubernetes 集群的工具,用于快速部署Kubernetes 集群。

虚拟机准备

下载ubuntu22.04镜像,使用vmware部署三台ubuntu22.04虚拟机并配置静态ip和主机名,节点配置如下:

主节点 :192.168.0.151 master
从节点1:192.168.0.152 node1
从节点2:192.168.0.153 node2

虚拟机设置

  • 修改为阿里云镜像源

    参考文章ubuntu修改apt为国内镜像源设置下面的

    deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
    

    更新系统软件

    sudo apt update
    sudo apt upgradesudo apt update
    sudo apt upgrade
    
  • 安装net-tools(如果未安装,用不了ifconfig命令)

    sudo apt install net-tools  
    
  • 配置静态ip

    参考链接:https://blog.csdn.net/qq_43347021/article/details/129303786

  • 设置hosts

    方便节点之间相互访问,在每个节点上执行vim /etc/hosts命令,增加下面内容

    192.168.0.151 node1
    192.168.0.152 node2
    192.168.0.153 node3
    
  • 修改主机名

    参考链接:https://blog.csdn.net/qq_43347021/article/details/129304674

  • 关闭selinux

    selinux,这个是用来加强安全性的一个组件,但非常容易出错且难以定位,一般上来装完系统就先给禁用了(我安装ubuntu没有自带这个功能模块,不用管它)

    sestatus  # 查看
    sudo nano /etc/selinux/config # 修改配置
    SELINUX=disabled
    reboot
    
  • 关闭防火墙

    iptables防火墙,会对所有网络流量进行过滤、转发,如果是内网机器一般都会直接关闭,省的影响网络性能,但k8s不能直接关了,k8s是需要用防火墙做ip转发和修改的,当然也看使用的网络模式,如果采用的网络模式不需要防火墙也是可以直接关闭的

    sudo ufw disable # 关闭防火墙
    sudo ufw status # 查看状态,inactive 表示已关闭
    
  • 禁用swap

    当内存不足时,linux会自动使用swap,将部分内存数据存放到磁盘中,这个这样会使性能下降,为了性能考虑推荐关掉

    ## 关闭交换空间
    sudo swapoff -a  
    # 永久禁用。-i 插入修改模式,g 标识符表示全局查找替换,表示注释掉swap的那一行。
    sudo sed -i '/ swap / s/^(.*)$/#1/g' /etc/fstab  
    # 查看禁用状态
    free -h 
    
  • 进行一些内核配置

    # Ubuntu安全软件的限制,可能导致节点处于notReady状态,所以需要关闭
    # 停止安全应用 apparmor
    systemctl stop apparmor
    # 禁用安全限制
    systemctl disable apparmor
    
    # 添加系统配置
    sudo tee /etc/sysctl.d/kubernetes.conf<
    # 重新加载 sysctl
    sudo sysctl --system
    

安装docker

sudo apt install docker.io

安装 Kubeadm

设置镜像源地址

deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main

执行更新

apt-get update

报错NO_PUBKEY

在这里插入图片描述

将上面红框里的字符串复制替换下面命令中的recv-keys 参数并执行

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B53DC80D13EDEF05

再次更新

apt-get update

Ubuntu22.04部署Kubernetes集群(亲测可用)_第1张图片

安装

apt-cache madison kubelet # 查看可安装版本
sudo apt install -y kubelet=1.23.6-00 kubeadm=1.23.6-00 kubectl=1.23.6-00

主节点初始化

查看所需镜像

kubeadm config images list

Ubuntu22.04部署Kubernetes集群(亲测可用)_第2张图片

拉取镜像

kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers

在这里插入图片描述

初始化

kubeadm init \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.23.6 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16

Ubuntu22.04部署Kubernetes集群(亲测可用)_第3张图片

从加入主节点

查询加入节点的命令,在master上执行

kubeadm token create --print-join-command

配置网络

kubectl apply -f kube-flannel.yaml

kube-flannel.yaml文件如下:

vim 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:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
- apiGroups:
  - "networking.k8s.io"
  resources:
  - clustercidrs
  verbs:
  - list
  - watch
---
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: docker.io/flannel/flannel-cni-plugin:v1.1.2
       #image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.2
        command:
        - cp
        args:
        - -f
        - /flannel
        - /opt/cni/bin/flannel
        volumeMounts:
        - name: cni-plugin
          mountPath: /opt/cni/bin
      - name: install-cni
        image: docker.io/flannel/flannel:v0.21.4
       #image: docker.io/rancher/mirrored-flannelcni-flannel:v0.21.4
        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: docker.io/flannel/flannel:v0.21.4
       #image: docker.io/rancher/mirrored-flannelcni-flannel:v0.21.4
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            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

结束

执行上kubectl get nodes命令,status状态为Ready表示成功

在这里插入图片描述

你可能感兴趣的:(容器,Kubernetes,服务器,kubernetes,容器,linux,服务器,云原生)