记一次用kubeadm安装k8s

76 阅读1分钟

安装配置

  1. 系统:centos7
  2. k8s 版本:1.16.2 (1.24之后用的是contained,这里安装方式会存在问题)
  3. 配置:一个master节点 两个node节点

前置配置

  1. 设置主机名
    hostnamectl set-hostname k8s-master
    hostnamectl set-hostname k8s-slave1
    hostnamectl set-hostname k8s-slave2
    
  2. 修改host
    cat << EOF >> /etc/hosts
    10.211.55.21 k8s-master
    10.211.55.22 k8s-slave1
    10.211.55.23 k8s-slave2
    EOF
    

安装docker(所有节点都需操作)

  1. 关闭防火墙

    systemctl stop firewalld.service 
    systemctl disable firewalld.service
    
  2. 关闭交换空间

    swapoff -a
    sed -i 's/.*swap.*/#&/' /etc/fstab
    
  3. 关闭selinux

    setenforce 0
    sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
    
  4. 修改内核数

    # 修改内核参数
    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
    EOF
    
  5. 设置yum源

    curl -o /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
    # 是否开启本仓库
    enabled=1
    # 是否检查 gpg 签名文件
    gpgcheck=0
    # 是否检查 gpg 签名文件
    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
    
  6. 设置yum缓存 yum clean all && yum makecache

    yum clean all && yum makecache
    
  7. 安装docker-ce

    yum install docker-ce
    
  8. 设置docker源加速

    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": [
        "https://dockerproxy.com",
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com",
        "https://ccr.ccs.tencentyun.com"
      ],
      "insecure-registries" : ["http://10.211.55.100:5000"]
    }
    EOF
    
  9. 重启docker

    systemctl daemon-reload && systemctl restart docker
    
  10. 安装kubeadm kubelet kubectl

    yum install -y kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2 -- disableexclues=kubernetes
    
    systemctl enable kubelet
    

安装kubeadm(只在master操作)

  1. 初始化

    kubeadm config print init-defaults > kubeadm.yaml 
    
  2. 修改配置

    apiVersion: kubeadm.k8s.io/v1beta2
    bootstrapTokens:
    - groups:
      - system:bootstrappers:kubeadm:default-node-token
      token: abcdef.0123456789abcdef
      ttl: 24h0m0s
      usages:
      - signing
      - authentication
    kind: InitConfiguration
    localAPIEndpoint:
      # 修改主机地址 为master的ip
      advertiseAddress: 10.211.55.21
      bindPort: 6443
    nodeRegistration:
      criSocket: /var/run/dockershim.sock
      name: k8s-master
      taints:
      - effect: NoSchedule
        key: node-role.kubernetes.io/master
    ---
    apiServer:
      timeoutForControlPlane: 4m0s
    apiVersion: kubeadm.k8s.io/v1beta2
    certificatesDir: /etc/kubernetes/pki
    clusterName: kubernetes
    controllerManager: {}
    dns:
      type: CoreDNS
    etcd:
      local:
        dataDir: /var/lib/etcd
    # 修改镜像地址
    imageRepository: registry.aliyuncs.com/google_containers
    kind: ClusterConfiguration
    # 修改版本号为install的版本号,前面我们装的事1.16.2
    kubernetesVersion: v1.16.2
    networking:
      dnsDomain: cluster.local
      # 增加pod网络配置
      podSubnet: 10.244.0.0/16
      serviceSubnet: 10.96.0.0/12
    scheduler: {}
    
  3. 拉取镜像

    kubeadm config images pull --config kubeadm.yaml
    
  4. 初始化

    kubeadm init --config kubeadm.yaml
    
  5. 根据提示信息导入环境变量

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

配置网络

  1. 获取配置
    wget https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml
    
  2. 安装
    kubectl apply -f kube-flannel.yml
    
  3. 解决 flannel 无可执行命令问题
    # slave可能也会有这个问题,需要同样处理
    https://github.com/containernetworking/plugins/releases/tag/v0.8.6
    wget https://github.com/containernetworking/plugins/releases/download/v0.8.6/cni-plugins-linux-amd64-v0.8.6.tgz
    tar axvf ./cni-plugins-linux-amd64-v0.8.6.tgz  -C /opt/cni/bin/
    

测试nginx

  1. 增加配置
    cat << EOF >> nginx.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.23.2
            ports:
            - containerPort: 80
    EOF
    
  2. 执行nginx配置
    kubectl apply -f nginx.yaml
    

操作命令记录

# 查看部署node
kubectl get node
# 查看所用namespace
kubectl get pods --all-namespaces
kubectl get pods --all-namespaces -o wide

# 查看node信息
kubectl describe node k8s-slave1

# 查看pod信息
kubectl -n ns describe pod podName

# 查看kubelet状态 
journalctl -f -u kubelet.service

kubectl create -f nginx.yaml
kubectl apply -f nginx.yaml
kubectl delete -f nginx.yaml