Bootstrap

K8S单节点部署及集群部署

1.Minikube搭建单节点K8S

  • 前置条件:安装docker,注意版本兼容问题

    # 配置docker源
    wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
    
    # 安装docker环境依赖
    yum install -y yum-utils device-mapper-persistent-data lvm2
    
    # 安装docker
    yum install -y docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io
    
    # 启动docker并设置开机自启
    systemctl start docker && systemctl enable docker
    
    # 配置镜像加速
    vim /etc/docker/daemon.json
    {
        "registry-mirrors": [
            "https://docker.m.daocloud.io",
            "https://dockerproxy.com",
            "https://registry.docker-cn.com",
            "https://docker.mirrors.ustc.edu.cn",
            "https://hub-mirror.c.163.com",
            "https://hub.uuuadc.top",
            "https://docker.anyhub.us.kg",
            "https://dockerhub.jobcher.com",
            "https://dockerhub.icu",
            "https://docker.ckyl.me",
            "https://docker.awsl9527.cn",
            "https://mirror.baidubce.com"
        ]
    }
    
    # 重新启动docker
    systemctl daemon-reload && systemctl restart docker
    
  • 安装kubectl和minikube

    # 下载kubectl上传到服务器
    http://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl
    
    # 下载后重命名minikube并上传到服务器
    https://storage.googleapis.com/minikube/releases/v1.18.0/minikube-linux-amd64
    
    # 添加执行权限
    chmod +x kubectl && chmod +x minikube
      
    # 复制到/usr/local/bin
    cp kubectl /usr/local/bin/ && cp minikube /usr/local/bin/
    
    # 查看版本检查是否安装成功
    kubectl version --client
    minikube version
    
    # 配置源
    vim /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
    enabled=1
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    
    # 更新yum缓存
    yum clean all
    yum -y makecache
    
    # 安装bash-completion conntrack
    yum -y install bash-completion socat conntrack
    source /etc/profile.d/bash_completion.sh
    
    # 拉取minikube启动所需镜像
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.0 &&
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.0 &&
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.0 &&
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.0 &&
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 &&
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3 &&
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7 &&
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v1.8.1
    
    # 打标签
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.0 k8s.gcr.io/kube-apiserver:v1.18.0 &&
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.0 k8s.gcr.io/kube-controller-manager:v1.18.0 &&
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.0 k8s.gcr.io/kube-scheduler:v1.18.0 &&
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.0 k8s.gcr.io/kube-proxy:v1.18.0 &&
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2 &&
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3 k8s.gcr.io/etcd:3.4.3-0 &&
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7 k8s.gcr.io/coredns:1.6.7 &&
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v1.8.1 gcr.io/k8s-minikube/storage-provisioner:v1.8.1
    
    # 启动minikube –vm-driver=none表示使用Linux本机作为运行环境,--kubernetes-version表示使用的版本
    minikube start --vm-driver=none --kubernetes-version='v1.18.0'
    
    # 启动kubectl proxy
    kubectl proxy --port=8001 --address='192.168.91.129' --accept-hosts='^.*' &
    # 在kubectl proxy中运行
    minikube dashboard
    
    # 浏览器访问:http://192.168.91.129:8001/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/overview?namespace=default
    
    # 查看K8S集群信息
    kubectl cluster-info
    # 查看节点信息
    kubectl get node
    # 查看内部组件
    kubectl get pod -A
    
  • 测试部署nginx

    # 创建deployment(Pod控制器的一种,直接删除Pod后,会自动创建新的,需要删除deployment)
    kubectl create deploy gen-nginx --image=nginx:1.23.0
    # 对比docker部署
    # docker run --name gen-nginx -p 8080:80 -d nginx:1.23.0
    
    # 查看deployment和pod
    kubectl get deploy,pod,svc
    
    # 暴露80端口
    kubectl expose deploy gen-nginx --port=80 --type=NodePort
    
    # 转发端口(Mini Kube临时),解释:kubectl port-forward转发一个本地端口到Pod端口,不会返回数据,需要另开终端练习
    kubectl port-forward --address 0.0.0.0 service/gen-nginx 80:80
    
    # 浏览器访问http://192.168.91.129/
    

2.KubeAdm搭建多节点K8S集群

  • 安装docker(主节点+工作节点),见文章上面,此处不重复编写

  • 配置阿里云镜像源(主节点+工作节点)vim /etc/yum.repos.d/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
    
  • 安装kubelet kubeadm kubectl(主节点+工作节点)

    yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
    
  • 初始化主节点(主节点)master主机和K8S版本需配置自己的

    kubeadm init \
    --apiserver-advertise-address=192.168.91.132 \
    --image-repository registry.aliyuncs.com/google_containers \
    --kubernetes-version v1.18.0 \
    --service-cidr=10.96.0.0/12 \
    --pod-network-cidr=10.244.0.0/16
    
    • –apiserver-advertise-address:主节点的内网ip地址
    • –image-repository:镜像仓库
    • –kubernetes-version:K8S版本
    • –service-cidr --pod-network-cidr:网段不重复即可
  • 等待主节点初始化完成后执行下面命令

    在这里插入图片描述

    • 主节点执行命令

      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
      
    • 工作节点操作,如若两台虚拟机需注意主机名hostnamectl set-hostname node1

      kubeadm join 192.168.91.130:6443 --token bh7nz3.cxb8e9jtvqjc6thg \
          --discovery-token-ca-cert-hash sha256:2e41d09d203c66e678a6d71b138e2144daaf6fa42683d2e76ee63e85d5e3e8ec
      
    • 主节点执行命令

      # 查看节点,状态都是NotReady,需要配置网络插件
      kubectl get nodes
      
      # 安装网络插件
      kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
      # 出现外网不可用,换种方式解决下,查询raw.githubusercontent.com的ip加入host
      vim /etc/hosts
      185.199.111.133 raw.githubusercontent.com
      
      # 查看节点状态
      kubectl get nodes
      # 查看系统Pod状态
      kubectl get pods -n kube-system
      
  • 测试部署nginx

    # 创建deployment(Pod控制器的一种)
    kubectl create deploy gen-nginx --image=nginx:1.23.0
    
    # 查看deployment和Pod
    kubectl get deploy,pod,svc
    
    # 暴露80端口,创建service
    kubectl expose deploy gen-nginx --port=80 --type=NodePort
    
    # 查看端口映射
    kubectl get deploy,pod,svc
    
    # 浏览器访问,master、node节点ip都可访问 ip:30356(随机生成的端口)
    # 注意:kubeadm部署,暴露端口对外服务会随机选端口,默认范围30000~32767,可以修改指定,后续文章记录
    
;