环境准备
第一台节点(主节点): 192.168.123.200 master
第二台节点(从节点): 192.168.123.201 slave
以下文件需要单独下载
云盘地址地址
配置/etc/hosts域名解析(两台)
[root@master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.123.200 master-tz
192.168.123.201 slave01-tz
关闭防火墙、SELINUX和swap(两台)
1.systemctl disable firewalld --now
2.setenforce 0
3./etc/selinux/config中的一行修改为SELINUX=disabled
4.swapoff -a
5./etc/fstab中的swap加注释
#/dev/mapper/centos-swap swap swap defaults 0 0
配置系统内核参数,使流过网桥的流量页进入IPTables/Netfilter(两台)
[root@master ~]# modprobe br_netfilter
[root@master ~]# echo "modprobe br_netfilter" >> /etc/profi
le
[root@master ~]# cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
[root@master ~]# sysctl -p /etc/sysctl.d/k8s.conf
安装基本软件包,要求虚拟主机能够访问外网(两台)
yum -y install wget vim ntpdate get
配置时间同步
ntpdate ntp1.aliyun.com
配置yum源(两台)
[root@master ~]# rm -rf /etc/yum.repos.d/*
[root@master ~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@master ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@master ~]# wget -O kubernetes.sh https://www.xiongan.host/sh/kubernetes.sh && sh kubernetes.sh
开启ipvs(两台)把 ipvs .modules 上传到 /etc/sysconfig/ modules目录下
chmod 755 /etc/sysconfig/modules/ipvs.modules
bash /etc/sysconfig/modules/ipvs.modules
lsmod | grep ip_vs
安装kubeadm和相关工具包(两台)
yum install -y kubelet-1.20.6 kubeadm-1.20.6 kubectl-1.20.6
systemctl enable kubelet
注:docker的版本是20.10.8
注:每个软件包的作用
Kubeadm : kubeadm 是一个工具,用来初始化 k 8s 集群的
kubelet: 安装 在集群所有节点上,用于启动 Pod 的
kubectl:通过 kubectl 可以部署和管理应用,查看各种资源,创建、删除和更新各种组件
kubeadm初始化k8s集群(两台)
上传k8simage-1-20-6.tar.gz到两个节点
docker load -i k8simage-1-20-6.tar.gz
在master节点执行kubeadm命令(master节点)
[root@master ~]# kubeadm init --kubernetes-version=1.20.6 --apiserver-advertise-address=192.168.123.200 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=SystemVerification
kubernetes-version 代表 k8s的版本
apiserver-advertise-address 如果master节点有多个网卡,则需要进行指定
pod-network-cidr 指定pod网络的范围。
image repository registry.aliyuncs.com/google_containers 手动指定仓库地址为
registry.aliyuncs.com/google_containers 。
kubeadm 默认从 k 8s.grc.io 拉取镜像 ,但是 k 8s.gcr.io
访问不到,所以需要指定从 registry.aliyuncs.com/google_containers 仓库拉取镜像
配置kubectl的配置文件config,相当于对kubectl进行授权,这样kubectl命令可以使用这个证书对k8s
集群进行管理
[root@master ~]# mkdir -p $HOME/.kube
[root@master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
进入slave节点执行添加到集群,命令是上图中的
[root@slave01-tz ~]# kubeadm join 192.168.123.200:6443 --token d32tmx.utjgdkqxhy9sk517 \
> --discovery-token-ca-cert-hash sha256:d6a0bb61368c23be10444d7a18eab071b750c97c45186020980714fd57b13bdd
再次查看master节点
[root@master-tz ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master-tz NotReady control-plane,master 8m31s v1.20.6
slave01-tz NotReady <none> 12s v1.20.6
此时集群状态还是NotReady 状态,因为 没有安装网络 插件 。
若要扩充集群(master节点)
master执行
kubeadm token create --print-join-command
结果在新增节点执行
安装k8s网络组件Calico(master节点)
上传calico.yaml文件到master节点。
[root@master ~]# kubectl apply -f calico.yaml
再次使用kubectl get nodes命令查看节点状态为Ready
安装dashboard(master节点)
上传dashboard_2_0_0.tar.gz和metrics-scrapter-1-0-1.tar.gz到两个节点。
上传kubernetes-dashboard.yaml到master节点。
[root@master ~]# docker load -i dashboard_2_0_0.tar.gz
[root@master ~]# docker load -i metrics-scrapter-1-0-1.tar.gz
[root@master ~]# kubectl apply -f kubernetes-dashboard.yaml
[root@master-tz ~]# kubectl get pods -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-7445d59dfd-p572g 1/1 Running 0 10s
kubernetes-dashboard-54f5b6dc4b-5zxpm 1/1 Running 0 10s
说明dasbnoard安装成功了。
查看dashboard的service
[root@master-tz ~]# kubectl get svc -n kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.101.56.238 <none> 8000/TCP 97s
kubernetes-dashboard ClusterIP 10.97.126.230 <none> 443/TCP 97s
修改service type的类型为NodePort
[root@master ~]# kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
[root@master ~]# kubectl get svc -n kubernetes-dashboard
通过浏览器进行访问https://192.168.123.200:30245
通过token登录dashboard(master节点)
创建管理员token,具有查看任何空间的权限,可以管理所有资源对象。
[root@master-tz ~]# kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:kubernetes-dashboard
clusterrolebinding.rbac.authorization.k8s.io/dashboard-cluster-admin created
[root@master-tz ~]# kubectl get secret -n kubernetes-dashboard
NAME TYPE DATA AGE
default-token-scvqs kubernetes.io/service-account-token 3 14m
kubernetes-dashboard-certs Opaque 0 14m
kubernetes-dashboard-csrf Opaque 1 14m
kubernetes-dashboard-key-holder Opaque 2 14m
kubernetes-dashboard-token-bs98s kubernetes.io/service-account-token 3 14m
[root@master-tz ~]# kubectl describe secret kubernetes-dashboard-token-bs98s -n kubernetes-dashboard
Name: kubernetes-dashboard-token-bs98s
Namespace: kubernetes-dashboard
Labels: <none>
Annotations: kubernetes.io/service-account.name: kubernetes-dashboard
kubernetes.io/service-account.uid: d0842b14-e79e-4129-b6e3-bfd3c7039334
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1066 bytes
namespace: 20 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IkdHTy1lQ2tndl9qQ29INUtEMEREMW1iUWhWeENOODB1Q2lOOERSYnN6OTQifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi1iczk4cyIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImQwODQyYjE0LWU3OWUtNDEyOS1iNmUzLWJmZDNjNzAzOTMzNCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlcm5ldGVzLWRhc2hib2FyZDprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.f6iGY-QbB5YQFuaTkU6qR9UBTbFiIcDbpgT40E_ceQZGh3kdWyKzeTB-pWUkrJV1gWFaQt3Er7_brB-T7juO8eywunXkE6Xd_xH7XzaiWbNYFYfr3gMMXI8SmbnpqDKHclqw_tUIgun37ao7YYY_22_mYDdcTSIVFvx9XehK48eJWVfdyy-snuZiTKoR2pKMH0Rau3oXKlw7is8bV7yezeucZnaMPa60N-1KIMAvRM7gXlMX9m_BKiqvxEoru-2FDEoOkiCFXV-juGclxM_Qtn70i9R2JVjPgE5VX_gP7RFHDoXIEwykyjJqOg2fguE9Vy8nKnrfOo0c99aGXxnW_g
使用token值登录
测试创建nginx
拉取nginx镜像
docker pull nginx
创建nginx应用服务
kubectl create deployment ngix-deployment1 --image nginx --port=80 --replicas=2
创建service服务
kubectl expose deployment ngix-deployment1 --name=nginx --port=80 --target-port=80 --type=NodePort
访问nginx服务
[root@master-tz ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 109m nginx NodePort 10.110.242.218 <none> 80:31079/TCP 9s