Kubeadmin来快速搭建一个k8s集群:
二进制搭建适合大集群,50台以上的主机,
但是kubeadm更适合中小企业的业务集群
环境:
Master:20.0.0.71 2核4G 或者4核8G docker kubelet kubectl flannel
Node1:20.0.0.72 2核4G 或者4核8G docker kubelet kubectl flannel
Node2: 20.0.0.73 2核4G docker kubelet kubectl flannel
Harbor节点:20.0.0.74 2核4Gdocker-compose harbor
实验开始:
所有节点
systemctl stop firewalld
setenforce 0
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
swapoff -a

所有节点
for i in (ls /usr/lib/modules/(uname -r)/kernel/net/netfilter/ipvs|grep -o "^.*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done

改主机名
master1:
hostnamectl set-hostname master01
node1:
hostnamectl set-hostname node01
node2:
hostnamectl set-hostname node02
映射

做内核优化:

vim /etc/sysctl.d/k8s.conf
#开启网桥模式:
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
#网桥的流量传给iptables链,实现地址映射
#关闭ipv6的流量(可选项)
net.ipv6.conf.all.disable_ipv6=1
#根据工作中的实际情况,自定
net.ipv4.ip_forward=1
wq!
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1

sysctl --system
时间同步(全部)
yum install ntpdate -y
ntpdate ntp.aliyun.com
date
所有节点安装docker
资源控制器必须systemd(三台)
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://pkm63jfy.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
EOF
systemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service
docker info | grep "Cgroup Driver"
Cgroup Driver: systemd


安装kuberctl(三台)
//定义kubernetes源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[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
EOF
yum install -y kubelet-1.20.15 kubeadm-1.20.15 kubectl-1.20.15

//查看初始化需要的镜像
kubeadm config images list --kubernetes-version 1.20.15
k8s.gcr.io/kube-apiserver:v1.20.15
k8s.gcr.io/kube-controller-manager:v1.20.15
k8s.gcr.io/kube-scheduler:v1.20.15
k8s.gcr.io/kube-proxy:v1.20.15
取消同步操作
主节点操作:
//查看初始化需要的镜像
kubeadm config images list --kubernetes-version 1.20.15

解析:
Pause:特殊的pod
Pause会在节点上创建一个网络命名空间,其他容器可以加入这个网络命名空间
Pod里面的容器可能使用不同的代码架构编写,可以在一个网络空间里面实现通信,协调这个命名空间里面的资源(实现pod内容器的兼容性)
Kubeadm安装的k8s都是以pod的形式运行在kube-system这个空间当中
Kubeletnode管理器可以进行系统控制

kubeadm init \
--apiserver-advertise-address=20.0.0.71 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.20.15 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--token-ttl=0
第二行:声明master节点的apiserver法人监听地址
第三行:声明拉取镜像的仓库,使用阿里云
第四行:k8s的版本号
第五行:所有server的对外代理地址都是10.96.0.0/16
第六行:所有pod的IP地址的网段
第七行:默认的token的有效期,默认是24小时,0表示永不过期

将黑色代码,复制到两个node节点,node1,node2执行
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown (id -u):(id -g) $HOME/.kube/config
systemctl restart kubelet

初始化后需要修改 kube-proxy 的 configmap,开启 ipvs
kubectl edit cm kube-proxy -n=kube-system
修改mode: ipvs


Systemctl restart kublet

kubectl get nodes


kubectl get cs发现集群不健康,更改以下两个文件

修改监听地址
vim /etc/kubernetes/manifests/kube-scheduler.yaml
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
systemctl restart kubelet








kubectl get pods -n kube-system

部署网络(三台)
//所有节点上传 flannel 镜像 flannel.tar 和网络插件 cni-plugins-linux-amd64-v0.8.6.tgz 到 /opt 目录,
master节点上传 kube-flannel.yml 文件

cd /opt
docker load < flannel.tar
mv /opt/cni /opt/cni_bak
mkdir -p /opt/cni/bin
tar zxvf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/bin
主节点:
//在 master 节点创建 flannel 资源
kubectl apply -f kube-flannel.yml

kubectl get node

删除节点:
kubectl delete node master01

kubectl get pod -n kube-system

证书有效期
#证书有效期的问题,kubeadm默认只有1年:
openssl x509 -in /etc/kubernetes/pki/ca.crt -noout -text | grep Not
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text | grep Not
chmod 777 update-kubeadm-cert.sh all
./update-kubeadm-cert.sh all

看有效期


运行脚本改日期

赋权,然后运行脚本

//在master节点查看节点状态
kubectl get nodes
kubectl get pods -n kube-system

添加自动补齐

集群已经搭建完毕

验证
创建镜像
kubectl create deployment nginx --image=nginx --replicas=3

kubectl get pods

kubectl describe pod

pod后面加name,可以查看状态
做个server
kubectl get svc


Curl +ip :31038
搭建仓库:
Test4:
//上传 harbor-offline-installer-v2.8.1.tgz 和 docker-compose 文件到 /opt 目录
cd /opt
cp docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose



tar zxvf harbor-offline-installer-v2.8.1.tgz


cd harbor/

vim harbor.yml








安装 部署与k8s集群对接的Harbor仓库
//上传 harbor-offline-installer-v2.8.1.tgz 和 docker-compose 文件到 /opt 目录
cd /opt
cp docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose
tar zxvf harbor-offline-installer-v2.8.1.tgz
cd harbor/
vim harbor.yml
hostname = hub.test.com
https:
https port for harbor, default is 443
port: 443
The path of cert and key files for nginx
certificate: /data/cert/server.crt
private_key: /data/cert/server.key
harbor_admin_password = 123456
//生成证书
mkdir -p /data/cert
cd /data/cert
#生成私钥
openssl genrsa -des3 -out server.key 2048
输入两遍密码:123456
openssl genrsa: 用于生成 RSA 密钥。
-des3: 使用 Triple DES 加密算法对生成的私钥进行加密。
-out server.key: 指定生成的私钥文件的名称为 server.key。
2048: 指定 RSA 密钥的位数为 2048 位。
#生成证书签名请求文件
openssl req -new -key server.key -out server.csr
输入私钥密码:123456
输入国家名:CN
输入省名:BJ
输入市名:BJ
输入组织名:TEST
输入机构名:TEST
输入域名:hub.kgc.com
输入管理员邮箱:admin@test.com
其它全部直接回车
#备份私钥
cp server.key server.key.org
#清除私钥密码
openssl rsa -in server.key.org -out server.key
输入私钥密码:123456
#签名证书
openssl x509 -req -days 1000 -in server.csr -signkey server.key -out server.crt
chmod +x /data/cert/*
cd /opt/harbor/
./prepare
./install.sh
在本地使用火狐浏览器访问:https://20.0.0.74
添加例外 -> 确认安全例外
用户名:admin
密码:123456
Node2
mkdir -p /etc/docker/certs.d/hub.test.com/

Harbor操作
scp -r data/ root@20.0.0.72:/



映射

vim /lib/systemd/system/docker.service

##修改
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=hub.test.com

systemctl daemon-reload
systemctl restart docker

登录
docker login -u admin -p 123456 https://hub.test.com

测试是否成功

docker pull nginx
docker tag nginx:latest hub.test.com/library/nginx:v1
docker push hub.test.com/library/nginx:v1

Node3也是同样的操作
mkdir -p /etc/docker/certs.d/hub.test.com/
scp -r data/ root@20.0.0.73:/
mkdir -p /etc/docker/certs.d/hub.test.com/
cd /data/cert/
cp server.crt server.csr server.key /etc/docker/certs.d/hub.test.com/
cd /etc/docker/certs.d/hub.test.com/

vim /etc/hosts
vim /lib/systemd/system/docker.service
#添加
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=hub.test.com
systemctl daemon-reload
systemctl restart docker
docker login -u admin -p 123456 https://hub.test.com
演示从仓库拉取镜像
kubectl create deployment nginx --image=hub.test.com/library/nginx:v1 --replicas=3

拉取失败
kubectl get pods

把harbor权限改为公开
即可成功

基于deployments,创建的无法通过delete删除,必须要先删除deployments.apps,如上图所示
绑定到集群,#获取token值


创建用户,获取token


浏览器登录