• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Centos7安装Kubernetes 1.27.2

武飞扬头像
熟透的蜗牛
帮助1

目录

一、准备工作

二、容器运行时

三、安装kubelet 、kubeadm、 kubectl

四、配置CNI

五、安装nginx


一、准备工作

1、更新yum源安装 vim、net-tools等工具(每个节点都执行)

  1.  
     yum update -y
  2.  
      yum install vim -y
  3.  
      yum install net-tools -y

2、配置每个节点的网络,然后能互相ping通(每个节点上都要执行)

  1.  
    vi /etc/hosts
  2.  
     
  3.  
    192.168.178.141 master
  4.  
    192.168.178.150 node1
  5.  
    192.168.178.151 node2

3、安装时间插件,保证每个节点时间一致(每个节点上都要执行)

  1.  
    yum install -y chrony
  2.  
    systemctl start chronyd
  3.  
    systemctl enable chronyd

4、关闭防火墙(这里为了省事,生产环境请开放相应的端口,每个节点都执行)

systemctl stop firewalld && systemctl disable firewalld

5、禁用swap分区(每个节点都执行)

sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

6、禁用SELINUX(每个节点上都要执行)

sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config  && setenforce 0

7、转发 IPv4 并让 iptables 看到桥接流量

  1.  
    cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
  2.  
    overlay
  3.  
    br_netfilter
  4.  
    EOF
  5.  
     
  6.  
    sudo modprobe overlay
  7.  
    sudo modprobe br_netfilter
  8.  
     
  9.  
    # 设置所需的 sysctl 参数,参数在重新启动后保持不变
  10.  
    cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
  11.  
    net.bridge.bridge-nf-call-iptables = 1
  12.  
    net.bridge.bridge-nf-call-ip6tables = 1
  13.  
    net.ipv4.ip_forward = 1
  14.  
    EOF
  15.  
     
  16.  
    # 应用 sysctl 参数而不重新启动
  17.  
    sudo sysctl --system
  18.  
     
  19.  
     
  20.  
    #检查模块是否被加载
  21.  
    lsmod | grep br_netfilter
  22.  
    lsmod | grep overlay
  23.  
     
  24.  
    #检查是否设置成功
  25.  
    sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward
学新通

8、 配置ipvs功能(每个节点上都要执行)

  1.  
    #安装
  2.  
    yum install ipset ipvsadm -y
  3.  
    #配置
  4.  
    cat <<EOF > /etc/sysconfig/modules/ipvs.modules
  5.  
    #!/bin/bash
  6.  
    modprobe -- ip_vs
  7.  
    modprobe -- ip_vs_rr
  8.  
    modprobe -- ip_vs_wrr
  9.  
    modprobe -- ip_vs_sh
  10.  
    modprobe -- nf_conntrack_ipv4
  11.  
    EOF
  12.  
    #授权
  13.  
    chmod x /etc/sysconfig/modules/ipvs.modules
  14.  
    #执行
  15.  
    sh x /etc/sysconfig/modules/ipvs.modules
  16.  
    #查看
  17.  
    lsmod | grep -e ip_vs -e nf_conntrack_ipv4
学新通

9、重启服务器让SELINUX生效

reboot -h now

二、容器运行时

学新通

 k8s支持4中容器运行时,这里介绍containerd方式安装。(每个节点都要安装)

  1.  
    # 添加docker源
  2.  
    curl -L -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  3.  
    # 安装containerd
  4.  
    yum install -y containerd.io
  5.  
    # 创建默认配置文件
  6.  
    containerd config default > /etc/containerd/config.toml
  7.  
    # 设置aliyun地址,不设置会连接不上, 如果无法下载镜像检查一下配置是否替换 cat /etc/containerd/config.toml |grep sandbox_image
  8.  
    sed -i "s#registry.k8s.io/pause#registry.aliyuncs.com/谷歌_containers/pause#g" /etc/containerd/config.toml
  9.  
    # 设置驱动为systemd
  10.  
    sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
  11.  
    # 设置docker地址为aliyun镜像地址
  12.  
    sed -i '/\[plugins\."io\.containerd\.grpc\.v1\.cri"\.registry\.mirrors\]/a\ [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]\n endpoint = ["https://3kcpregv.mirror.aliyuncs.com" ,"https://registry-1.docker.io"]' /etc/containerd/config.toml
  13.  
     
  14.  
    # 重启服务
  15.  
    systemctl daemon-reload
  16.  
    systemctl enable --now containerd
  17.  
    systemctl restart containerd
学新通

三、安装kubelet 、kubeadm、 kubectl

1、安装Crictl

  1.  
    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
  2.  
    [kubernetes]
  3.  
    name=Kubernetes
  4.  
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
  5.  
    enabled=1
  6.  
    gpgcheck=1
  7.  
    repo_gpgcheck=1
  8.  
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  9.  
    EOF
  10.  
     
  11.  
    setenforce 0
  12.  
     
  13.  
    # 安装crictl工具
  14.  
    yum install -y cri-tools
  15.  
    # 生成配置文件
  16.  
    crictl config runtime-endpoint
  17.  
    # 编辑配置文件
  18.  
    cat << EOF | tee /etc/crictl.yaml
  19.  
    runtime-endpoint: "unix:///run/containerd/containerd.sock"
  20.  
    image-endpoint: "unix:///run/containerd/containerd.sock"
  21.  
    timeout: 10
  22.  
    debug: false
  23.  
    pull-image-on-create: false
  24.  
    disable-pull-on-run: false
  25.  
    EOF
  26.  
     
  27.  
    # 查看是否安装成功,和docker命令差不多
  28.  
    crictl info
  29.  
    crictl images
学新通

2、安装kubelet、kubeadm、kubectl

  1.  
    yum install --setopt=obsoletes=0 kubelet-1.27.2-0 kubeadm-1.27.2-0 kubectl-1.27.2-0 -y
  2.  
     
  3.  
    systemctl enable kubelet && systemctl start kubelet

3、节点初始化(仅在master节点执行)

  1.  
    kubeadm init \
  2.  
    --apiserver-advertise-address=192.168.178.141 \
  3.  
    --image-repository registry.aliyuncs.com/谷歌_containers \
  4.  
    --kubernetes-version v1.27.2 \
  5.  
    --service-cidr=10.96.0.0/12 \
  6.  
    --pod-network-cidr=10.244.0.0/16 \
  7.  
    --ignore-preflight-errors=all

 如果报错执行重置命令

  1.  
    kubeadm reset
  2.  
     
  3.  
    rm -fr ~/.kube/ /etc/kubernetes/* var/lib/etcd/*

学新通

  在master节点执行

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

 在node节点执行,加入集群

  1.  
    kubeadm join 192.168.178.141:6443 --token 2jm0vf.l3xyduayvig5w9cv \
  2.  
    --discovery-token-ca-cert-hash sha256:379185fe826631b65fb41bc8d57665742e8cffc0736552cf06dc5423c7e80c41

四、配置CNI

1、创建kube-flannel.yml(master节点执行)

  1.  
    cat > kube-flannel.yml << EOF
  2.  
    ---
  3.  
    kind: Namespace
  4.  
    apiVersion: v1
  5.  
    metadata:
  6.  
    name: kube-flannel
  7.  
    labels:
  8.  
    pod-security.kubernetes.io/enforce: privileged
  9.  
    ---
  10.  
    kind: ClusterRole
  11.  
    apiVersion: rbac.authorization.k8s.io/v1
  12.  
    metadata:
  13.  
    name: flannel
  14.  
    rules:
  15.  
    - apiGroups:
  16.  
    - ""
  17.  
    resources:
  18.  
    - pods
  19.  
    verbs:
  20.  
    - get
  21.  
    - apiGroups:
  22.  
    - ""
  23.  
    resources:
  24.  
    - nodes
  25.  
    verbs:
  26.  
    - list
  27.  
    - watch
  28.  
    - apiGroups:
  29.  
    - ""
  30.  
    resources:
  31.  
    - nodes/status
  32.  
    verbs:
  33.  
    - patch
  34.  
    ---
  35.  
    kind: ClusterRoleBinding
  36.  
    apiVersion: rbac.authorization.k8s.io/v1
  37.  
    metadata:
  38.  
    name: flannel
  39.  
    roleRef:
  40.  
    apiGroup: rbac.authorization.k8s.io
  41.  
    kind: ClusterRole
  42.  
    name: flannel
  43.  
    subjects:
  44.  
    - kind: ServiceAccount
  45.  
    name: flannel
  46.  
    namespace: kube-flannel
  47.  
    ---
  48.  
    apiVersion: v1
  49.  
    kind: ServiceAccount
  50.  
    metadata:
  51.  
    name: flannel
  52.  
    namespace: kube-flannel
  53.  
    ---
  54.  
    kind: ConfigMap
  55.  
    apiVersion: v1
  56.  
    metadata:
  57.  
    name: kube-flannel-cfg
  58.  
    namespace: kube-flannel
  59.  
    labels:
  60.  
    tier: node
  61.  
    app: flannel
  62.  
    data:
  63.  
    cni-conf.json: |
  64.  
    {
  65.  
    "name": "cbr0",
  66.  
    "cniVersion": "0.3.1",
  67.  
    "plugins": [
  68.  
    {
  69.  
    "type": "flannel",
  70.  
    "delegate": {
  71.  
    "hairpinMode": true,
  72.  
    "isDefaultGateway": true
  73.  
    }
  74.  
    },
  75.  
    {
  76.  
    "type": "portmap",
  77.  
    "capabilities": {
  78.  
    "portMappings": true
  79.  
    }
  80.  
    }
  81.  
    ]
  82.  
    }
  83.  
    net-conf.json: |
  84.  
    {
  85.  
    "Network": "10.244.0.0/16",
  86.  
    "Backend": {
  87.  
    "Type": "vxlan"
  88.  
    }
  89.  
    }
  90.  
    ---
  91.  
    apiVersion: apps/v1
  92.  
    kind: DaemonSet
  93.  
    metadata:
  94.  
    name: kube-flannel-ds
  95.  
    namespace: kube-flannel
  96.  
    labels:
  97.  
    tier: node
  98.  
    app: flannel
  99.  
    spec:
  100.  
    selector:
  101.  
    matchLabels:
  102.  
    app: flannel
  103.  
    template:
  104.  
    metadata:
  105.  
    labels:
  106.  
    tier: node
  107.  
    app: flannel
  108.  
    spec:
  109.  
    affinity:
  110.  
    nodeAffinity:
  111.  
    requiredDuringSchedulingIgnoredDuringExecution:
  112.  
    nodeSelectorTerms:
  113.  
    - matchExpressions:
  114.  
    - key: kubernetes.io/os
  115.  
    operator: In
  116.  
    values:
  117.  
    - linux
  118.  
    hostNetwork: true
  119.  
    priorityClassName: system-node-critical
  120.  
    tolerations:
  121.  
    - operator: Exists
  122.  
    effect: NoSchedule
  123.  
    serviceAccountName: flannel
  124.  
    initContainers:
  125.  
    - name: install-cni-plugin
  126.  
    #image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)
  127.  
    image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
  128.  
    command:
  129.  
    - cp
  130.  
    args:
  131.  
    - -f
  132.  
    - /flannel
  133.  
    - /opt/cni/bin/flannel
  134.  
    volumeMounts:
  135.  
    - name: cni-plugin
  136.  
    mountPath: /opt/cni/bin
  137.  
    - name: install-cni
  138.  
    #image: flannelcni/flannel:v0.19.0 for ppc64le and mips64le (dockerhub limitations may apply)
  139.  
    image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.0
  140.  
    command:
  141.  
    - cp
  142.  
    args:
  143.  
    - -f
  144.  
    - /etc/kube-flannel/cni-conf.json
  145.  
    - /etc/cni/net.d/10-flannel.conflist
  146.  
    volumeMounts:
  147.  
    - name: cni
  148.  
    mountPath: /etc/cni/net.d
  149.  
    - name: flannel-cfg
  150.  
    mountPath: /etc/kube-flannel/
  151.  
    containers:
  152.  
    - name: kube-flannel
  153.  
    #image: flannelcni/flannel:v0.19.0 for ppc64le and mips64le (dockerhub limitations may apply)
  154.  
    image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.0
  155.  
    command:
  156.  
    - /opt/bin/flanneld
  157.  
    args:
  158.  
    - --ip-masq
  159.  
    - --kube-subnet-mgr
  160.  
    resources:
  161.  
    requests:
  162.  
    cpu: "100m"
  163.  
    memory: "50Mi"
  164.  
    limits:
  165.  
    cpu: "100m"
  166.  
    memory: "50Mi"
  167.  
    securityContext:
  168.  
    privileged: false
  169.  
    capabilities:
  170.  
    add: ["NET_ADMIN", "NET_RAW"]
  171.  
    env:
  172.  
    - name: POD_NAME
  173.  
    valueFrom:
  174.  
    fieldRef:
  175.  
    fieldPath: metadata.name
  176.  
    - name: POD_NAMESPACE
  177.  
    valueFrom:
  178.  
    fieldRef:
  179.  
    fieldPath: metadata.namespace
  180.  
    - name: EVENT_QUEUE_DEPTH
  181.  
    value: "5000"
  182.  
    volumeMounts:
  183.  
    - name: run
  184.  
    mountPath: /run/flannel
  185.  
    - name: flannel-cfg
  186.  
    mountPath: /etc/kube-flannel/
  187.  
    - name: xtables-lock
  188.  
    mountPath: /run/xtables.lock
  189.  
    volumes:
  190.  
    - name: run
  191.  
    hostPath:
  192.  
    path: /run/flannel
  193.  
    - name: cni-plugin
  194.  
    hostPath:
  195.  
    path: /opt/cni/bin
  196.  
    - name: cni
  197.  
    hostPath:
  198.  
    path: /etc/cni/net.d
  199.  
    - name: flannel-cfg
  200.  
    configMap:
  201.  
    name: kube-flannel-cfg
  202.  
    - name: xtables-lock
  203.  
    hostPath:
  204.  
    path: /run/xtables.lock
  205.  
    type: FileOrCreate
  206.  
    EOF
学新通

2、执行

kubectl apply -f kube-flannel.yml

3、查看node节点(等待1-2分钟)

kubectl get nodes

学新通

五、安装nginx

1、在master执行如下命令

kubectl create deployment nginx --image=nginx


2、暴露端口

kubectl expose deployment nginx --port=80 --type=NodePort


3、查看运行的服务

kubectl get pods,service

学新通

 4、访问nginx

ip 31201

学新通

参考:https://blog.csdn.net/qq_36765991/article/details/128702891

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibckbb
系列文章
更多 icon
同类精品
更多 icon
继续加载