kubernetes集群编排

目录

k8s 集群部署

集群环境初始化

所有节点安装kubeadm

拉取集群所需镜像

集群初始化

安装flannel网络插件

设置kubectl命令补齐


k8s 集群部署

实验环境

主机名

ip

角色

k8s1(上一章的docker1)

192.168.81.10

reg.westos.org,harbor仓库

k8s2

192.168.81.11

master,k8s集群控制节点

k8s3

192.168.81.12

node,k8s集群工作节点

k8s4

192.168.81.13

node,k8s集群工作节点

所有节点禁用selinux和防火墙

所有节点同步时间和解析

所有节点安装docker-ce

所有节点禁用swap,注意注释掉/etc/fstab文件中的定义

集群环境初始化

所有k8s集群节点执行以下步骤

禁用swap

[root@k8s2 ~]# swapoff -a
[root@k8s2 ~]# vim /etc/fstab
#/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

修改内核参数

[root@k8s2 sysctl.d]# vim docker.conf
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1

[root@k8s2 ~]#  sysctl --system

配置仓库

[root@k8s2 yum.repos.d]# vim docker.repo 
[docker]
name=docker-ce
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/gpgcheck=0

[centos]
name=extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/extras/x86_64/
gpgcheck=0

[root@k8s2 ~]# yum install -y docker-ce
[root@k8s2 ~]# systemctl enable --now docker

配置守护进程

[root@k8s2 ~]# vim /etc/docker/daemon.json
{
        "registry-mirrors": ["https://reg.westos.org"],
        "exec-opts": ["native.cgroupdriver=systemd"],
        "log-driver": "json-file",
        "log-opts": {
                "max-size": "100m"
        },
        "storage-driver": "overlay2"
}

[root@k8s2 ~]# systemctl  restart docker

kubernetes集群编排_第1张图片

所有节点同步docker配置,以及拷贝harbor仓库的证书

[root@k8s1 ~]# cd /etc/docker/
[root@k8s1 docker]# ls
certs.d
[root@k8s1 docker]# scp -r certs.d/ k8s2:/etc/docker/

确保所有k8s节点可以从私有仓库下载镜像

所有节点安装kubeadm

[root@k8s2 yum.repos.d]# vim k8s.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0

[root@k8s2 ~]# yum install -y kubelet-1.23.17-0 kubeadm-1.23.17-0 kubectl-1.23.17-0

[root@k8s2 ~]# systemctl enable --now kubelet

拉取集群所需镜像

[root@k8s1 ~]# docker load -i k8s-v1.23.17.tar

登录仓库

[root@k8s1 ~]# docker login reg.westos.org
Username: admin
Password:

先在harbor仓库上新建一个项目

kubernetes集群编排_第2张图片

[root@k8s1 ~]# docker images |grep google_containers | awk '{print $1":"$2}' | awk -F/ '{system("docker tag "$0" reg.westos.org/k8s/"$3"")}'

[root@k8s1 ~]# docker images  |grep k8s | awk '{system("docker push "$1":"$2"")}'

kubernetes集群编排_第3张图片

集群初始化

[root@k8s2 ~]# kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository reg.westos.org/k8s --kubernetes-version v1.23.17

kubernetes集群编排_第4张图片设置环境变量

[root@k8s2 ~]# export KUBECONFIG=/etc/kubernetes/admin.conf

写入环境变量,确保重启后依然生效

[root@k8s2 ~]# vim .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
export KUBECONFIG=/etc/kubernetes/admin.conf

kubernetes集群编排_第5张图片查看集群状态

[root@k8s2 ~]# kubectl get node
[root@k8s2 ~]# kubectl get pod -A

当前节点还没有就绪,是因为没有安装网路插件,pod还没运行

kubernetes集群编排_第6张图片

安装flannel网络插件

下载flannel网络插件

[root@k8s1 ~]# wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

kubernetes集群编排_第7张图片修改镜像位置

[root@k8s1 ~]# vim kube-flannel.yml
[root@k8s1 ~]# scp kube-flannel.yml k8s2:

kubernetes集群编排_第8张图片

新建项目仓库

kubernetes集群编排_第9张图片

下载镜像

[root@k8s1 docker]# docker pull docker.io/flannel/flannel:v0.21.2
[root@k8s1 docker]# docker pull docker.io/flannel/flannel-cni-plugin:v1.1.2

kubernetes集群编排_第10张图片上传镜像

[root@k8s1 docker]# docker images |grep flannel | awk '{print $1":"$2}' | awk '{system("docker tag "$0" reg.westos.org/"$0"")}'

[root@k8s1 docker]# docker push reg.westos.org/flannel/flannel:v0.21.2
[root@k8s1 docker]# docker push reg.westos.org/flannel/flannel-cni-plugin:v1.1.2

kubernetes集群编排_第11张图片

kubernetes集群编排_第12张图片

部署网络插件

[root@k8s2 ~]# kubectl apply -f kube-flannel.yml
[root@k8s2 ~]# kubectl  -n kube-flannel get pod
[root@k8s2 ~]# kubectl get node
[root@k8s2 ~]# kubectl get pod -A

kubernetes集群编排_第13张图片

扩容节点

[root@k8s3 ~]# kubeadm join 192.168.81.11:6443 --token 02n0ronbcez6a06uu5ogs3
--discovery-token-ca-cert-hash sha256:83a0b7b4f2d5dda0c4105121ba6a3aa8d747eed5386bcf654ceaaf50c66be9ce
[root@k8s4 ~]# kubeadm join 192.168.81.11:6443 --token 02n0ronbcez6a06uu5ogs3
--discovery-token-ca-cert-hash sha256:83a0b7b4f2d5dda0c4105121ba6a3aa8d747eed5386bcf654ceaaf50c66be9ce

kubernetes集群编排_第14张图片

[root@k8s2 ~]# kubectl get node

kubernetes集群编排_第15张图片

设置kubectl命令补齐

[root@k8s2 ~]# yum install -y bash-completion
[root@k8s2 ~]# echo "source <(kubectl completion bash)" >> ~/.bashrc
[root@k8s2 ~]# source  ~/.bashrc

集群升级

部署cri-docker (所有集群节点)

k8s从1.24版本开始移除了dockershim,所以需要安装cri-docker插件才能使用docker

软件下载:GitHub - Mirantis/cri-dockerd: dockerd as a compliant Container Runtime Interface for Kubernetes

安装

[root@k8s2 ~]# rpm -ivh cri-dockerd-0.3.5-3.el7.x86_64.rpm

配置cri-docker

[root@k8s2 ~]# vim /usr/lib/systemd/system/cri-docker.service
[Service]
Type=notify
ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --network-plugin=cni --pod-infra-container-image=reg.westos.org/k8s/pause:3.7

[root@k8s2 ~]# systemctl daemon-reload
[root@k8s2 ~]# systemctl  enable --now cri-docker
[root@k8s2 ~]# ll /var/run/cri-dockerd.sock
srw-rw---- 1 root docker 0 Jan  9 17:51 /var/run/cri-dockerd.sock

kubernetes集群编排_第16张图片kubernetes集群编排_第17张图片

升级master节点

首先上传镜像到harbor仓库,便于升级

reg.westos.org/k8s/kube-apiserver                                v1.24.17   

reg.westos.org/k8s/kube-proxy                                      v1.24.17    

reg.westos.org/k8s/kube-scheduler                               v1.24.17   

reg.westos.org/k8s/kube-controller-manager                 v1.24.17   

reg.westos.org/k8s/etcd                                            3.5.3-0    

reg.westos.org/k8s/pause                                           3.7        

reg.westos.org/k8s/coredns                                         v1.8.6     

 kubernetes集群编排_第18张图片

升级kubeadm

[root@k8s2 ~]# yum install -y kubeadm-1.24.0-0

执行升级

[root@k8s2 ~]# kubeadm upgrade plan

 kubernetes集群编排_第19张图片

修改节点套接字

[root@k8s2 ~]# kubectl edit nodes k8s2
...
kubeadm.alpha.kubernetes.io/cri-socket: unix:///var/run/cri-dockerd.sock

[root@k8s2 ~]# kubeadm upgrade apply v1.24.0

kubernetes集群编排_第20张图片kubernetes集群编排_第21张图片腾空节点

[root@k8s2 ~]# kubectl drain k8s2 --ignore-daemonsets

 kubernetes集群编排_第22张图片

升级kubelet

[root@k8s2 ~]# yum install -y kubelet-1.24.0-0 kubectl-1.24.0-0

配置kubelet使用cri-docker

[root@k8s2 ~]# vim /var/lib/kubelet/kubeadm-flags.env
KUBELET_KUBEADM_ARGS="--pod-infra-container-image=reg.westos.org/k8s/pause:3.7 --container-runtime=remote --container-runtime-endpoint=unix:///var/run/cri-dockerd.sock"

kubernetes集群编排_第23张图片

重启kubelet

[root@k8s2 ~]# systemctl daemon-reload 
[root@k8s2 ~]# systemctl  restart kubelet 

解除节点保护

[root@k8s2 ~]# kubectl uncordon k8s2

kubernetes集群编排_第24张图片

升级worker节点

升级kubeadm

[root@k8s3 ~]# yum install -y kubeadm-1.24.0-0

执行升级

[root@k8s3 ~]# kubeadm upgrade node

kubernetes集群编排_第25张图片腾空节点

[root@k8s2 ~]# kubectl drain k8s3 --ignore-daemonsets    //需要在master节点执行

kubernetes集群编排_第26张图片 

配置kubelet使用cri-docker

[root@k8s3 ~]# vim /var/lib/kubelet/kubeadm-flags.env
KUBELET_KUBEADM_ARGS="--pod-infra-container-image=reg.westos.org/k8s/pause:3.7 --container-runtime=remote --container-runtime-endpoint=unix:///var/run/cri-dockerd.sock"

kubernetes集群编排_第27张图片 

修改节点套接字

[root@k8s2 ~]# kubectl edit nodes k8s3		//需要在master节点执行
...
kubeadm.alpha.kubernetes.io/cri-socket: unix:///var/run/cri-dockerd.sock

kubernetes集群编排_第28张图片重启kubelet

[root@k8s3 ~]# systemctl daemon-reload 
[root@k8s3 ~]# systemctl  restart kubelet 

接触节点保护

[root@k8s2 ~]# kubectl uncordon k8s3		//需要在master节点执行

kubernetes集群编排_第29张图片

其它节点依此类推

不使用docker集群部署

k8s2、k8s3、k8s4在配置前需要重置节点,关闭docker

[root@k8s2 ~]# kubeadm reset
[root@k8s3 ~]# kubeadm reset  --cri-socket unix:///var/run/cri-dockerd.sock
[root@k8s4 ~]# kubeadm reset  --cri-socket unix:///var/run/cri-dockerd.sock

kubernetes集群编排_第30张图片kubernetes集群编排_第31张图片

所有节点清楚iptables规则

[root@k8s2 ~]# iptables -F
[root@k8s2 ~]# iptables -F -t nat

禁用所有节点docker和cri-docker服务

[root@k8s2 ~]# systemctl  disable  docker
[root@k8s2 ~]# systemctl  disable  cri-docker

重置后所有节点重启

之前部署过docker,containerd默认已经安装

修改配置

[root@k8s2 ~]# containerd config default | tee /etc/containerd/config.toml

kubernetes集群编排_第32张图片

[root@k8s2 ~]# cd /etc/containerd/
[root@k8s2 containerd]# vim config.toml
...
sandbox_image = "reg.westos.org/k8s/pause:3.7"
...
SystemdCgroup = true

kubernetes集群编排_第33张图片kubernetes集群编排_第34张图片

拷贝证书

[root@k8s2 containerd]# mkdir -p /etc/containerd/certs.d/reg.westos.org 
[root@k8s2 containerd]# cp /etc/docker/certs.d/reg.westos.org/ca.crt /etc/containerd/certs.d/reg.westos.org/

[root@k8s2 containerd]# systemctl  restart containerd

[root@k8s2 containerd]# scp -r certs.d/ config.toml k8s3:/etc/containerd/
[root@k8s2 containerd]# scp -r certs.d/ config.toml k8s4:/etc/containerd/

[root@k8s3 docker]# systemctl disable --now docker cri-docker
[root@k8s3 docker]# systemctl  enable --now containerd
[root@k8s3 docker]# crictl config runtime-endpoint unix:///run/containerd/containerd.sock

[root@k8s4 ~]# systemctl disable --now docker cri-docker
[root@k8s4 ~]#  systemctl  enable --now containerd
[root@k8s4 ~]# crictl config runtime-endpoint unix:///run/containerd/containerd.sock

启动containerd

[root@k8s2 containerd ]# systemctl  enable containerd
[root@k8s2 containerd ]# systemctl  restart containerd
[root@k8s2 ~]# crictl config runtime-endpoint unix:///run/containerd/containerd.sock
[root@k8s2 ~]# crictl img
[root@k8s2 ~]# crictl pull reg.westos.org/k8s/pause:3.7

kubernetes集群编排_第35张图片

集群初始化

 kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository reg.westos.org/k8s --kubernetes-version v1.24.17

kubernetes集群编排_第36张图片kubernetes集群编排_第37张图片

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