Loading

ubuntu20.04安装Kubernetes(k8s 1.27.4)


官方文档:https://kubernetes.io/zh-cn/docs/home/

参考:https://www.cnblogs.com/wwph/p/14203626.html
https://blog.csdn.net/wanghui2087/article/details/129329357
kubeadm部署Kubernetes 网络插件:https://blog.csdn.net/cojn52/article/details/109449828

1. 禁止swap分区(必须)

暂时禁止:

sudo swapoff -a

永久禁止:

 # 注释掉swap一行
sudo vi /etc/fstab

2. 设置主机名

最下方添加:

sudo vim /etc/hosts
192.168.2.5 master

3. 更改net.bridge.bridge-nf-call-iptables的值为1.(Ubuntu 20.04默认为1)

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

4. 安装Docker

sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker

开始安装K8S

1.安装kubeadm kubeadm kubectl

sudo apt-get update && sudo apt-get install -y ca-certificates curl software-properties-common apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF 
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

2.初始化

修改containerd的配置,因为containerd默认从k8s官网拉取镜像

mkdir -p /etc/containerd

生成默认配置文件

containerd config default | sudo tee /etc/containerd/config.toml
vim /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri"]
..................................
# 搜索sandbox_image,把原来的k8s.gcr.io/pause:3.6改为"registry.aliyuncs.com/google_containers/pause:3.9" 
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"	 
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
..........................
SystemdCgroup = true					#搜索SystemdCgroup,把这个false改为true		

# 搜索config_path,配置镜像加速地址(这是一个目录下面创建)
[plugins."io.containerd.grpc.v1.cri".registry]
     config_path = "/etc/containerd/certs.d"			

# 创建镜像加速的目录 
mkdir /etc/containerd/certs.d/docker.io -pv
# 配置加速
cat > /etc/containerd/certs.d/docker.io/hosts.toml << EOF
server = "https://docker.io"
[host."https://b9pmyelo.mirror.aliyuncs.com"]
  capabilities = ["pull", "resolve"]
EOF

# 加载containerd的内核模块
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter

# 重启containerd
systemctl restart containerd 
systemctl status containerd

初始化我们使用配置文件的方式进行部署

创建默认的kubeadm-config.yaml文件

sudo kubeadm config print init-defaults  > kubeadm-config.yaml

我的配置文件:

注意:就修改标注的这3处就可以了,其他还使用默认生成的配置,因为版本不一样。

apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 192.168.1.73 # 修改成本地内网ip
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock 
  imagePullPolicy: IfNotPresent
  name: master # 修改成自定义的名称
  taints: null
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers # 使用阿里源
kind: ClusterConfiguration
kubernetesVersion: 1.22.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.244.0.0/16  # 修改成10.244.0.0/16 
scheduler: {}

安装master节点

# 日志级别为 5 可能会产生大量的输出 可以方便我们查看报错信息
sudo kubeadm init --config kubeadm-config.yaml --v=5

如果kubeadm init初始化失败可以使用重置命令。

# 重置
sudo kubeadm reset

3.看是否安装成功

日志出现一下代码表示安装成功、然后根据步骤进行操作就OK了:

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/config

4.排查问题

排查这类问题的方法是执行如下命令查看具体错误原因:

journalctl -fxeu kubelet

查看端口是否被占用

sudo netstat -tunlp | grep 10250

相关命令:

# 验证k8s是否安装成功
kubectl get pods -n kube-system 

sudo systemctl start kubelet
sudo systemctl start docker

与部署相关的常用命令:

#  创建部署
kubectl create -f k8s.yml
# 更新部署配置
kubectl apply -f k8s.yml
# 查看已部署pod
kubectl get service
kubectl get deployment 

# 查看service下的所有pod的详情信息
kubectl describe service
# 查看service下的mysql的详情信息
kubectl describe service mysql

# 查看pod输出日志
kubectl logs [-f] pod名称

# 使用以下命令进入MySQL Pod的Shell
kubectl exec -it mysql -- /bin/bash

# 删除pod
kubectl delete deployment mysql

删除所有名称带有registry的镜像

docker rmi $(docker images |  grep "registry*"  | awk '{print $3}')

删除命名以k8s开头的容器

docker rm -f $(docker ps -a |  grep "k8s*"  | awk '{print $1}')

使用阿里源拉取k8s所需的镜像并修改名称脚本

#!/bin/bash

images=("kube-apiserver:v1.27.4" "kube-controller-manager:v1.27.4" "kube-scheduler:v1.27.4" "kube-proxy:v1.27.4" "pause:3.9" "etcd:3.5.7-0" "coredns:v1.10.1")

for image in "${images[@]}"
do
    # echo "Pulling image registry.k8s.io/$image"
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$image

    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$image registry.k8s.io/$image

    docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$image
done


posted @ 2023-07-19 16:39  就学45分钟  阅读(1949)  评论(0编辑  收藏  举报