小哥之哥 小哥之哥
首页
    • Prometheus
    • Kubertenes
    • Docker
    • MySQL
  • Go
  • Python
  • Vue
  • Jenkins
  • ELK
  • LDAP
  • 随笔
  • 最佳实践
  • 博客搭建
  • 问题杂谈
关于
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

小哥之哥

运维扫地僧
首页
    • Prometheus
    • Kubertenes
    • Docker
    • MySQL
  • Go
  • Python
  • Vue
  • Jenkins
  • ELK
  • LDAP
  • 随笔
  • 最佳实践
  • 博客搭建
  • 问题杂谈
关于
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Kubertenes

    • 安装部署

      • kubeadmin-1.26部署
      • 技术杂谈

      • 最佳实践

    • Prometheus

    • Docker

    • 数据库

    • 运维利器

    • 运维
    • Kubertenes
    • 安装部署
    tchua
    2023-02-01
    目录

    kubeadmin-1.26部署

    # kubeadmin 1.26部署


    # 1.1 系统初始化
    # 修改主机名
    hostnamectl set-hostname k8s-master01
    hostnamectl set-hostname k8s-node01
    hostnamectl set-hostname k8s-node02
    # 禁用防火墙selinux
    setenforce 0
    sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
    getenforce
    # br_netfilter是否加载
    lsmod | grep br_netfilter 
    # ## 如果没有加载则直接运行以下
    sudo modprobe br_netfilter
    
    # 配置iptables流量桥接
    cat <<EOF | tee /etc/modules-load.d/k8s.conf
    overlay
    br_netfilter
    EOF
    
    # 配置内核参数
    cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
    net.bridge.bridge-nf-call-iptables  = 1
    net.bridge.bridge-nf-call-ip6tables = 1
    net.ipv4.ip_forward                 = 1
    vm.swappiness=0
    EOF
    
    sudo sysctl --system
    
    # 安装ipvs
    cat > /etc/sysconfig/modules/ipvs.modules <<EOF
    #!/bin/bash
    modprobe -- ip_vs
    modprobe -- ip_vs_rr
    modprobe -- ip_vs_wrr
    modprobe -- ip_vs_sh
    modprobe -- nf_conntrack_ipv4
    EOF
    chmod 755 /etc/sysconfig/modules/ipvs.modules 
    bash /etc/sysconfig/modules/ipvs.modules 
    lsmod | grep -e ip_vs -e nf_conntrack_ipv4
    
    yum install ipset ipvsadm -y
    
    
    
    # 禁用swap
    swapoff -a
    sed -i '/ swap / s/^/# /g' /etc/fstab
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    # 1.2 安装containerd

    官方文档:点击直达 (opens new window)

    # yum 安装
    wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    yum install -y containerd.io
    # containerd 配置
    mkdir -p /etc/containerd
    containerd config default | tee /etc/containerd/config.toml
    
    cat <<EOF | tee /etc/crictl.yaml
    runtime-endpoint: "unix:///run/containerd/containerd.sock"
    image-endpoint: "unix:///run/containerd/containerd.sock"
    timeout: 10
    debug: false
    pull-image-on-create: false
    disable-pull-on-run: false
    EOF
    # 配置systemd cgroup 驱动
    sed -i "s#k8s.gcr.io#registry.aliyuncs.com/google_containers#g"  /etc/containerd/config.toml
    sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
    sed -i "s#registry.k8s.io#registry.aliyuncs.com/google_containers#g"  /etc/containerd/config.toml
    
    # 启动
    systemctl daemon-reload
    systemctl enable containerd
    systemctl restart containerd
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    镜像加速配置

    由于当前运营商网络问题,可能会导致拉取 Docker Hub 镜像变慢,因此我们可以配置阿里的镜像加速配置。

    • 获取镜像加速器地址

    ​ ACR会为每一个账号(阿里云账号或RAM用户)生成一个镜像加速器地址,配置镜像加速器前,您需要获取镜像加速器地址。

    ​ 登录容器镜像服务控制台 (opens new window),在左侧导航栏选择镜像工具 > 镜像加速器,在镜像加速器页面获取加速器地址

    例如:
    加速器地址:[系统分配前缀].mirror.aliyuncs.com    
    
    1
    2
    • 配置Docker运行时镜像加速器

      • 方式1: 配置文件直接指定/etc/containerd/config.toml

      找到如下配置新增最后两项。如果涉及到多个地址替换,则配置多个 [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]和endpoint = ["https://xxxx.mirror.aliyuncs.com"]即可

      [plugins."io.containerd.grpc.v1.cri".registry]
            config_path = ""
      
            [plugins."io.containerd.grpc.v1.cri".registry.auths]
      
            [plugins."io.containerd.grpc.v1.cri".registry.configs]
      
            [plugins."io.containerd.grpc.v1.cri".registry.headers]
      
            [plugins."io.containerd.grpc.v1.cri".registry.mirrors] 
              [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] # 替换docker.io为下面的地址
                endpoint = ["https://xxxx.mirror.aliyuncs.com"]				   #  指定加速地址
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      • 方式二:config_path方式

      Containerd通过在启动时指定一个配置文件夹,使后续所有镜像仓库相关的配置都可以在里面热加载,无需重启Containerd。

      1. 在/etc/containerd/config.toml配置文件中插入如下config_path:

        [plugins."io.containerd.grpc.v1.cri".registry]
          config_path = "/etc/containerd/certs.d"
          
        ### 若果配置文件中有如下配置,则需要清理
        [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
          [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
            endpoint = ["https://registry-1.docker.io"]
        
        1
        2
        3
        4
        5
        6
        7
      2. 重启Containerd

        systemctl restart containerd
        
        1
      3. 在步骤一中指定的config_path路径中创建docker.io/hosts.toml文件并在文件中写入如下配置。

      server = "https://registry-1.docker.io"
      
      [host."https://xxx.mirror.aliyuncs.com"]
        capabilities = ["pull", "resolve", "push"]
      
      1
      2
      3
      4

    拉取Docker镜像验证加速是否生效。如未生效,请参见Reference (opens new window)。

    # 1.3 安装kubeadmin
    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
    enabled=1
    gpgcheck=0
    repo_gpgcheck=0
    gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
            http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF
    
    yum clean all && yum makecache
    
    yum install  kubelet kubeadm kubectl --disableexcludes=kubernetes -y
    systemctl enable kubelet
    systemctl start kubelet
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # 1.4 初始化集群
    # 1.4.1 方案一
    #1 直接初始化集群
    kubeadm init --kubernetes-version=1.26.0 --apiserver-advertise-address=10.70.31.100 --image-repository registry.aliyuncs.com/google_containers --service-cidr=172.15.0.0/16 --pod-network-cidr=172.16.0.0/16 --cri-socket unix:///var/run/containerd/containerd.sock
    
    1
    2

    参数说明

    • kubeadm init: 这是命令的主体,它告诉 Kubernetes 初始化一个新的群集。
    • --kubernetes-version=1.26.0: 这个参数指定了要安装的 Kubernetes 版本,这里是1.26.0版本。
    • --apiserver-advertise-address=10.70.31.100: 这个参数指定了 API Server(API服务器)的广播地址,也就是告诉其他节点如何连接到这个主节点。
    • --image-repository registry.aliyuncs.com/google_containers: 这个参数指定了容器镜像的仓库地址,这里使用了阿里云的容器镜像仓库地址。
    • --service-cidr=172.15.0.0/16: 这个参数指定了服务网络的 CIDR 地址范围,Kubernetes 使用此范围来分配 ClusterIP。
    • --pod-network-cidr=172.16.0.0/16: 这个参数指定了 Pod 网络的 CIDR 地址范围,该范围内的 IP 地址将分配给 Pod。
    • --cri-socket unix:///var/run/containerd/containerd.sock: 这个参数指定了容器运行时接口的套接字地址,这里是容器d (Containerd) 的套接字地址。
    # 1.4.2 方案二
    #2 使用配置文件部署
    # ## 查看默认配置
    kubeadm config print init-defaults
    # 查看所需镜像
    kubeadm config images list --image-repository registry.aliyuncs.com
    
    # 导出默认配置文件到当前目录
    kubeadm config print init-defaults > kubeadm.yaml
    # 提前pull所需要镜像
    kubeadm config images pull --config kubeadm.yaml
    # 执行初始化
    kubeadm init --config kubeadm.yaml
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 1.4.3 node加入

    使用上面两种方案,无论哪一种,执行结束之后都会看到如下信息

    .....
    .....
    [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
    [addons] Applied essential addon: CoreDNS
    [addons] Applied essential addon: kube-proxy
    
    Your Kubernetes control-plane has initialized successfully!
    
    To start using your cluster, you need to run the following as a regular user:
    
      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
    Alternatively, if you are the root user, you can run:
    
      export KUBECONFIG=/etc/kubernetes/admin.conf
    
    You should now deploy a pod network to the cluster.
    Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
      https://kubernetes.io/docs/concepts/cluster-administration/addons/
    
    Then you can join any number of worker nodes by running the following on each as root:
    # node节点加入的时候 直接执行即可
    kubeadm join 10.70.31.101:6443 --token iv4jwm.ckmg2re8y3q5hxx9 \
    	--discovery-token-ca-cert-hash sha256:8ac51b12b4c84ff79d7876f49e2cf4fdab30861ccacefb3c10a97c0f0a18f1d0
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    # 1.5 网络CNI
    • Calico
    wget https://projectcalico.docs.tigera.io/archive/v3.25/manifests/calico.yaml --no-check-certificate
    
    1

    提示

    这里下载自己所需要的版本之后,直接执行kubectl apply -f calico.yaml即可,无需做其它修改,因为我这边直接在初始化的时候,就指定pod,service的网段。

    # 1.6 命令自动补全

    这个是为了后续维护方便,根据自己需要是否安装即可

    yum -y install bash-completion
    echo "source <(kubectl completion bash)" >> /etc/profile
    source /etc/profile
    
    1
    2
    3
    # 1.7 删除集群
    kubeadm reset
    rm -rf /etc/cni/net.d/ /root/.kube/
    iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
    ipvsadm -C
    
    1
    2
    3
    4
    # 1.8 容器抓包
    容器抓包方法
    1 可以先执行kubectl get pods $PodName -n $NameSpace -o wide看看pod运行的节点
    2 登录到对应的node上,如果是docker运行时,执行 docker ps| grep $pod名称 找到容器ID,然后在执行 docker inspect -f {{.State.Pid}} 容器id 找到容器的进程pid
    如果是containerd, yum -y install jq ; crictl inspect $(crictl ps | grep `crictl pods | grep $POD_NAME名称 | awk '{print$1}'` | awk '{print$1}') | jq .info.pid ,找到容器的进程pid
    3 执行yum -y install util-linux.x86_64 安装下 nsenter工具,然后执行 nsenter --target 容器pid -n 进入到容器的网络名称空间,通过tcpdump -i eth0 -s 0 -w /tmp/1.pcap抓包 
    
    
    1
    2
    3
    4
    5
    6
    # 1.9 集群管理
    • token管理
    ## 查看token状态
    ### TTL值 就是token生于时间
    [root@k8s-master01 ~]# kubeadm token list
    TOKEN                     TTL         EXPIRES                USAGES                   DESCRIPTION                                                EXTRA GROUPS
    wd54ig.u4n1ryclg1ep5ewf   23h         2023-03-01T08:56:28Z   authentication,signing   <none>                                                     system:bootstrappers:kubeadm:default-node-token
    ## 上面看到token只有24h 如果我们超过24h再加入新的节点则需要重新生产token
    [root@k8s-master-01 ~]# kubeadm token create
    wd54ig.u4n1ryclg1ep5ewf
    ## 获取--discovery-token-ca-cert-hash值
    [root@k8s-master ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    编辑 (opens new window)
    #k8s
    上次更新: 2024/06/14, 16:50:33
    k8s-创建serviceAccount

    k8s-创建serviceAccount→

    最近更新
    01
    cert-manager自动签发Lets Encrypt
    09-05
    02
    Docker构建多架构镜像
    08-02
    03
    Prometheus数据迁移至VMstorage
    08-01
    更多文章>
    Theme by Vdoing | Copyright © 2023-2024 |豫ICP备2021026650号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式