ubuntu-18.04.6部署kubelet的步骤及常用命令
- 版本信息
- 操作步骤
- 替换apt源
- 安装containerd 1.7.7
- 安装runc 1.1.10
- 安装cni 1.1.1
- 安装kubelet 1.24.10
- 系统配置(同步时钟,禁止swap等)
- 重置k8s配置
- 初化containerd配置文件
- 创建kubelet配置文件,采用containerd
- 重启containerd和kubelet
- 提前下载镜像(非必须)
- 创建集群
- containerd 常用命令
- 创建Pod
本文介绍了如何在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源
sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.listsed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.listapt update
安装containerd 1.7.7
wget https://github.com/containerd/containerd/releases/download/v1.7.7/containerd-1.7.7-linux-amd64.tar.gztar -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.servicesystemctl daemon-reloadsystemctl enable containerdsystemctl start containerd
安装runc 1.1.10
wget https://github.com/opencontainers/runc/releases/download/v1.1.10/runc.amd64install -m 755 runc.amd64 /usr/local/sbin/runc
安装cni 1.1.1
wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgzmkdir -p /opt/cni/bintar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.1.1.tgz
安装kubelet 1.24.10
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<<EOFdeb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial mainEOFapt update -yapt-cache madison kubeadm apt install -y kubelet=1.24.10-00apt install -y kubectl=1.24.10-00apt install -y kubeadm=1.24.10-00systemctl enable kubelet
系统配置(同步时钟,禁止swap等)
apt install -y chronysystemctl enable --now chronychronyc sourcesswapoff -a && sed -i '/swap/d' /etc/fstabcat <<EOF | sudo tee /etc/sysctl.d/k8s.confnet.ipv4.ip_forward = 1vm.swappiness = 0net.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1EOFsysctl -p /etc/sysctl.d/k8s.confcat <<EOF | sudo tee /etc/modules-load.d/k8s.confoverlaybr_netfilterip_vsip_vs_rrip_vs_wrrip_vs_shnf_conntrackEOFfor i in overlay br_netfilter ip_vs ip_vs_rr ip_vs_wrr ip_vs_sh nf_conntrack;do modprobe $i;done
重置k8s配置
echo y | kubeadm resetrm -rf /var/lib/cni/rm -rf /var/lib/kubelet/*rm -rf /etc/cni/ip link set cni0 downip link set flannel.1 downip link set docker0 downip link delete cni0ip link delete flannel.1rm -rf /var/lib/kubelet/rm -rf /var/lib/dockershim/rm -rf /var/run/kubernetesrm -rf /var/lib/cnirm -rf /etc/kubernetes/*rm -rf ~/.kube/*rm -rf /var/lib/etcd/*lsof -i :6443|grep -v "PID"|awk '{print "kill -9",$2}'|shlsof -i :10257|grep -v "PID"|awk '{print "kill -9",$2}'|shlsof -i :10259|grep -v "PID"|awk '{print "kill -9",$2}'|shlsof -i :2379|grep -v "PID"|awk '{print "kill -9",$2}'|shlsof -i :2380|grep -v "PID"|awk '{print "kill -9",$2}'|shsystemctl stop kubelet
初化containerd配置文件
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
创建kubelet配置文件,采用containerd
mkdir -p /etc/sysconfigcat > /etc/sysconfig/kubelet << EOFKUBELET_EXTRA_ARGS=--container-runtime=remote --container-runtime-endpoint=unix:///run/containerd/containerd.sock --cgroup-driver=systemdEOF
重启containerd和kubelet
rm /etc/docker/daemon.jsonsystemctl daemon-reloadsystemctl restart containerd systemctl status containerd systemctl restart kubelet systemctl status kubelet
提前下载镜像(非必须)
ctr -n k8s.io i pull registry.aliyuncs.com/google_containers/pause:3.6ctr -n k8s.io i tag registry.aliyuncs.com/google_containers/pause:3.6 registry.k8s.io/pause:3.8ctr -n k8s.io i pull docker.io/flannel/flannel:v0.24.2
创建集群
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.24.10 \ --pod-network-cidr=10.244.0.0/16mkdir -p $HOME/.kubesudo cp -f /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configkubectl 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 常用命令
# 删除镜像ctr -n=k8s.io imagesls | 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_democtr -n=k8s.io task kill -a -s 9 pod_demo# 删除容器ctr -n=k8s.io container del pod_demo
创建Pod
# 创建命名空间kubectl create ns hello-world# 删除Podkubectl delete -f sample_pod.yamlkubectl get pods --all-namespaces# 创建podkubectl apply -f sample_pod.yamlkubectl get pods --all-namespaces# 进入podkubectl exec -ti `kubectl get pods --all-namespaces | grep "sample-pod" | awk '{print $2}'` -n hello-world /bin/bash
sample_pod.yaml:
apiVersion: apps/v1kind: Deploymentmetadata:name: sample-podnamespace: hello-worldlabels:app: sample-podspec:replicas: 1selector:matchLabels:app: sample-podtemplate:metadata:labels:app: sample-podspec:containers:- name: sample-podimage: docker.io/library/ubuntu:22.04imagePullPolicy: IfNotPresenttty: truestdin: truecommand: [ "/bin/bash", "-c", "--" ]args: [ "while true; do sleep 30; done;" ]volumeMounts:- name: sharedmountPath: /home/volumes:- name: sharedhostPath:path: /home/