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/
相关推荐
写代码的学渣5 分钟前
ubuntu 22.04 新装的系统 xshell 连不上
linux·运维·ubuntu
序属秋秋秋27 分钟前
《Linux系统编程之进程环境》【环境变量】
linux·运维·服务器·c语言·c++·操作系统·系统编程
云计算练习生1 小时前
linux shell编程实战 10 Git工具详解与运维场景实战
linux·运维·git
虚伪的空想家3 小时前
KVM的ubuntu虚机如何关闭安全启动
linux·安全·ubuntu
t198751289 小时前
在Ubuntu 22.04系统上安装libimobiledevice
linux·运维·ubuntu
skywalk81639 小时前
linux安装Code Server 以便Comate IDE和CodeBuddy等都可以远程连上来
linux·运维·服务器·vscode·comate
晚风吹人醒.10 小时前
缓存中间件Redis安装及功能演示、企业案例
linux·数据库·redis·ubuntu·缓存·中间件
Hard but lovely10 小时前
linux: pthread库的使用和理解
linux
这儿有一堆花12 小时前
Kali Linux:探测存活到挖掘漏洞
linux·运维·服务器
松涛和鸣12 小时前
从零开始理解 C 语言函数指针与回调机制
linux·c语言·开发语言·嵌入式硬件·排序算法