“Install k8s using shell on 2022”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
→‎R
 
(未显示同一用户的12个中间版本)
第2行: 第2行:
 
   ansible的那个也是可以的 拿来改一下  
 
   ansible的那个也是可以的 拿来改一下  
 
这脚本是在 centos7.x上跑的  我再加个 debian 11的
 
这脚本是在 centos7.x上跑的  我再加个 debian 11的
 +
 +
=kuboard提供的脚本=
 +
https://kuboard.cn/install/history-k8s/install-k8s-1.21.x.html
 +
 +
==pre==
 +
有空改一下为 debian11的
 +
从这里开始  Auguest first  monday 
 +
  先做一个  centos7.8的模板
 +
qm clone  116  118 --name k8s-node1-centos7.8
 +
qm clone  116  119 --name k8s-node2-centos7.8
 +
 +
apiserver.linuxsa.org 得hosts master ip
 +
特别是节点上
 +
 +
== 修改 hostname==
 +
<pre>
 +
# 修改 hostname
 +
hostnamectl set-hostname your-new-host-name
 +
# 查看修改结果
 +
hostnamectl status
 +
# 设置 hostname 解析
 +
echo "127.0.0.1  $(hostname)" >> /etc/hosts
 +
</pre>
 +
== install  containerd  kubelet kubeadm kubectl==
 +
<pre>
 +
# 在 master 节点和 worker 节点都要执行
 +
# 最后一个参数 1.21.6 用于指定 kubenetes 版本,支持所有 1.21.x 版本的安装
 +
# 腾讯云 docker hub 镜像
 +
# export REGISTRY_MIRROR="https://mirror.ccs.tencentyun.com"
 +
# DaoCloud 镜像
 +
# export REGISTRY_MIRROR="http://f1361db2.m.daocloud.io"
 +
# 华为云镜像
 +
# export REGISTRY_MIRROR="https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com"
 +
# 阿里云 docker hub 镜像
 +
export REGISTRY_MIRROR=https://registry.cn-hangzhou.aliyuncs.com
 +
curl -sSL https://kuboard.cn/install-script/v1.21.x/install_kubelet.sh | sh -s 1.21.6
 +
 +
</pre>
 +
=== v1.21.x/install_kubelet.sh===
 +
<pre>
 +
#!/bin/bash
 +
 +
# 在 master 节点和 worker 节点都要执行
 +
 +
# 安装 containerd
 +
# 参考文档如下
 +
# https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd
 +
 +
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
 +
overlay
 +
br_netfilter
 +
EOF
 +
 +
sudo modprobe overlay
 +
sudo modprobe br_netfilter
 +
 +
# Setup required sysctl params, these persist across reboots.
 +
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
 +
net.bridge.bridge-nf-call-iptables  = 1
 +
net.ipv4.ip_forward                = 1
 +
net.bridge.bridge-nf-call-ip6tables = 1
 +
EOF
 +
 +
# Apply sysctl params without reboot
 +
sysctl --system
 +
 +
# 卸载旧版本
 +
yum remove -y containerd.io
 +
 +
# 设置 yum repository
 +
yum install -y yum-utils device-mapper-persistent-data lvm2
 +
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
 +
 +
# 安装 containerd
 +
yum install -y containerd.io-1.4.3
 +
 +
mkdir -p /etc/containerd
 +
containerd config default > /etc/containerd/config.toml
 +
 +
sed -i "s#k8s.gcr.io#registry.aliyuncs.com/k8sxio#g"  /etc/containerd/config.toml
 +
sed -i '/containerd.runtimes.runc.options/a\ \ \ \ \ \ \ \ \ \ \ \ SystemdCgroup = true' /etc/containerd/config.toml
 +
sed -i "s#https://registry-1.docker.io#${REGISTRY_MIRROR}#g"  /etc/containerd/config.toml
 +
 +
 +
systemctl daemon-reload
 +
systemctl enable containerd
 +
systemctl restart containerd
 +
 +
 +
# 安装 nfs-utils
 +
# 必须先安装 nfs-utils 才能挂载 nfs 网络存储
 +
yum install -y nfs-utils
 +
yum install -y wget
 +
 +
# 关闭 防火墙
 +
systemctl stop firewalld
 +
systemctl disable firewalld
 +
 +
# 关闭 SeLinux
 +
setenforce 0
 +
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
 +
 +
# 关闭 swap
 +
swapoff -a
 +
yes | cp /etc/fstab /etc/fstab_bak
 +
cat /etc/fstab_bak |grep -v swap > /etc/fstab
 +
 +
# 配置K8S的yum源
 +
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
 +
[kubernetes]
 +
name=Kubernetes
 +
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
 +
enabled=1
 +
gpgcheck=0
 +
repo_gpgcheck=0
 +
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
 +
      http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
 +
EOF
 +
 +
# 卸载旧版本
 +
yum remove -y kubelet kubeadm kubectl
 +
 +
# 安装kubelet、kubeadm、kubectl
 +
# 将 ${1} 替换为 kubernetes 版本号,例如 1.21.6
 +
yum install -y kubelet-${1} kubeadm-${1} kubectl-${1}
 +
 +
crictl config runtime-endpoint /run/containerd/containerd.sock
 +
 +
# 重启 docker,并启动 kubelet
 +
systemctl daemon-reload
 +
systemctl enable kubelet && systemctl start kubelet
 +
 +
containerd --version
 +
kubelet --version
 +
 +
</pre>
 +
 +
== 初始化master==
 +
<pre>
 +
初始化 master 节点
 +
# 只在 master 节点执行
 +
# 替换 x.x.x.x 为 master 节点的内网IP
 +
# export 命令只在当前 shell 会话中有效,开启新的 shell 窗口后,如果要继续安装过程,请重新执行此处的 export 命令
 +
export MASTER_IP=192.168.10.171
 +
# 替换 apiserver.demo 为 您想要的 dnsName
 +
export APISERVER_NAME=apiserver.linuxsa.org
 +
# Kubernetes 容器组所在的网段,该网段安装完成后,由 kubernetes 创建,事先并不存在于您的物理网络中
 +
export POD_SUBNET=10.100.0.0/16
 +
echo "${MASTER_IP}    ${APISERVER_NAME}" >> /etc/hosts
 +
 +
#一定要加上
 +
curl -sSL https://kuboard.cn/install-script/v1.21.x/init_master.sh | sh -s 1.21.6 /coredns
 +
</pre>
 +
=== v1.21.x/init_master.sh===
 +
<pre>
 +
#!/bin/bash
 +
 +
# 只在 master 节点执行
 +
# ${1} 1.21.6  ${2} /coredns
 +
# 脚本出错时终止执行
 +
set -e
 +
 +
if [ ${#POD_SUBNET} -eq 0 ] || [ ${#APISERVER_NAME} -eq 0 ]; then
 +
  echo -e "\033[31;1m请确保您已经设置了环境变量 POD_SUBNET 和 APISERVER_NAME \033[0m"
 +
  echo 当前POD_SUBNET=$POD_SUBNET
 +
  echo 当前APISERVER_NAME=$APISERVER_NAME
 +
  exit 1
 +
fi
 +
 +
 +
# 查看完整配置选项 https://godoc.org/k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2
 +
rm -f ./kubeadm-config.yaml
 +
cat <<EOF > ./kubeadm-config.yaml
 +
---
 +
apiVersion: kubeadm.k8s.io/v1beta2
 +
kind: ClusterConfiguration
 +
kubernetesVersion: v${1}
 +
imageRepository: registry.aliyuncs.com/k8sxio
 +
controlPlaneEndpoint: "${APISERVER_NAME}:6443"
 +
networking:
 +
  serviceSubnet: "10.96.0.0/16"
 +
  podSubnet: "${POD_SUBNET}"
 +
  dnsDomain: "cluster.local"
 +
dns:
 +
  type: CoreDNS
 +
  imageRepository: swr.cn-east-2.myhuaweicloud.com${2}
 +
  imageTag: 1.8.0
 +
 +
---
 +
apiVersion: kubelet.config.k8s.io/v1beta1
 +
kind: KubeletConfiguration
 +
cgroupDriver: systemd
 +
EOF
 +
 +
# kubeadm init
 +
# 根据您服务器网速的情况,您需要等候 3 - 10 分钟
 +
echo ""
 +
echo "抓取镜像,请稍候..."
 +
kubeadm config images pull --config=kubeadm-config.yaml
 +
echo ""
 +
echo "初始化 Master 节点"
 +
kubeadm init --config=kubeadm-config.yaml --upload-certs
 +
 +
# 配置 kubectl
 +
rm -rf /root/.kube/
 +
mkdir /root/.kube/
 +
cp -i /etc/kubernetes/admin.conf /root/.kube/config
 +
 +
</pre>
  
 
=脚本=
 
=脚本=
第9行: 第218行:
 
   有交互界面的 记得yes 还有  hostname什么的得先手工  
 
   有交互界面的 记得yes 还有  hostname什么的得先手工  
 
   deb_install_k8s_master.sh  deb_install_k8s_node.sh
 
   deb_install_k8s_master.sh  deb_install_k8s_node.sh
 +
 +
 +
[https://blog.csdn.net/weixin_42562106/article/details/123100476  Debian 11快速部署K8S]
 +
 +
[https://lework.github.io/2021/04/03/debian-kubeadm-install/ 在 debian 10 上以 kubeadm 方式安装 Kubernetes v1.20.5 ha集群]
 +
 
==Centos7 用这两个==
 
==Centos7 用这两个==
 
   centos_install_k8s_master.sh  centos_install_k8s_node.sh
 
   centos_install_k8s_master.sh  centos_install_k8s_node.sh
第18行: 第233行:
 
[https://blog.csdn.net/marlinlm/article/details/122089803  Debian安装kubernetes1.23.1实战【详细步骤】]
 
[https://blog.csdn.net/marlinlm/article/details/122089803  Debian安装kubernetes1.23.1实战【详细步骤】]
 
==RHEL ==
 
==RHEL ==
 +
 +
=troubleshooting=
 +
 +
 +
[https://www.cnblogs.com/zhuangjie/p/15900986.html  K8S master节点初始化 #初始化化失败, #重新初始化 #国内 ]
 +
 +
[https://blog.csdn.net/qq_43566826/article/details/120918666  k8s-集群初始化kubeadm init踩的坑和经验]
 +
 +
[https://blog.csdn.net/qq_38496902/article/details/106560163  【kubeadm】init配置文件]
 
[[category:k8s]] [[category:devops]]
 
[[category:k8s]] [[category:devops]]

2022年8月12日 (五) 08:46的最新版本

pre

 ansible的那个也是可以的 拿来改一下 

这脚本是在 centos7.x上跑的 我再加个 debian 11的

kuboard提供的脚本

https://kuboard.cn/install/history-k8s/install-k8s-1.21.x.html

pre

有空改一下为 debian11的

从这里开始 Auguest first monday

 先做一个  centos7.8的模板
qm clone  116  118 --name k8s-node1-centos7.8
qm clone  116  119 --name k8s-node2-centos7.8

apiserver.linuxsa.org 得hosts master ip 特别是节点上

修改 hostname

# 修改 hostname
hostnamectl set-hostname your-new-host-name
# 查看修改结果
hostnamectl status
# 设置 hostname 解析
echo "127.0.0.1   $(hostname)" >> /etc/hosts

install containerd kubelet kubeadm kubectl

# 在 master 节点和 worker 节点都要执行
# 最后一个参数 1.21.6 用于指定 kubenetes 版本,支持所有 1.21.x 版本的安装
# 腾讯云 docker hub 镜像
# export REGISTRY_MIRROR="https://mirror.ccs.tencentyun.com"
# DaoCloud 镜像
# export REGISTRY_MIRROR="http://f1361db2.m.daocloud.io"
# 华为云镜像
# export REGISTRY_MIRROR="https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com"
# 阿里云 docker hub 镜像
export REGISTRY_MIRROR=https://registry.cn-hangzhou.aliyuncs.com
curl -sSL https://kuboard.cn/install-script/v1.21.x/install_kubelet.sh | sh -s 1.21.6

v1.21.x/install_kubelet.sh

#!/bin/bash

# 在 master 节点和 worker 节点都要执行

# 安装 containerd
# 参考文档如下
# https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd

cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# Setup required sysctl params, these persist across reboots.
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

# Apply sysctl params without reboot
sysctl --system

# 卸载旧版本
yum remove -y containerd.io

# 设置 yum repository
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 安装 containerd
yum install -y containerd.io-1.4.3

mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml

sed -i "s#k8s.gcr.io#registry.aliyuncs.com/k8sxio#g"  /etc/containerd/config.toml
sed -i '/containerd.runtimes.runc.options/a\ \ \ \ \ \ \ \ \ \ \ \ SystemdCgroup = true' /etc/containerd/config.toml
sed -i "s#https://registry-1.docker.io#${REGISTRY_MIRROR}#g"  /etc/containerd/config.toml


systemctl daemon-reload
systemctl enable containerd
systemctl restart containerd


# 安装 nfs-utils
# 必须先安装 nfs-utils 才能挂载 nfs 网络存储
yum install -y nfs-utils
yum install -y wget

# 关闭 防火墙
systemctl stop firewalld
systemctl disable firewalld

# 关闭 SeLinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

# 关闭 swap
swapoff -a
yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak |grep -v swap > /etc/fstab

# 配置K8S的yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
       http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# 卸载旧版本
yum remove -y kubelet kubeadm kubectl

# 安装kubelet、kubeadm、kubectl
# 将 ${1} 替换为 kubernetes 版本号,例如 1.21.6
yum install -y kubelet-${1} kubeadm-${1} kubectl-${1}

crictl config runtime-endpoint /run/containerd/containerd.sock

# 重启 docker,并启动 kubelet
systemctl daemon-reload
systemctl enable kubelet && systemctl start kubelet

containerd --version
kubelet --version

初始化master

初始化 master 节点
# 只在 master 节点执行
# 替换 x.x.x.x 为 master 节点的内网IP
# export 命令只在当前 shell 会话中有效,开启新的 shell 窗口后,如果要继续安装过程,请重新执行此处的 export 命令
export MASTER_IP=192.168.10.171
# 替换 apiserver.demo 为 您想要的 dnsName
export APISERVER_NAME=apiserver.linuxsa.org
# Kubernetes 容器组所在的网段,该网段安装完成后,由 kubernetes 创建,事先并不存在于您的物理网络中
export POD_SUBNET=10.100.0.0/16
echo "${MASTER_IP}    ${APISERVER_NAME}" >> /etc/hosts

#一定要加上 
 curl -sSL https://kuboard.cn/install-script/v1.21.x/init_master.sh | sh -s 1.21.6 /coredns

v1.21.x/init_master.sh

#!/bin/bash

# 只在 master 节点执行
# ${1} 1.21.6  ${2} /coredns
# 脚本出错时终止执行
set -e

if [ ${#POD_SUBNET} -eq 0 ] || [ ${#APISERVER_NAME} -eq 0 ]; then
  echo -e "\033[31;1m请确保您已经设置了环境变量 POD_SUBNET 和 APISERVER_NAME \033[0m"
  echo 当前POD_SUBNET=$POD_SUBNET
  echo 当前APISERVER_NAME=$APISERVER_NAME
  exit 1
fi


# 查看完整配置选项 https://godoc.org/k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2
rm -f ./kubeadm-config.yaml
cat <<EOF > ./kubeadm-config.yaml
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: v${1}
imageRepository: registry.aliyuncs.com/k8sxio
controlPlaneEndpoint: "${APISERVER_NAME}:6443"
networking:
  serviceSubnet: "10.96.0.0/16"
  podSubnet: "${POD_SUBNET}"
  dnsDomain: "cluster.local"
dns:
  type: CoreDNS
  imageRepository: swr.cn-east-2.myhuaweicloud.com${2}
  imageTag: 1.8.0

---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd
EOF

# kubeadm init
# 根据您服务器网速的情况,您需要等候 3 - 10 分钟
echo ""
echo "抓取镜像,请稍候..."
kubeadm config images pull --config=kubeadm-config.yaml
echo ""
echo "初始化 Master 节点"
kubeadm init --config=kubeadm-config.yaml --upload-certs

# 配置 kubectl
rm -rf /root/.kube/
mkdir /root/.kube/
cp -i /etc/kubernetes/admin.conf /root/.kube/config

脚本

https://github.com/evan886/k8s-install

说明 一个放在master 另外的一个放在node 直接 执行  这个脚本是原作者的 在这里谢谢了

debian 11用这两个

 有交互界面的 记得yes 还有  hostname什么的得先手工 
 deb_install_k8s_master.sh  deb_install_k8s_node.sh


Debian 11快速部署K8S

在 debian 10 上以 kubeadm 方式安装 Kubernetes v1.20.5 ha集群

Centos7 用这两个

 centos_install_k8s_master.sh  centos_install_k8s_node.sh

Reference

debian

Debian11最小化部署k8s集群

Debian安装kubernetes1.23.1实战【详细步骤】

RHEL

troubleshooting

K8S master节点初始化 #初始化化失败, #重新初始化 #国内

k8s-集群初始化kubeadm init踩的坑和经验

【kubeadm】init配置文件