Centos7.9安装k8s图文详解

Posted *郑*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7.9安装k8s图文详解相关的知识,希望对你有一定的参考价值。

Kubernetes用两种部署方式

1. kubeadm

Kubeadm是一个k8s部署工具,提供kubeadm inint和 kubeadm join,用于快速部署Kubenetes集群

2. 二进制部署

从github下载二进制包,手动部署每个组件,组成Kubernetes集群.

Kubeadm部署步骤

步骤中为说明节点的步骤都是master,node节点都执行

节点主机名ip配置
mastermaster192.168.0.1102c 2g 20g
node1node1192.168.0.1112c 2g 20g

- 服务器信息

cat /etc/redhat-release

- 安装所需工具

yum -y install vim
yum -y install wget

- 修改主机名

#master
hostnamectl set-hostname master
#node1
hostnamectl set-hostname node1

- 编辑hosts

vim /etc/hosts

#增加一下内容
192.168.0.110 master
192.168.0.111 ndoe1

- 安装ntpdate并同步时间

yum -y install ntpdate
ntpdate ntp1.aliyun.com
systemctl start ntpdate
sustemctl enable ntpdate
systemctl status ntpdate

- 安装并配置 bash-completion,添加命令自动补充

yum -y install bash-completion
source /etc/profile

- 关闭防火墙、或者开通指定端口

systemctl stop firewalld.service 
systemctl disable firewalld.service

- 关闭 selinux

sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久

- 关闭 swap

free -h
sudo swapoff -a
sudo sed -i 's/.*swap.*/#&/' /etc/fstab
free -h

- 安装 Containerd

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 
sudo yum install -y containerd.io

systemctl stop containerd.service

cp /etc/containerd/config.toml /etc/containerd/config.toml.bak
sudo containerd config default > $HOME/config.toml
sudo cp $HOME/config.toml /etc/containerd/config.toml
# 修改 /etc/containerd/config.toml 文件后,要将 docker、containerd 停止后,再启动
sudo sed -i "s#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g" /etc/containerd/config.toml
# https://kubernetes.io/zh-cn/docs/setup/production-environment/container-runtimes/#containerd-systemd
# 确保 /etc/containerd/config.toml 中的 disabled_plugins 内不存在 cri
sudo sed -i "s#SystemdCgroup = false#SystemdCgroup = true#g" /etc/containerd/config.toml

#启动containerd
systemctl start containerd.service
systemctl status containerd.service

- 添加阿里云 k8s 镜像仓库

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
# 是否开启本仓库
enabled=1
# 是否检查 gpg 签名文件
gpgcheck=0
# 是否检查 gpg 签名文件
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

- 将桥接的 IPv4 流量传递到 iptables 的链

# 设置所需的 sysctl 参数,参数在重新启动后保持不变
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1

EOF

# 应用 sysctl 参数而不重新启动
sudo sysctl --system

- 安装k8s

# 可以安装1.24.0-1.26.3版本,本文使用1.26.0
# sudo yum install -y kubelet-1.24.0-0 kubeadm-1.24.0-0 kubectl-1.24.0-0 --disableexcludes=kubernetes --nogpgcheck


#sudo yum install -y kubelet-1.25.3-0 kubeadm-1.25.3-0 kubectl-1.25.3-0 --disableexcludes=kubernetes --nogpgcheck

# 2022-11-18,经过测试,版本号:1.25.4
# sudo yum install -y kubelet-1.25.4-0 kubeadm-1.25.4-0 kubectl-1.25.4-0 --disableexcludes=kubernetes --nogpgcheck

# 2023-02-07,经过测试,版本号:1.25.5,
# sudo yum install -y kubelet-1.25.5-0 kubeadm-1.25.5-0 kubectl-1.25.5-0 --disableexcludes=kubernetes --nogpgcheck

# 2023-02-07,经过测试,版本号:1.25.6,
# sudo yum install -y kubelet-1.25.6-0 kubeadm-1.25.6-0 kubectl-1.25.6-0 --disableexcludes=kubernetes --nogpgcheck

# 2023-02-07,经过测试,版本号:1.26.0,
# sudo yum install -y kubelet-1.26.0-0 kubeadm-1.26.0-0 kubectl-1.26.0-0 --disableexcludes=kubernetes --nogpgcheck

# 2023-02-07,经过测试,版本号:1.26.1,
# sudo yum install -y kubelet-1.26.1-0 kubeadm-1.26.1-0 kubectl-1.26.1-0 --disableexcludes=kubernetes --nogpgcheck

# 2023-03-02,经过测试,版本号:1.26.2,
# sudo yum install -y kubelet-1.26.2-0 kubeadm-1.26.2-0 kubectl-1.26.2-0 --disableexcludes=kubernetes --nogpgcheck

sudo yum install -y kubelet-1.26.3-0 kubeadm-1.26.3-0 kubectl-1.26.3-0 --disableexcludes=kubernetes --nogpgcheck

systemctl daemon-reload
sudo systemctl restart kubelet
sudo systemctl enable kubelet

- 初始化,只需要在master节点

kubeadm init \\
 --apiserver-advertise-address=192.168.0.110 \\
 --image-repository registry.aliyuncs.com/google_containers

执行完成

如果报如下错误,执行一下命令,再执行上面命令

# 执行命令
modprobe br_netfilter
echo 1 > /proc/sys/net/ipv4/ip_forward

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

kubectl cluster-info
# 初始化失败后,可进行重置,重置命令:kubeadm reset

- node节点加入集群,node节点执行

#复制master初始化后输出的命令执行, 需要注意是node节点执行
kubeadm join 192.168.0.110:6443 --token srvub0.trjoe8kvdn3paf0o \\
        --discovery-token-ca-cert-hash sha256:ef424bb8692efdb5ba61332a08868db7e6af81e0147e928e89c3b641f46ad9a9


如果清屏可以在master执行以下命令,查看master节点初始化token

kubeadm token create --print-join-command

- master查看状态

kubectl get pods --all-namespaces -o wide

kubectl get nodes -o wide

maste节点配置网络,使用Calico

# 下载
wget --no-check-certificate https://projectcalico.docs.tigera.io/archive/v3.25/manifests/calico.yaml
# 修改 calico.yaml 文件
vim calico.yaml
# 在 - name: CLUSTER_TYPE 下方添加如下内容
- name: CLUSTER_TYPE
  value: "k8s,bgp"
  # 下方为新增内容
- name: IP_AUTODETECTION_METHOD
  value: "interface=网卡名称"
  # INTERFACE_NAME=ens33
# 配置网络
kubectl apply -f calico.yaml

- master查看pods,nodes

kubectl get nodes -o wide

kubectl get pods --all-namespaces -o wide


需要等待几分钟,再次查看pods,nodes,如下图为正常状态

master创建nginx服务

创建命名空间

kubectl create namespace zlm-k8s
cat > nginx.yaml << EOF
# 创建命名空间  :kubectl create namespace zlm-k8s
# 创建 pod    :kubectl apply -f nginx-deployment.yaml
# 查看 pod    :kubectl -n zlm-k8s get pod -o wide
# 查看 pod    :kubectl -n zlm-k8s get pod -o wide
# 进入 pod    :kubectl -n zlm-k8s exec -it pod名称 bash
# 编辑 pod    :kubectl -n zlm-k8s edit deployment nginx-deployment
# 删除 pod    :kubectl -n zlm-k8s delete deployment nginx-deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: zlm-k8s
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.23.2
        ports:
        - containerPort: 80
---
# 创建 Service(不能指定 nodePort) :kubectl -n zlm-k8s expose deployment nginx-deployment --type=NodePort --name=nginx-service
# 编辑 Service                    :kubectl -n zlm-k8s edit service nginx-service
# 删除 Service                    :kubectl -n zlm-k8s delete service nginx-service
# 查看 pod、Service               :kubectl -n 命名空间 get pod,svc -o wide

# https://kubernetes.io/zh-cn/docs/concepts/services-networking/service/
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: zlm-k8s
spec:
  ports:
    - nodePort: 30080
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: nginx
  type: NodePort
EOF
cat nginx.yaml

kubectl apply -f nginx.yaml
#查看pods
kubectl get pods --all-namespaces -o wide


查看nginx状态

kubectl get pods -o wide -n zlm-k8s

# 控制面板:查看pod,svc
kubectl get pod,svc -o wide 

#查看指定命名空间pod,svc
kubectl get pod,svc -o wide -n zlm-k8s

访问192.168.0.110:30080

以上是关于Centos7.9安装k8s图文详解的主要内容,如果未能解决你的问题,请参考以下文章

centos7.9安装k8s

centos7.9安装k8s

centos7.9安装k8s

centos7.9安装k8s

k8s单节点集群二进制部署(步骤详细,图文详解)

k8s单节点集群二进制部署(步骤详细,图文详解)