kubeadm快速搭建k8s集群

二进制适合大集群,50台以上主机

kubeadm更适合中下企业的业务集群

架构

master 20.0.0.70 docker kubelet kubeadm kubelet flannel

node1 20.0.0.71 docker kubelet kubeadm kubelet flannel

node 20.0.0.72 docker kubelet kubeadm kubelet flannel

Harbor 20.0.0.73 docker docker-compose Harbor

操作系统初始化配置
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
关闭selinux
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab 

加载 ip_vs 模块
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


根据规划设置主机名(70 71 72)
hostnamectl set-hostname master
hostnamectl set-hostname node01
hostnamectl set-hostname node02

cat >> /etc/hosts << EOF
20.0.0.70 master
20.0.0.71 node01
20.0.0.72 node02
EOF

调整内核参数(所有节点)
cat > /etc/sysctl.d/k8s.conf << EOF
#开启网桥模式,可将网桥的流量传递给iptables链
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
#关闭ipv6协议
net.ipv6.conf.all.disable_ipv6=1
#看实际的生产情况,需要开启ipv6流量,可以不关。
net.ipv4.ip_forward=1
EOF

sysctl --system

时间同步
yum install ntpdate -y
ntpdate ntp.aliyun.com 

所有节点安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2 
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
yum install -y docker-ce docker-ce-cli containerd.io

systemctl start docker.service
systemctl enable docker.service 
节点安装kubeadm,kubelet和kubectl
在70 71 72 主机上操作
定义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  
开机自启kubelet
systemctl enable kubelet.service

查看初始化需要的镜像
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
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.13-0
k8s.gcr.io/coredns:1.7.0
pause:特殊的pod
pause会在节点上创建一个网络命名空间,其他容器可以加入这个网络命名空间,pod里面的容器可能使用不同的代码和架构编写,可以在一个网络空间里面实现通信,协调这个命名空间里面的资源(实现pod内容器的兼容性)

kubeadm安装的k8s组件都是以pod的形式运行在kube-system这个命令空间当中

kubelet的node节点管理器可以进行系统控制



kubeadm init \
--apiserver-advertise-address=20.0.0.70 \
--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

--apiserver-advertise-address:声明master节点的apiserver的监听地址
-image-repository registry.aliyuncs.com/google_containers:声明拉去镜像的仓库,使用阿里云
--kubernetes-version=v1.20.15\ k8s集群的版本号
--service-cidr=10.96.0.0/16\	所有service的--pod-network-cidr=10.244.0.0/16\		kubeadm init \
> --apiserver-advertise-address=192.168.233.91 \
> --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对外代理地址都是10.96.0.0/16
所有pod的IP地址网段

kubeadm join 192.168.233.91:6443 --token j7h4sa.yau6cfyzva2zk9ll \
    --discovery-token-ca-cert-hash sha256:13b961db6119c69691992ef0e33b46a97339290d6ff19d8effe00329e543d28f
#在node节点加入集群

mkdir -p $HOME/.kube
cp admin.conf /root/.kube/config
cd /root/.kube/
chown $(id-u):$(id-g) $HOME/.kube/config

重启kubelet
systemctl restart kubelet

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

kubectl get cs 发现集群不健康,更改以下两个文件
vim /etc/kubernetes/manifests/kube-scheduler.yaml 
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
# 修改如下内容
把--bind-address=127.0.0.1变成--bind-address=192.168.233.91	#修改成k8s的控制节点master01的ip
把httpGet:字段下的hosts由127.0.0.1变成192.168.233.91(有两处)
#- --port=0					# 搜索port=0,把这一行注释掉
 
systemctl restart kubelet

所有节点上传 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 

#删除node节点
kubectl delete node master01

证书有效期的问题,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

./update-kubeadm-cert.sh all

执行脚本即可

在master节点查看节点状态
kubectl get nodes
 
kubectl get pods -n kube-system
测试 pod 资源创建
kubectl create deployment nginx --image=nginx
 
kubectl get pods -o wide
       
//暴露端口提供服务
kubectl expose deployment nginx --port=80 --type=NodePort

进入pod修改页面
 
kubectl get svc
 
 
//测试访问
curl http://20.0.0.71:30067
 
//缩容副本
kubectl scale deployment nginx --replicas=3
kubectl get pods -o wide
搭建镜像仓库
把源码包拖到opt目录下
mv  docker-compose   docker-compose
cp docker-compose /usr/local/bin/
chmod 777 /usr/local/bin/docker-compose

解压Harbor
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 req -new -key server.key -out server.csr
输入私钥密码:123456
输入国家名:CN
输入省名:BJ
输入市名:BJ
输入组织名:TEST
输入机构名:TEST
输入域名:hub.kgc.com
输入管理员邮箱:[email protected]
其它全部直接回车
 
#备份私钥
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.73
添加例外 -> 确认安全例外
用户名:admin
密码:123456

node1节点:
mkdir -p /etc/docker/certs.d/hub.test.com/

在harbor节点把密钥验证目录整个转给node节点:
scp -r /data/ [email protected]:/
scp -r /data/ [email protected]:/

把刚才的生成的密钥文件放入:
cd /etc/docker/certs.d/hub.test.com/
[root@node01 hub.test.com]# ls
server.crt  server.csr  server.key

vim /etc/hosts
20.0.0.73 hub.test.com

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

[root@node01 system]# docker login -u admin -p 123456 https://hub.test.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

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

docker push hub.test.com/library/nginx:v1


//在master节点上删除之前创建的nginx资源
kubectl delete deployment nginx
 
kubectl create deployment myapp-test  --image=hub.test.com/library/nginx:v1 --port=80 --replicas=2
kubectl expose deployment myapp-test  --port=30000 --target-port=80
 
kubectl get svc,pods
 
 
 
yum install ipvsadm -y

ipvsadm -Ln

#通过clusterip+端口号 进行访问
curl 10.96.245.162:30000
 
 
kubectl edit svc nginx-deployment
25   type: NodePort						#把调度策略改成NodePort
 
kubectl get svc

部署 Dashboard
上传 recommended.yaml 文件到 /opt/k8s 目录中
cd /opt/k8s

kubectl apply -f recommended.yaml
创建service account并绑定默认cluster-admin管理员集群角色
kubectl create serviceaccount dashboard-admin -n kube-system

kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin


#获取token值
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

#使用输出的token登录Dashboard
https://20.0.0.70:30001

页面访问结果

kubeadm快速搭建k8s集群_第1张图片

你可能感兴趣的:(kubernetes,容器)