使用Kubeadm一步步部署 kubernetes 集群

部署教程

基础环境准备

VirtualBox: https://download.virtualbox.org/virtualbox/6.0.12/VirtualBox-6.0.12-133076-OSX.dmg
Centos7-Minimal: http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1908.iso
虚拟机安装完成后关闭,并配置网卡2和网卡3如下
网卡2配置
网卡3配置

1、master、node节点均需执行初始化操作
## 设置主机名(主要用来作节点名称,便于区分)
hostnamectl set-hostname <您的主机名>

## 添加主机名与IP对应关系(记得设置主机名)
cat /etc/hosts
192.168.56.112 master
192.168.56.113 node1

## 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

## 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

## 关闭swap
swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab  # 取消开机挂载swap

## 使用lsmod查看br_netfilter模块是否加载,未加载则加载
lsmod
yum install -y bridge-utils.x86_64 && modprobe  br_netfilter # 加载

## 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

## 重新加载所有配置文件
sysctl --system
2、master、node节点均需安装docker,建议版本一致
## 卸载原来的docker(初次安装docker也最好执行一次,否则后面安装docker可能会报错)
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine \
                  docker-selinux
                  
## 安装依赖
## 更新可选
yum update -y 
yum install -y yum-utils device-mapper-persistent-data lvm2

## 添加阿里云docker源(速度快)
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

## 添加官方docker源(官方源,速度慢,不建议)
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

## 查看docker版本列表
yum list docker-ce --showduplicates | sort -r

## 安装docker
## 以下命令默认安装最新stable版
yum install docker-ce -y 

## 安装的特定版本,测试时最新版未授支持
yum install docker-ce-18.09.4-3.el7.x86_64 -y

## 查看docker版本
docker version

## 启动docker并设为开机启动
systemctl enable --now docker

## 修改docker驱动与k8s一致,默认cgroup,使用systemd(不设置可能会出现错误或不兼容)
cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF

## 重启使配置生效
systemctl restart docker
3、master、node节点均需kubelet kubeadm kubectl,版本需要一致
## 配置kubernetes yum源
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

## 默认安装最新版kubelet kubeadm kubectl
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes


## 启动kubelet并设为开机启动
systemctl enable --now kubelet
4、部署Kubernetes Master(master需要至少2核,否则无法部署)
kubeadm init \
    --apiserver-advertise-address=192.168.56.112 \
    --image-repository registry.aliyuncs.com/google_containers  \
    --kubernetes-version v1.16.0 \
    --service-cidr=10.1.0.0/16 \
    --pod-network-cidr=10.244.0.0/16
## --apiserver-advertise-address master节点的ip地址
## --image-repository 旧版本可能没有此选项,指定镜像仓库地址,默认国外可能会下载失败(可选)
## --kubernetes-version kubernetes的版本,kubectl  version查看(可选)
## --service-cidr 为service VIPs使用不同的ip地址(可选)
## --pod-network-cidr 指定pod网络的ip地址范围(可选)

## 片刻后输出结果类似
......
[bootstrap-token] Using token: mce4gk.v28qvqgq2yskb4la
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[kubelet-check] Initial timeout of 40s passed.
[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

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 192.168.56.112:6443 --token mce4gk.v28qvqgq2yskb4la \
    --discovery-token-ca-cert-hash sha256:2bde976b1903fce4b1d5e56187d1b1a63958f0a53b35bf50e31450a0498d9e88

## 保存好最后带token的指令,节点加入集群需要使用它(也可重新生成)
    
## 配置使用kubectl工具
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

## 查看节点状态(状态NotReady,未配置网络,网络配置成功查看即为ready)
kubectl get nodes
5、应用flannel网络(master节点执行)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
## 确保master、node节点均能能够访问到quay.io这个registery,如果失败,可将kube-flannel.yml下载到本地,替换文件中的quay.io
## 为quay.azk8s.cn,应用flannel网络 kubectl apply -f kube-flannel.yml
6、部署Kubernetes Node
## node节点执行
kubeadm join 192.168.56.112:6443 --token mce4gk.v28qvqgq2yskb4la \
    --discovery-token-ca-cert-hash sha256:2bde976b1903fce4b1d5e56187d1b1a63958f0a53b35bf50e31450a0498d9e88

## 片刻后输出结果类似
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.16" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

## 回到master节点查看节点状态,至此集群搭建完毕
kubectl get nodes
## 重置集群
kubeadm reset

## 当前输出日志
journalctl -f  

## 只看当前的kubelet进程日志
journalctl -f -u kubelet  

## 查看k8s系统级的pod状态
kubectl get pods -n kube-system

## 查看k8s系统级的pod详细状态
kubectl get pod -o wide -n kube-system
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值