名称主机部署服务
master192.168.91.10docker、kubeadm、kubelet、kubectl、flannel
node01192.168.91.11docker、kubeadm、kubelet、kubectl、flannel
node02192.168.91.20docker、kubeadm、kubelet、kubectl、flannel
Harbor192.168.91.30docker、docker-compose、harbor-offline-v1.2.2

 1.系统初始化配置:

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						#交换分区必须要关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab		#永久关闭swap分区,&符号在sed命令中代表上次匹配的结果
 
#加载 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

#修改主机名
hostnamectl set-hostname master01
hostnamectl set-hostname node01
hostnamectl set-hostname node02
#域名解析
vim /etc/hosts
192.168.91.10 master01
192.168.91.11 node01
192.168.91.20 node02
#调整内核参数
cat > /etc/sysctl.d/kubernetes.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
net.ipv4.ip_forward=1
EOF

#生效参数
sysctl --system  

2.所有节点部署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
cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  }
}
EOF

#使用Systemd管理的Cgroup来进行资源控制与管理,因为相对Cgroupfs而言,Systemd限制CPU、内存等资源更加简单和成熟稳定。
#日志使用json-file格式类型存储,大小为100M,保存在/var/log/containers目录下,方便ELK等日志系统收集和管理日志。

systemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service 

docker info | grep "Cgroup Driver"
Cgroup Driver: systemd

3.所有节点安装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

#yum安装组件
yum install -y kubelet-1.20.11 kubeadm-1.20.11 kubectl-1.20.11

#开机自启kubelet
systemctl enable kubelet.service

#K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启

4.部署K8S集群:

//查看初始化需要的镜像
kubeadm config images list

//在 master 节点上传 v1.20.11.zip 压缩包至 /opt 目录
unzip v1.20.11.zip -d /opt/k8s
cd /opt/k8s/v1.20.11
for i in $(ls *.tar); do docker load -i $i; done

//复制镜像和脚本到 node 节点,并在 node 节点上执行脚本加载镜像文件
scp -r /opt/k8s root@node01:/opt
scp -r /opt/k8s root@node02:/opt

#在node节点上执行加载镜像文件
cd /opt/k8s/v1.20.11
for i in $(ls *.tar); do docker load -i $i; done

kubeadm config print init-defaults > /opt/kubeadm-config.yaml

cd /opt/
vim kubeadm-config.yaml
......
11 localAPIEndpoint:
12   advertiseAddress: 192.168.91.10		#指定master节点的IP地址
13   bindPort: 6443
......
34 kubernetesVersion: v1.20.11				#指定kubernetes版本号
35 networking:
36   dnsDomain: cluster.local
37   podSubnet: "10.244.0.0/16"				#指定pod网段,10.244.0.0/16用于匹配flannel默认网段
38   serviceSubnet: 10.96.0.0/16			#指定service网段
39 scheduler: {}

#末尾再添加以下内容
--- 

apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs									#把默认的kube-proxy调度方式改为ipvs模式

kubeadm init --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.log
#--experimental-upload-certs 参数可以在后续执行加入节点时自动分发证书文件,K8S V1.16版本开始替换为 --upload-certs
#tee kubeadm-init.log 用以输出日志  

//查看 kubeadm-init 日志
less kubeadm-init.log

//kubernetes配置文件目录
ls /etc/kubernetes/

//存放ca等证书和密码的目录
ls /etc/kubernetes/pki		

5.设定kubectl:

kubectl需经由API server认证及授权后方能执行相应的管理操作,kubeadm 部署的集群为其生成了一个具有管理员权限的认证配置文件 /etc/kubernetes/admin.conf,它可由 kubectl 通过默认的 “$HOME/.kube/config” 的路径进行加载。

mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

//如果 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.10.19		#修改成k8s的控制节点master01的ip
把httpGet:字段下的hosts由127.0.0.1变成192.168.10.19(有两处)
#- --port=0					# 搜索port=0,把这一行注释掉

systemctl restart kubelet

查看node节点状态:
kubectl get node

6.所有节点部署网络插件flannel:

//所有节点上传flannel-cni-v1.2.0.tar ,flannel-v0.22.2.tar镜像node 到 /opt 目录,master节点上传cni-plugins-linux-amd64-v1.2.0.tgz,(由于版本更新后 不使用这个) flannel-cni-v1.2.0.tar ,flannel-v0.22.2.tar文件

cd /opt 
unzip  kuadmin.zip
docker load -i flannel-v0.22.2.tar
docker load -i flannel-cni-v1.2.0.tar

#将flannel-cni-v1.2.0.tar flannel-v0.22.2.tar传给node01和node02

scp flannel-cni-v1.2.0.tar flannel-v0.22.2.tar node01:/opt/ 
scp flannel-cni-v1.2.0.tar flannel-v0.22.2.tar node02:/opt/ 
docker load -i flannel-v0.22.2.tar
docker load -i flannel-cni-v1.2.0.tar

//在 master 节点创建 flannel 资源(将kube-flannel.yml传入/opt )
kubectl apply -f kube-flannel.yml
namespace/kube-flannel created

#在 node 节点上执行 kubeadm join 命令加入群集
kubeadm join 192.168.91.10:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:8846f1ebde6460f6f8905e3b313bb9828dcfd063606c45944c3ea59d93e4d2b2

#在master节点查看节点状态

kubectl get nodes

kubectl get pods -n kube-system

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

#curl访问测试:
kubectl get pod -owide
curl 10.244.2.2

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

kubectl get svc

//扩展3个副本

kubectl scale deployment nginx --replicas=3
kubectl get pods -o wide

7.部署 Dashboard:

#上传 recommended.yaml 文件到 /opt/k8s 目录中
cd /opt/k8s
vim recommended.yaml
#默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001     #添加
  type: NodePort          #添加
  selector:
    k8s-app: kubernetes-dashboard
	
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
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

//修改主机名

hostnamectl set-hostname harbor
//所有节点加上主机名映射

echo '192.168.91.30 www.benet.com' >> /etc/hosts
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
cat > /etc/docker/daemon.json <<EOF
{
  "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.benet.com"]
}
EOF
 
systemctl start docker
systemctl enable docker
//所有 node 节点都修改 docker 配置文件,加上私有仓库配置

cat > /etc/docker/daemon.json <<EOF
{
  "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.benet.com"]
}
EOF
 
systemctl daemon-reload
systemctl restart docker
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐