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/
相关推荐
冬天vs不冷14 分钟前
Linux用户与权限管理详解
linux·运维·chrome
凯子坚持 c1 小时前
深入Linux权限体系:守护系统安全的第一道防线
linux·运维·系统安全
✿ ༺ ོIT技术༻1 小时前
C++11:新特性&右值引用&移动语义
linux·数据结构·c++
watermelonoops4 小时前
Deepin和Windows传文件(Xftp,WinSCP)
linux·ssh·deepin·winscp·xftp
疯狂飙车的蜗牛5 小时前
从零玩转CanMV-K230(4)-小核Linux驱动开发参考
linux·运维·驱动开发
远游客07137 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
马甲是掉不了一点的<.<7 小时前
本地电脑使用命令行上传文件至远程服务器
linux·scp·cmd·远程文件上传
jingyu飞鸟7 小时前
centos-stream9系统安装docker
linux·docker·centos
超爱吃士力架8 小时前
邀请逻辑
java·linux·后端
White_Mountain9 小时前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu