ubuntu-18.04.6部署kubelet的步骤及常用命令

ubuntu-18.04.6部署kubelet的步骤及常用命令

本文介绍了如何在ubuntu-18.04.6上部署kubelet 1.24.10,并且整理了常用的命令,供查阅

版本信息

属性
ubuntu-18.04.6 下载链接
containerd 1.7.7
runc 1.1.10
kubelet 1.24.10

操作步骤

替换apt源

bash 复制代码
sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
apt update

安装containerd 1.7.7

bash 复制代码
wget https://github.com/containerd/containerd/releases/download/v1.7.7/containerd-1.7.7-linux-amd64.tar.gz
tar -xvf containerd-1.7.7-linux-amd64.tar.gz -C /usr/local/
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service -O /etc/systemd/system/containerd.service
systemctl daemon-reload
systemctl enable containerd
systemctl start containerd

安装runc 1.1.10

bash 复制代码
wget https://github.com/opencontainers/runc/releases/download/v1.1.10/runc.amd64
install -m 755 runc.amd64 /usr/local/sbin/runc

安装cni 1.1.1

bash 复制代码
wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz
mkdir -p /opt/cni/bin
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.1.1.tgz

安装kubelet 1.24.10

bash 复制代码
apt-get update && apt-get install -y apt-transport-https && \
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - && \
cat > /etc/apt/sources.list.d/kubernetes.list<<EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

apt update -y
apt-cache madison kubeadm 
apt install -y kubelet=1.24.10-00
apt install -y kubectl=1.24.10-00
apt install -y kubeadm=1.24.10-00
systemctl enable kubelet

系统配置(同步时钟,禁止swap等)

bash 复制代码
apt install -y chrony
systemctl enable --now chrony
chronyc sources
swapoff -a && sed -i '/swap/d' /etc/fstab

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
vm.swappiness = 0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
EOF
for i in overlay br_netfilter ip_vs ip_vs_rr ip_vs_wrr ip_vs_sh nf_conntrack;do modprobe $i;done

重置k8s配置

bash 复制代码
echo y | kubeadm reset
rm -rf /var/lib/cni/
rm -rf /var/lib/kubelet/*
rm -rf /etc/cni/
ip link set cni0 down
ip link set flannel.1 down
ip link set docker0 down
ip link delete cni0
ip link delete flannel.1
rm -rf /var/lib/kubelet/
rm -rf /var/lib/dockershim/
rm -rf /var/run/kubernetes
rm -rf /var/lib/cni
rm -rf /etc/kubernetes/*
rm -rf ~/.kube/*
rm -rf /var/lib/etcd/*
lsof -i :6443|grep -v "PID"|awk '{print "kill -9",$2}'|sh
lsof -i :10257|grep -v "PID"|awk '{print "kill -9",$2}'|sh
lsof -i :10259|grep -v "PID"|awk '{print "kill -9",$2}'|sh
lsof -i :2379|grep -v "PID"|awk '{print "kill -9",$2}'|sh
lsof -i :2380|grep -v "PID"|awk '{print "kill -9",$2}'|sh
systemctl stop kubelet

初化containerd配置文件

bash 复制代码
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1

创建kubelet配置文件,采用containerd

bash 复制代码
mkdir -p /etc/sysconfig
cat > /etc/sysconfig/kubelet << EOF
KUBELET_EXTRA_ARGS=--container-runtime=remote --container-runtime-endpoint=unix:///run/containerd/containerd.sock --cgroup-driver=systemd
EOF

重启containerd和kubelet

bash 复制代码
rm /etc/docker/daemon.json
systemctl daemon-reload
systemctl restart containerd 
systemctl status containerd 
systemctl restart kubelet 
systemctl status kubelet

提前下载镜像(非必须)

bash 复制代码
ctr -n k8s.io i pull registry.aliyuncs.com/google_containers/pause:3.6
ctr -n k8s.io i tag registry.aliyuncs.com/google_containers/pause:3.6 registry.k8s.io/pause:3.8
ctr -n k8s.io i pull docker.io/flannel/flannel:v0.24.2

创建集群

bash 复制代码
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.24.10 \
             --pod-network-cidr=10.244.0.0/16

mkdir -p $HOME/.kube
sudo cp -f /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel.yml  #安装网络

# 设置master节点可调度
kubectl taint nodes --all node-role.kubernetes.io/master-
kubectl taint nodes --all node-role.kubernetes.io/control-plane-

# 确保所有的pod都为running状态
kubectl get pods --all-namespaces

containerd 常用命令

bash 复制代码
# 删除镜像
ctr -n=k8s.io images  ls | grep "ubuntu:22.04"
ctr -n=k8s.io images rm ubuntu:22.04

# 导入镜像
ctr -n=k8s.io images import ubuntu_22.04.tar

# 拉取镜像
ctr -n=k8s.io images pull docker.io/library/ubuntu:22.04

# 创建容器
ctr -n=k8s.io run --privileged --net-host --mount type=bind,src=/home,dst=/home,options=rbind -t docker.io/library/ubuntu:22.04 pod_demo bash

# 查询容器
ctr -n=k8s.io container ls | grep "pod_demo"

# 启动容器
ctr -n=k8s.io task start -d pod_demo

# 进入容器
ctr -n=k8s.io task exec --exec-id 0 -t pod_demo bash

# 杀死容器
ctr -n=k8s.io container ls | grep pod_demo
ctr -n=k8s.io task kill -a -s 9 pod_demo

# 删除容器
ctr -n=k8s.io container del pod_demo

创建Pod

bash 复制代码
# 创建命名空间
kubectl create ns hello-world

# 删除Pod
kubectl delete -f sample_pod.yaml
kubectl get pods --all-namespaces

# 创建pod
kubectl apply -f sample_pod.yaml
kubectl get pods --all-namespaces

# 进入pod
kubectl exec -ti `kubectl get pods --all-namespaces | grep "sample-pod" | awk '{print $2}'` -n hello-world /bin/bash

sample_pod.yaml:

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-pod
  namespace: hello-world
  labels:
    app: sample-pod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sample-pod
  template:
    metadata:
      labels:
        app: sample-pod
    spec:
      containers:
        - name: sample-pod
          image: docker.io/library/ubuntu:22.04
          imagePullPolicy: IfNotPresent
          tty: true
          stdin: true
          command: [ "/bin/bash", "-c", "--" ]
          args: [ "while true; do sleep 30; done;" ]
          volumeMounts:
            - name: shared
              mountPath: /home/
      volumes:
        - name: shared
          hostPath:
            path: /home/
相关推荐
Peter_chq17 分钟前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
一坨阿亮1 小时前
Linux 使用中的问题
linux·运维
dsywws2 小时前
Linux学习笔记之vim入门
linux·笔记·学习
幺零九零零3 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
小林熬夜学编程4 小时前
【Linux系统编程】第四十一弹---线程深度解析:从地址空间到多线程实践
linux·c语言·开发语言·c++·算法
程思扬5 小时前
为什么Uptime+Kuma本地部署与远程使用是网站监控新选择?
linux·服务器·网络·经验分享·后端·网络协议·1024程序员节
sun0077005 小时前
拷贝 cp -rdp 和 cp -a
linux·运维·服务器
wowocpp5 小时前
ubuntu 22.04 server 安装 anaconda3
linux·运维·ubuntu
乡村农夫5 小时前
cuda 环境搭建
linux
tingting01196 小时前
Linux 普通用户禁用sudo su - 命令
linux·运维·服务器