角色 | 主机名 | IP地址 |
master | test-b-k8s-master | 192.168.11.13 |
node | test-b-k8s-node01 | 192.168.11.14 |
node | test-b-k8s-node02 | 192.168.11.15 |
软件 | 版本 |
OS | Ubuntu 20.04.5 focal |
Docker | Docker version 20.10.21 |
Kubernetes | v1.25.4 |
修改阿里apt源
sudo vi /etc/apt/sources.listdeb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiversedeb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiversedeb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiversedeb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiversedeb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiversesudo apt-get update -y
关闭防火墙
sudo ufw disable
关闭selinux
略...
我安装的ubuntu20默认没有selinux这东西,因此不涉及关闭
关闭swap
sudo swapoff -a # 临时sudo sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
添加hosts
sudo vi /etc/hosts192.168.11.13 test-b-k8s-master192.168.11.14 test-b-k8s-node01192.168.11.15 test-b-k8s-node02
将桥接的IPv4流量传递到iptables的链
sudo vi /etc/sysctl.d/k8s.confnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1sudo sysctl --system
时间同步
sudo apt install ntpdatesudo ntpdate time.windows.com
4. 安装Docker/cri-dockerd/kubeadm/kubelet/kubectl(所有节点)
安装Docker-ce
sudo apt-get -y install apt-transport-https ca-certificates software-properties-commonsudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"sudo apt-get -y update && sudo apt-get -y install docker-ce# 配置镜像下载加速器sudo vi /etc/docker/daemon.json{ "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"]}sudo systemctl daemon-reload && sudo systemctl restart docker && sudo docker info
安装cri-dockerd
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.2.6/cri-dockerd_0.2.6.3-0.ubuntu-focal_amd64.debsudo dpkg -i cri-dockerd_0.2.6.3-0.ubuntu-focal_amd64.deb# 指定依赖镜像地址sudo vi /usr/lib/systemd/system/cri-docker.serviceExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:latestsudo systemctl daemon-reload && sudo systemctl enable cri-docker && sudo systemctl restart cri-docker
安装kubeadm,kubelet和kubectl
# 添加k8s的阿里云apt源sudo apt-get install -y apt-transport-https ca-certificatessudo vi /etc/apt/sources.list.d/kubernetes.listdeb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial mainsudo curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -# 开始安装sudo apt-get update -y && sudo apt-get install -y kubelet kubeadm kubectl && sudo systemctl enable kubelet.service
5. 部署Kubernetes Master
在192.168.11.13(Master)执行
sudo kubeadm init \ --apiserver-advertise-address=192.168.11.13 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.25.4 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16 \ --cri-socket=unix:///var/run/cri-dockerd.sock \ --ignore-preflight-errors=all
配置kubectl使用的连接k8s认证文件
# 普通用户mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config# 管理员(可加到环境变量)export KUBECONFIG=/etc/kubernetes/admin.conf
6. 加入节点
在2台node节点上执行
sudo kubeadm join 192.168.11.13:6443 --token o37091.z858bts6jmth9irz \--discovery-token-ca-cert-hash sha256:628a5b50227c93e465adc1ca380cf335e8f639c15c8a92892f9d22b71ac6c2ac \--cri-socket=unix:///var/run/cri-dockerd.sock
上面用到的token,默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,可以直接使用命令快捷生成:
sudo kubeadm token create --print-join-command
加入后在master查看工作节点
tantianran@test-b-k8s-master:~$ kubectl get nodesNAME STATUS ROLES AGE VERSIONtest-b-k8s-master NotReady control-plane 7m57s v1.25.4test-b-k8s-node01 NotReady <none> 14s v1.25.4test-b-k8s-node02 NotReady <none> 8s v1.25.4tantianran@test-b-k8s-master:~$
7. 部署容器网络接口(CNI)Calico目前工作节点的状态为NotReady,是因为还没有部署网络插件,等部署完就会处于Ready。
Calico是一个纯三层的数据中心网络方案,是目前Kubernetes主流的网络方案。下面开始部署Calico,在master上进行部署操作。
下载calico的YAML:
wget https://docs.projectcalico.org/manifests/calico.yaml
下载完后还需要修改里面定义Pod网络(CALICO_IPV4POOL_CIDR),与前面kubeadm init的 --pod-network-cidr指定的一样。
calico.yaml中CALICO_IPV4POOL_CIDR默认的配置如下:
# - name: CALICO_IPV4POOL_CIDR#电脑 value: "192.168.0.0/16"
取消注释,并修改成与前面kubeadm init的 --pod-network-cidr(10.244.0.0/16)指定的一样。
- name: CALICO_IPV4POOL_CIDRvalue: "10.244.0.0/16"
修改完后文件后,开始部署:
# 部署calicokubectl apply -f calico.yaml
接下来就是比较漫长的等待,等待多久取决于你的网速,如果网络的好的话,就会更快一点,因为它还要拉镜像,而且是3台节点都要拉取相关镜像,下面可以在master上查看位于kube-system命名空间下的pod
tantianran@test-b-k8s-master:~$ kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcalico-kube-controllers-798cc86c47-sg65n 0/1 Pending 0 111scalico-node-9d2gz 0/1 Init:0/3 0 111scalico-node-csnwt 0/1 Init:0/3 0 111scalico-node-g7rk2 0/1 Init:0/3 0 111scoredns-c676cc86f-p2sgs 电脑 0/1 Pending 0 19mcoredns-c676cc86f-pn5zk 0/1 Pending 0 19metcd-test-b-k8s-master 1/1 Running 0 19mkube-apiserver-test-b-k8s-master 1/1 Running 0 19mkube-controller-manager-test-b-k8s-master 1/1 Running 0 19mkube-proxy-6bdwl 1/1 Running 0 12mkube-proxy-d8xgk 1/1 Running 0 19mkube-proxy-lcw2n 1/1 Running 0 11mkube-scheduler-test-b-k8s-master 1/1 Running 0 19mtantianran@test-b-k8s-master:~$
等calico的全部pod状态都为Running的时候,就成功了。到时候你再去查看node的状态,应该也会为Ready了。
刚才提到,在部署calico的过程中3台节点都需要拉取相关镜像,可以到其中1台Node上查看有没有镜像了:
tantianran@test-b-k8s-node02:~$ sudo docker imagesREPOSITORY 电脑 TAG IMAGE ID CREATED SIZEregistry.aliyuncs.com/google_containers/kube-proxy v1.25.4 2c2bc1864279 2 weeks ago 61.7MBcalico/cni v3.24.5 628dd7088041 2 weeks ago 198MBcalico/node v3.24.5 54637cb36d4a 2 weeks ago 226MBregistry.aliyuncs.com/google_containers/pause latest 350b164e7ae1 8 years ago 240kBtantianran@test-b-k8s-node02:~$
经过一小段时间等待后,calico相关的pod已经都为Running了
tantianran@test-b-k8s-master:~$ kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcalico-kube-controllers-798cc86c47-sg65n 1/1 Running 0 7m45scalico-node-9d2gz 1/1 Running 0 7m45scalico-node-csnwt 1/1 Running 0 7m45scalico-node-g7rk2 1/1 Running 0 7m45scoredns-c676cc86f-p2sgs 1/1 Running 0 25mcoredns-c676cc86f-pn5zk 1/1 Running 0 25metcd-test-b-k8s-master 1/1 Running 0 25mkube-apiserver-test-b-k8s-master 1/1 Running 0 25mkube-controller-manager-test-b-k8s-master 1/1 Running 0 25mkube-proxy-6bdwl 1/1 Running 0 17mkube-proxy-d8xgk 1/1 Running 0 25mkube-proxy-lcw2n 1/1 Running 0 17mkube-scheduler-test-b-k8s-master 1/1 Running 0 25mtantianran@test-b-k8s-master:~$
继续查看工作节点的状态,也是Ready状态了
tantianran@test-b-k8s-master:~$ kubectl get nodesNAME STATUS ROLES AGE VERSIONtest-b-k8s-master Ready control-plane 26m v1.25.4test-b-k8s-node01 Ready <none> 19m v1.25.4test-b-k8s-node02 Ready <none> 19m v1.25.4tantianran@test-b-k8s-master:~$
到此为止!CNI网络插件calico就已经完成部署啦!
8. 部署DashboardDashboard是官方提供的一个UI,可用于基本管理K8s资源,也是在master上进行部署操作。github项目地址:https://github.com/kubernetes/dashboard
YAML下载地址:
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
默认Dashboard只能集群内部访问,下载yaml文件后,修改Service为NodePort类型,暴露到外部:
kind: ServiceapiVersion: v1metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kubernetes-dashboardspec: ports: - port: 443 targetPort: 8443 nodePort: 30001 # 增加这个(等会我们访问UI的端口) selector: k8s-app: kubernetes-dashboard type: NodePort # 增加这个(让每个节点都可访问)
开始部署
kubectl apply -f recommended.yaml
这时候,也是需要等待到dashboard相关的pod为Running的状态,它也是要拉镜像,可查看命名空间kubernetes-dashboard下的pod:
tantianran@test-b-k8s-master:~$ kubectl get pods -n kubernetes-dashboardNAME READY STATUS RESTARTS AGEdashboard-metrics-scraper-64bcc67c9c-nnzqv 0/1 ContainerCreating 0 17skubernetes-dashboard-5c8bd6b59-blpgm 0/1 ContainerCreating 0 17stantianran@test-b-k8s-master:~$
等待后,再次查看,dashboard相关的pod已经为Running的状态,说明已经部署好
tantianran@test-b-k8s-master:~$ kubectl get pods -n kubernetes-dashboardNAME READY STATUS RESTARTS AGEdashboard-metrics-scraper-64bcc67c9c-nnzqv 1/1 Running 0 2mkubernetes-dashboard-5c8bd6b59-blpgm 1/1 Running 0 2m
接下来就可以访问任意节点的30001端口访问UI,记得是用https哦
https://192.168.11.13:30001/#/loginhttps://192.168.11.14:30001/#/loginhttps://192.168.11.15:30001/#/login创建登录UI的token
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboardkubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-adminkubectl create token dashboard-admin -n kubernetes-dashboard
将创建好的token复制进去即可完成登录
最后说明一下,以后所有yaml文件都只在Master节点执行(部署操作),切记!
角色 | 主机名 | IP地址 |
master | test-a-k8s-master | 192.168.11.10 |
node | test-a-k8s-node01 | 192.168.11.11 |
node | test-a-k8s-node02 | 192.168.11.12 |
软件 | 版本 |
OS | CentOS Linux release 7.9.2009 |
Docker | Docker version 20.10.21 |
Kubernetes | v1.25.4 |
3.1 关闭防火墙
systemctl stop firewalldsystemctl disable firewalld
3.2 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久setenforce 0 # 临时
3.3 关闭swap
swapoff -a # 临时sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
3.4 根据规划设置主机名
hostnamectl set-hostname <hostname>
3.5 在master添加hosts
cat >> /etc/hosts << EOF192.168.11.10 test-a-k8s-master192.168.11.11 test-a-k8s-node01192.168.11.12 test-a-k8s-node02EOF
3.6 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOFnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1EOFsysctl --system # 生效
3.7 时间同步
yum install ntpdate -yntpdate time.windows.comntpdate ntp1.aliyun.com # 阿里云时钟服务
4. 安装Docker/kubeadm/kubelet/cri-dockerd(所有节点)
4.1 安装Docker-ce
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repoyum -y install docker-cesystemctl enable docker && systemctl start docker
4.2 配置镜像下载加速器
cat > /etc/docker/daemon.json << EOF{ "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"]}EOFsystemctl restart dockerdocker info
4.3 安装cri-dockerd
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.2.6/cri-dockerd-0.2.6-3.el7.x86_64.rpmrpm -ivh cri-dockerd-0.2.6-3.el7.x86_64.rpm
4.4 指定依赖镜像地址
在cri-docker.service配置中的 fd:// 后面增加 --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.7 说明:pause:3.7 也可以指向最新的版本 pause:latest
vi /usr/lib/systemd/system/cri-docker.service# 内容:ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.7systemctl daemon-reload systemctl enable cri-docker && systemctl start cri-docker
4.5 添加阿里云YUM软件源
cat > /etc/yum.repos.d/kubernetes.repo << EOF[kubernetes]name=Kubernetesbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64enabled=1gpgcheck=0repo_gpgcheck=0gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOF
4.6 安装kubeadm,kubelet和kubectl
yum install -y kubelet-1.25.4 kubeadm-1.25.4 kubectl-1.25.4systemctl enable kubelet.service # 这里仅设置enable,后续部署的时候会自动交由kubeadm拉起
5. 部署Kubernetes Master
在192.168.11.10(Master)执行
kubeadm init \ --apiserver-advertise-address=192.168.11.10 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.25.4 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16 \ --cri-socket=unix:///var/run/cri-dockerd.sock \ --ignore-preflight-errors=all
在进行更改之前,运行一些列检查,验证系统状态,有些检查只会触发警告,有些检查会被视为错误并退出kubeadm,因此使用 --ignore-preflight-errors=all 忽略检查中的错误。
初始化完成之后,最后会出现下面这样的提示:
Your Kubernetes control-plane has initialized successfully!To start using your cluster, you need to run the following as a regular user:要开始使用集群,您需要以普通用户身份运行以下命令: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/configAlternatively, if you are the root user, you can run:或者,如果您是root用户,则可以运行: export KUBECONFIG=/etc/kubernetes/admin.confYou should now deploy a pod network to the cluster.Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/Then you can join any number of worker nodes by running the following on each as root:# 记住这个join命令,后面用,作用是在node节点运行这条命令,加入集群kubeadm join 192.168.11.10:6443 --token pk474p.uvc65opv1zs625lq \ --discovery-token-ca-cert-hash sha256:c14439c309345c6a02340bb7df74108c4cde6e0f8393f45a123f347f11b98b57
6. 配置kubectl使用的连接k8s认证文件
# 普通用户要开始使用集群,您需要以普通用户身份运行以下命令:mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config# 或者,如果您是root用户,则可以运行(可以加入到环境变量):export KUBECONFIG=/etc/kubernetes/admin.conf
查看工作节点
[root@test-a-k8s-master ~]# kubectl get nodesNAME STATUS ROLES AGE VERSIONtest-a-k8s-master NotReady control-plane 12m v1.25.4[root@test-a-k8s-master ~]#
7. 加入Kubernetes Node由于网络插件还没有部署,还没有准备就绪 NotReady,先继续
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令并手动加上--cri-socket=unix:///var/run/cri-dockerd.sock
# 在2台node上运行(192.168.11.11、192.168.11.12)kubeadm join 192.168.11.10:6443 --token pk474p.uvc65opv1zs625lq \--discovery-token-ca-cert-hash sha256:c14439c309345c6a02340bb7df74108c4cde6e0f8393f45a123f347f11b98b57 \--cri-socket=unix:///var/run/cri-dockerd.sock
上面用到的token,默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,可以直接使用命令快捷生成:
kubeadm token create --print-join-command
8. 部署容器网络接口(CNI)Calico
Calico是一个纯三层的数据中心网络方案,是目前Kubernetes主流的网络方案。下面开始部署Calico,在master上进行部署操作。
下载calico的YAML:
wget https://docs.projectcalico.org/manifests/calico.yaml
下载完后还需要修改里面定义Pod网络(CALICO_IPV4POOL_CIDR),与前面kubeadm init的 --pod-network-cidr指定的一样。
calico.yaml中CALICO_IPV4POOL_CIDR默认的配置如下:
# - name: CALICO_IPV4POOL_CIDR# value: "192.168.0.0/16"
取消注释,并修改成与前面kubeadm init的 --pod-network-cidr指定的一样。
- name: CALICO_IPV4POOL_CIDRvalue: "10.244.0.0/16"
修改完后文件后,开始部署:
# 部署calicokubectl apply -f calico.yaml# 查看kube-system命名空间下的pod[root@test-a-k8s-master ~]# kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcalico-kube-controllers-798cc86c47-pd8zc 0/1 ContainerCreating 0 82scalico-node-d54c9 0/1 Init:0/3 0 82scalico-node-prpdl 0/1 Init:0/3 0 82scalico-node-rkwzv 1/1 Running 0 82scoredns-c676cc86f-kw9v8 1/1 Running 0 26mcoredns-c676cc86f-vfwz7 1/1 Running 0 26metcd-test-a-k8s-master 1/1 Running 0 26mkube-apiserver-test-a-k8s-master 1/1 Running 0 26mkube-controller-manager-test-a-k8s-master 1/1 Running 0 26mkube-proxy-kv9rx 1/1 Running 0 26mkube-proxy-qphqt 0/1 ContainerCreating 0 11mkube-proxy-z5fjm 0/1 ContainerCreating 0 11mkube-scheduler-test-a-k8s-master 1/1 Running 0 26m[root@test-a-k8s-master ~]#
说明:以后所有yaml文件都只在Master节点执行。
等Calico Pod都Running,节点也会准备就绪。
再次查看工作节点,已经为Ready状态
[root@test-a-k8s-master ~]# kubectl get nodesNAME STATUS ROLES AGE VERSIONtest-a-k8s-master Ready control-plane 13m v1.25.4test-a-k8s-node01 Ready <none> 8m43s v1.25.4test-a-k8s-node02 Ready <none> 7m57s v1.25.4[root@test-a-k8s-master ~]#
9. 部署Dashboard
Dashboard是官方提供的一个UI,可用于基本管理K8s资源,github项目地址:https://github.com/kubernetes/dashboard
YAML下载地址:
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:
kind: ServiceapiVersion: v1metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kubernetes-dashboardspec: ports: - port: 443 targetPort: 8443 nodePort: 30001 # 增加这个(等会我们访问UI的端口) selector: k8s-app: kubernetes-dashboard type: NodePort # 增加这个(让每个节点都可访问)
开始部署
kubectl apply -f recommended.yaml
查看命名空间kubernetes-dashboard下的pod
[root@test-a-k8s-master ~]# kubectl get pods -n kubernetes-dashboardNAME READY STATUS RESTARTS AGEdashboard-metrics-scraper-64bcc67c9c-4jvvw 0/1 ContainerCreating 0 40skubernetes-dashboard-5c8bd6b59-v7nvm 1/1 Running 0 40s[root@test-a-k8s-master ~]#
等dashboard的pod状态都为running时,就完成了
创建登录UI的token
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboardkubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-adminkubectl create token dashboard-admin -n kubernetes-dashboard
访问任意节点的30001的端口都可以登录UI:
https://192.168.11.10:30001/#/loginhttps://192.168.11.11:30001/#/loginhttps://192.168.11.12:30001/#/login将创建好的token复制进去即可完成登录。
本文转载于(喜欢的盆友关注我们哦):https://mp.weixin.qq.com/s/F504vm7xbom4-rY7lgdhKw
电脑