ubuntu-18.04.6部署kubelet的步骤及常用命令
- 版本信息
- 操作步骤
-
- 替换apt源
- [安装containerd 1.7.7](#安装containerd 1.7.7)
- [安装runc 1.1.10](#安装runc 1.1.10)
- [安装cni 1.1.1](#安装cni 1.1.1)
- [安装kubelet 1.24.10](#安装kubelet 1.24.10)
- 系统配置(同步时钟,禁止swap等)
- 重置k8s配置
- 初化containerd配置文件
- 创建kubelet配置文件,采用containerd
- 重启containerd和kubelet
- 提前下载镜像(非必须)
- 创建集群
- [containerd 常用命令](#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源
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/