一文吃透如何部署kubernetes高可用集群

使用 k8s 官方提供的部署工具 kubeadm 自动安装,需要在 master 和 node 节点上安装 docker 等组件,然后初始化,把管理端的控制服务和 node 上的服务都以 pod 的方式运行。
kubernetes部署过程:
本次部署的集群是v1.17.2
分为以下几个步骤
1.基础环境准备
2.在master 安装指定版本的 kubeadm 、kubelet、kubectl、docker
3.在所有 node 节点安装指定版本的 kubeadm 、kubelet、docker,在 node 节点 kubectl 为
可选安装,看是否需要在 node 执行 kubectl 命令进行集群管理及 pod 管理等操作。
4.master节点运行kubeadm init初始化命令
5.验证master节点状态
6.在 node 节点使用 kubeadm 命令将自己加入 k8s master(需要使用 master 生成 token 认
证)
7.验证集群状态
#######centos系统必须要关闭firewalld和SELINUX!!!
1.机器准备
k8s-master 10.0.0.50 Ubuntu Server k8s-node1 10.0.0.51 Ubuntu Server
k8s-node2 10.0.0.52
harbor
10.0.0.111 #这里的harbor主要为了方便上传下载镜像!!!
2.更改主机名
hostnamectl set-hostname K8s-master
hostnamectl set-hostname K8s-node1
hostnamectl set-hostname K8s-node2
3.部署harbor
##部署habror的步骤省略,在上一篇有详细说明
4.关闭swap分区
swapoff -a
5.调整内核参数,否则kubernetes集群无法起来。
root@K8s-node2:~# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
6.所有节点安装docker
#在阿里云有详细安装docker。


验证 docker 版本:
root@K8s-node1:/# docker --version
Docker version 19.03.8, build afacb8b7f0
7.配置docker的镜像加速。
https://help.aliyun.com/document_detail/60450.html

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
 "registry-mirrors": ["https://b4budxxz.mirror.aliyuncs.com"]
}
EOF
# sudo systemctl daemon-reload && sudo systemctl restart docker
8.所有节点安装 kubelet kubeadm kubectl
#所有节点配置阿里云仓库地址并安装相关组件,node 节点可选安装 kubectl
#https://developer.aliyun.com/mirror/kubernetes?spm=a2c6h.13651102.0.0.3e221b11JXALy6  阿里云kubernetes镜像地址
apt-get update && apt-get install -y apt-transport-https

root@K8s-node2:/# cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
> deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
> EOF   #
root@K8s-master:~# apt-get update


#安装指定版本 kubeadm 查看版本信息
# apt-cache madison kubeadm


root@K8s-master:~# apt install kubeadm=1.17.2-00 kubectl=1.17.2-00 kubelet=1.17.2-00

root@K8s-node1:/# apt install kubeadm=1.17.2-00 kubelet=1.17.2-00

root@K8s-master:~# systemctl start kubelet && systemctl enable kubelet && systemctl status kubelet


#验证kubeadm版本

 root@K8s-master:/# kubeadm version
 kubeadm version: &version.Info{Major:”1″, Minor:”17″, GitVersion:”v1.17.2″, GitCommit:”59603c6e503c87169aea6106f57b9f242f64df89″, GitTreeState:”clean”, BuildDate:”2020-0 1-18T23:27:49Z”, GoVersion:”go1.13.5″, Compiler:”gc”, Platform:”linux/amd64″}

 

 



9.准备k8s-master需要的镜像
#查看安装指定K8s版本需要的镜像有哪些
root@K8s-master:/# kubeadm config images list --kubernetes-version v1.17.2
k8s.gcr.io/kube-apiserver:v1.17.2
k8s.gcr.io/kube-controller-manager:v1.17.2
k8s.gcr.io/kube-scheduler:v1.17.2
k8s.gcr.io/kube-proxy:v1.17.2
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.5


10.下载master需要的镜像
root@K8s-master:~# vim images-download.sh

#!bin/bash
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5
root@K8s-master:~# bash images-download.sh




root@K8s-master:~# docker images
REPOSITORY                                                        TAG                 IMAGE ID            CREATED             SIZE
quay.io/coreos/flannel                                            v0.12.0-amd64       4e9f801d2217        10 months ago       52.8MB
registry.aliyuncs.com/google_containers/kube-proxy                v1.17.2             cba2a99699bd        12 months ago       116MB
registry.aliyuncs.com/google_containers/kube-controller-manager   v1.17.2             da5fd66c4068        12 months ago       161MB
registry.aliyuncs.com/google_containers/kube-apiserver            v1.17.2             41ef50a5f06a        12 months ago       171MB
registry.aliyuncs.com/google_containers/kube-scheduler            v1.17.2             f52d4c527ef2        12 months ago       94.4MB
registry.aliyuncs.com/google_containers/coredns                   1.6.5               70f311871ae1        14 months ago       41.6MB
registry.aliyuncs.com/google_containers/etcd                      3.4.3-0             303ce5db0e90        14 months ago       288MB
registry.aliyuncs.com/google_containers/pause                     3.1                 da86e6ba6ca1        3 years ago         742kB
11.master节点运行集群初始化命令
 kubeadm init --apiserver-advertise-address=10.0.0.50 --apiserver-bind-port=6443 --
kubernetes-version=v1.17.2 --pod-network-cidr=192.168.0.0/16 --service-cidr=10.20.0.0/16 --
service-dns-domain=lengyuye --image-repository=registry.cnhangzhou.aliyuncs.com/google_containers --ignore-preflight-errors=swap
12.初始化结果
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

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:

kubeadm join 10.0.0.50:6443 --token 1gm82e.z91u96kh4cxes0o1 \
    --discovery-token-ca-cert-hash sha256:8427c509f211eece6e720623628d8895ccaf63b8482f85daf79002aec988d682
13.创建kube-config配置文件并且上传网络组件。
root@K8s-master:~# mkdir -p $HOME/.kube
root@K8s-master:~# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
root@K8s-master:~# sudo chown $(id -u):$(id -g) $HOME/.kube/config
root@K8s-master:~# kubectl get nodes


root@K8s-master:~# kubectl apply -f kube-flannel.yml
14.验证k8s集群状态
root@K8s-master:/# kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"} 


15当前csr证书状态
root@K8s-master:/# kubectl get csr
NAME        AGE   REQUESTOR                 CONDITION
csr-4s2pt   52m   system:bootstrap:1gm82e   Approved,Issued
csr-kkr64   84m   system:node:k8s-master    Approved,Issued
csr-km8fl   51m   system:bootstrap:1gm82e   Approved,Issued
16.k8s集群添加node节点

kubeadm join 10.0.0.50:6443 --token 1gm82e.z91u96kh4cxes0o1 \
    --discovery-token-ca-cert-hash sha256:8427c509f211eece6e720623628d8895ccaf63b8482f85daf79002aec99bd682

17.验证当前node节点状态

root@K8s-master:/# kubectl get node
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   86m   v1.17.2
k8s-node1    Ready    <none>   54m   v1.17.2
k8s-node2    Ready    <none>   53m   v1.17.2

## kubernetes集群部署到这里全部结束!!!希望对你能有所帮助!!

人已赞赏
经验教程

拿破仑,技术大牛晋级管理之后的困境

2021-2-27 13:39:00

经验教程

Linux下搭建ZooKeeper集群

2021-2-27 15:22:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索