有大量夸主机的容器需要管理,快速部署应用,快速扩展应用,无缝对接新的应用功能,节省资源,优化硬件资源的使用。
核心角色:
master (管理节点)
node(计算节点)
image(镜像仓库)
[root@registry ~]# yum makecache # 更新缓存
[root@registry ~]# yum install -y docker-distrubution
[root@registry ~]# systemctl enable --now docker-distribution
# 拷贝云盘myos目录到仓库服务器
[root@registry ~]# cd myos
[root@registry ~]# chmod 755 init-img.sh
[root@registry ~]# ./init-img.sh
[root@registry ~]# curl http://192.168.1.100:5000/v2/myos/tags/list
{"name":"myos","tags":["nginx","php-fpm","v1804","httpd"]}
# 把云盘 kubernetes/v1.17.6/base-images中的镜像拷贝到master
[root@master ~]# cd base-images/
[root@master base-images]# for i in *.tar.gz;do docker load -i $(i);done
[root@master base-images]# docker images
[root@master base-images]# docker images | awk '$2!="TAG"{print $1,$2}'|while read _f _v:do
docker tar ${_f}:${_v} 192.168.1.100:5000/${_f##*/}:${_v};
docker push 192.168.1.100:5000/${_f##*/}:${_v};
docker rmi ${_f}:${_v};
done
# 查看验证
[root@master base-images]# curl http://192.168.1.100:5000/v2/_catalog
tab键设置
[root@master ~]# kubectl completion bash > /etc/bash_completion.d/kubectl
[root@master ~]# kubeadm completion bash > /etc/bash_completion.d/kubeadm
[root@master ~]# exit # 退出系统
安装IPVS代理软件包
[root@master ~]# yum install -y ipvsadm ipset # 做成LVS集群
配置master主机环境 首先要开启网桥设备 可以将各容器之间进行通信
[root@master ~]# vim /etc/hosts
192.168.1.21 master
192.168.1.31 node-0001
192.168.1.32 node-0002
192.168.1.33 node-0003
192.168.1.100 registry
[root@master ~]# vim /etc/sysctl.d/k8s.conf # 内核模块配置参数
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
[root@master ~]# modprobe br_netfilter
[root@master ~]# sysctl --system
使用kubeadm部署
应答文件在云盘 kubernetes/v1.17.6/config目录
[root@master ~]# mkdir init ;cd init
# 拷贝kubeadm-init.yaml 到master云主机 init目录下
[root@master init]# kubeadm init --config=kubeadm-init.yaml | tee master-init.log # tee生成日志文件
# 按照提示执行命令
[root@master init]# mkdir -p $HOME/.kube
[root@master init]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master init]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
验证结果
[root@master ~]# kubectl version
[root@master ~]# kubectl get componentstatuses
kubeadm 命令
config: 配置管理命令
help: 查看帮助
init: 初始命令
join: node加入集群的命令
reset: 还原状态命令
token: token凭证管理命令
version:查看版本
# 列出token
kubeadm token list
# 删除token
kubeadm token delete <token>
# 创建token
kubeadm token create --ttl=0 --print-join-command # 无限期 打印安装命令
# 获取token_hash
# 1、查看安装日志 2、在创建token时候显示 3、使用 openssl 计算得到
[root@master ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt |openssl rsa -pubin -outform der |openssl dgst -sha256 -hex
[root@ecs-proxy 5]# cp -a kubernetes/nodejoin /root/
[root@ecs-proxy 5]# cd ~root/nodejoin/
[root@ecs-proxy nodejoin]# vim nodeinit.yaml
... ...
vars:
master: '192.168.1.50:6443'
token: '这里改成你自己的token'
token_hash: 'sha256:这里改成你自己的token ca hash'
... ...
[root@ecs-proxy node-install]# ansible-playbook nodeinit.yaml
验证安装
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady master 130m v1.22.5
node-0001 NotReady <none> 2m14s v1.22.5
node-0002 NotReady <none> 2m15s v1.22.5
node-0003 NotReady <none> 2m9s v1.22.5
# 拷贝 kubernetes/plugins 目录到 master 云主机上
[root@ecs-proxy ~]# rsync -av kubernetes/plugins 192.168.1.50:./
#---------------------------------------------------------------------
[root@master ~]# cd plugins/flannel
[root@master flannel]# docker load -i flannel.tar.xz
[root@master flannel]# docker images|while read i t _;do
[[ "${t}" == "TAG" ]] && continue
[[ "${i}" =~ ^"registry:5000/".+ ]] && continue
docker tag ${i}:${t} registry:5000/plugins/${i##*/}:${t}
docker push registry:5000/plugins/${i##*/}:${t}
docker rmi ${i}:${t} registry:5000/plugins/${i##*/}:${t}
done
修改配置文件并安装
[root@master ~]# vim flannel/kube-flannel.yaml
128: "Network": "10.244.0.0/16",
# 该地址必须与案例3初始化文件 kubeadm-init.yaml 中的 podSubnet 一致
172: image: 192.168.1.100:5000/flannel:v0.12.0-amd64 # 私有仓库
186: image: 192.168.1.100:5000/flannel:v0.12.0-amd64
227行到结尾的所有内容全部删除
验证效果
# 验证节点工作状态
[root@master flannel]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 26h v1.22.5
node-0001 Ready <none> 151m v1.22.5
node-0002 Ready <none> 152m v1.22.5
node-0003 Ready <none> 153m v1.22.5
# 验证容器工作状态
[root@master ~]# kubectl -n kube-system get pods
NAME READY STATUS RESTARTS AGE
coredns-54b6487f4d-t7f9m 1/1 Running 0 64m
coredns-54b6487f4d-v2zbg 1/1 Running 0 64m
etcd-master 1/1 Running 0 64m
kube-apiserver-master 1/1 Running 0 64m
kube-controller-manager-master 1/1 Running 0 64m
kube-flannel-ds-8x4hq 1/1 Running 0 55m
kube-flannel-ds-c5rkv 1/1 Running 0 55m
kube-flannel-ds-sk2gj 1/1 Running 0 55m
kube-flannel-ds-v26sx 1/1 Running 0 55m
kube-proxy-6gprw 1/1 Running 0 58m
kube-proxy-6tfn8 1/1 Running 0 58m
kube-proxy-9t5ln 1/1 Running 0 58m
kube-proxy-f956k 1/1 Running 0 64m
kube-scheduler-master 1/1 Running 0 64m