集群布置

配置信息

节点 ip地址 装置
master(2C/4G,cpu核心数要求大于2) 192.168.1.17 docker、kubeadm、kubelet、kubectl、flannel
node01(2C/2G) 192.168.1.18 docker、kubeadm、kubelet、kubectl、flannel
node02(2C/2G) 192.168.1.19 docker、kubeadm、kubelet、kubectl、flannel
Harbor节点(hub.kgc.com) 192.168.1.11 docker、docker-compose、harbor-offline-v1.2.2

布置流程

  1. 在一切节点上装置Docker和kubeadm
  2. 布置Kubernetes Master
  3. 布置容器网络插件
  4. 布置 Kubernetes Node,将节点参加Kubernetes集群中
  5. 布置 Dashboard Web 页面,可视化检查Kubernetes资源
  6. 布置 Harbor 私有库房,寄存镜像资源

环境预备

#一切节点,封闭防火墙规矩,封闭selinux,封闭swap交流 
systemctl stop firewalld 
systemctl disable firewalld 
setenforce 0 
sed -i 's/enforcing/disabled/' /etc/selinux/config 
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X 
swapoff -a      #交流分区有必要要封闭 
#永久封闭swap分区,&符号在sed指令中代表前次匹配的结果 
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

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

#修正主机名 
hostnamectl set-hostname master01 
hostnamectl set-hostname node01 
hostnamectl set-hostname node02

kubeadmin搭建Kubernetes

#一切节点修正hosts文件,修正主机名并增加主机映射 
vim /etc/hosts 
192.168.1.17 master01 
192.168.1.18 node01 
192.168.1.19 node02

kubeadmin搭建Kubernetes

#调整内核参数 
cat > /etc/sysctl.d/kubernetes.conf << EOF 
net.bridge.bridge-nf-call-ip6tables=1    #敞开网桥模式,可将网桥的流量传递给iptables链 
net.bridge.bridge-nf-call-iptables=1
net.ipv6.conf.all.disable_ipv6=1       #封闭ipv6协议
net.ipv4.ip_forward=1 
EOF
#收效参数 
sysctl --system    

kubeadmin搭建Kubernetes

一切节点装置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
vim /etc/docker/daemon.json 
{ 
  "registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"], 
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file", 
  "log-opts": { 
    "max-size": "100m" 
    } 
 }
 #运用Systemd办理的Cgroup来进行资源操控与办理,由于相对Cgroupfs而言,Systemd限制CPU、内存等资源更加简单和成熟稳定。 
 #日志运用json-file格局类型存储,大小为100M,保存在/var/log/containers目录下,方便ELK等日志体系收集和办理日志。
systemctl daemon-reload 
systemctl start docker.service 
systemctl enable docker.service
docker info | grep "Cgroup Driver" 

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

一切节点装置kubeadm,kubelet和kubectl

#定义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    

kubeadmin搭建Kubernetes

#装置软件
yum install -y kubelet-1.21.3 kubeadm-1.21.3 kubectl-1.21.3
#开机自启kubelet 
systemctl enable kubelet.service 
#K8S经过kubeadm装置出来以后都是以Pod方法存在,即底层是以容器方法运行,所以kubelet有必要设置开机自启    

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

布置K8S集群

#初始化kubeadm
kubeadm init \
--apiserver-advertise-address=192.168.1.17 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.21.3 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--token-ttl=0
*****可选参数*****
--apiserver-advertise-address:apiserver通告给其他组件的IP地址,一般应该为Master节点的用于集群内部通讯的IP地址,0.0.0.0表示节点上一切可用地址
--apiserver-bind-port:apiserver的监听端口,默许是6443
--cert-dir:通讯的ssl证书文件,默许/etc/kubernetes/pki
--control-plane-endpoint:操控台平面的共享终端,可以是负载均衡的ip地址或者dns域名,高可用集群时需求增加
--image-repository:拉取镜像的镜像库房,默许是k8s.gcr.io
--kubernetes-version:指定kubernetes版本
--pod-network-cidr:pod资源的网段,需与pod网络插件的值设置共同。Flannel网络插件的默许为10.244.0.0/16,Calico插件的默许值为192.168.0.0/16--service-cidr:service资源的网段
--service-dns-domain:service全域名的后缀,默许是cluster.local
--token-ttl:默许token的有效期为24小时,假如不想过期,可以加上 --token-ttl=0 这个参数

kubeadmin搭建Kubernetes

#初始化kubeadm之后会出现提示
#履行以下指令可运用kubectl办理工具#
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
*****注释*****
kubectl需经由API server认证及授权后方能履行相应的办理操作,kubeadm 布置的集群为其生成了一个具有办理员权限的认证配置文件 /etc/kubernetes/admin.conf,它可由 kubectl 经过默许的 “$HOME/.kube/config” 的途径进行加载。

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

#假如 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.1.17 #修正成k8s的操控节点master01的ip 
把httpGet:字段下的hosts由127.0.0.1变成192.168.1.17(有两处) 
#- --port=0 # 查找port=0,把这一行注释掉
systemctl restart kubelet  #重启kubelet

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

#一切节点上传flannel镜像 flannel.tar 到 /opt 目录
#master节点上传 kube-flannel.yml 文件到 /opt 目录
cd /opt
docker load < flannel.tar
#在 master 节点创立 flannel 资源
kubectl apply -f kube-flannel.yml 

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

#master节点token制造
kubeadm token create --print-join-command
#在两个node 节点上履行 kubeadm join 指令参加群集
kubeadm join 192.168.1.17:6443 --token ep1s3u.k1s0xryojd378x7o     --discovery-token-ca-cert-hash sha256:27df0763329044e7588521744e01f4815c349bdefeca691a3b2c482ec16fb85d

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

#在master节点检查节点状况
kubectl get nodes
kubectl get pods -n kube-system

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

#测试 pod 资源创立 
kubectl create deployment nginx --image=nginx
kubectl get pods -o wide

kubeadmin搭建Kubernetes

#暴露端口提供服务 
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get svc

kubeadmin搭建Kubernetes

#测试拜访 
curl <http://node02:32749>

kubeadmin搭建Kubernetes

#扩展3个副本 
kubectl scale deployment nginx --replicas=3 
kubectl get pods -o wide

kubeadmin搭建Kubernetes

装置Harbor私有库房

#修正主机名 
hostnamectl set-hostname hub.lwx.com
#一切节点加上主机名映射 
echo '192.168.1.11 hub.lwx.com' >> /etc/hosts

kubeadmin搭建Kubernetes

#装置 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
mkdir /etc/docker 
vim /etc/docker/daemon.json 
{
  "registry-mirrors": ["<https://6ijb8ubo.mirror.aliyuncs.com>"], 
  "exec-opts": ["native.cgroupdriver=systemd"], 
  "log-driver": "json-file", 
  "log-opts": {
    "max-size": "100m" 
    },
  "insecure-registries": ["https://hub.lwx.com"] 
}
systemctl start docker

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

#装置 Harbor
#上传 harbor-offline-installer-v1.2.2.tgz 和 docker-compose 文件到 /opt 目录
cd /opt
cp docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose
tar xf harbor-offline-installer-v1.2.2.tgz
cd harbor/
vim harbor.cfg
#第五行
hostname = hub.lwx.com
#第九行
 ui_url_protocol = https
#第24行
ssl_cert = /data/cert/server.crt
#第25行
ssl_cert_key = /data/cert/server.key
#第59行
harbor_admin_password = Harbor12345

kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes
kubeadmin搭建Kubernetes

#生成证书
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
输入组织名:LWX
输入组织名:LWX
输入域名:hub.lwx.com
输入办理员邮箱:admin@lwx.com
其它悉数直接回车

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

#备份私钥
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/
./install.sh
#在本地运用火狐浏览器拜访:https://hub.lwx.com
增加破例 -> 承认安全破例
用户名:admin
暗码:Harbor12345

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

#在一个node节点上登录harbor
docker login -u admin -p Harbor12345 https://hub.lwx.com
*****假如登陆失利*****
vim /etc/docker/daemon.json
参加以下内容:
 "insecure-registries": ["https://hub.lwx.com"]
 systemctl restart docker  重启docker

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

#上传镜像
docker tag nginx:latest hub.lwx.com/library/nginx:v1 
docker push hub.lwx.com/library/nginx:v1

kubeadmin搭建Kubernetes

#在master节点上删去之前创立的nginx资源
kubectl delete deployment nginx
kubectl create deployment nginx-deployment --image=hub.lwx.com/library/nginx:v1 --port=80 --replicas=3
kubectl expose deployment nginx-deployment --port=30000 --target-port=80
kubectl get svc,pods

kubeadmin搭建Kubernetes

yum install ipvsadm -y
ipvsadm -Ln
curl 10.110.21.54:30000
kubectl edit svc nginx-deployment
#29行
 type: NodePort	  #把调度战略改成NodePort

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

kubectl get svc
浏览器拜访:
192.168.1.17:30401
192.168.1.18:30401
192.168.1.19:30401

kubeadmin搭建Kubernetes

kubeadmin搭建Kubernetes

#将cluster-admin人物权限授予用户system:anonymous
kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous

kubeadmin搭建Kubernetes

########### 内核参数优化计划 ##########
cat > /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0									#禁止运用 swap 空间,只有当体系内存不足(OOM)时才答应运用它
vm.overcommit_memory=1							#不检查物理内存是否够用
vm.panic_on_oom=0								#敞开 OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963							#指定最大文件句柄数
fs.nr_open=52706963								#仅4.4以上版本支撑
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF

kubeadmin搭建Kubernetes