统一ubuntu机器源和kubernetes源
1、将原先的apt源备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
2、修改apt 阿里源
sudo vim /etc/apt/sources.list
添加
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
3、执行apt-upate
sudo apt-get update
4、添加kubernetes源
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 <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
安装runc
wget https://github.com/opencontainers/runc/releases/download/v1.0.2/runc.amd64 -O /usr/bin/runc
脚本测试情况如下:
安装containerd
自带ctr查看一下
安装crictl
VERSION="v1.22.0"
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz
sudo tar zxvf crictl-$VERSION-linux-amd64.tar.gz -C /usr/local/bin
配置containerd代理
cat > /etc/systemd/system/containerd.service.d/http_proxy.conf << EOF
[Service]
Environment="HTTP_PROXY=http://172.17.9.143:7890/"
Environment="HTTPS_PROXY=https://172.17.9.143:7890/"
EOF
systemctl daemon-reload
systemctl restart containerd
验证
运行 Pod 沙盒
crictl 运行 Pod 沙盒对容器运行时排错很有帮助。 在运行的 Kubernetes 集群中,沙盒会随机地被 kubelet 停止和删除。
编写下面的 JSON 文件,命名为pod-config.json:
{
"metadata": {
"name": "nginx-sandbox",
"namespace": "default",
"attempt": 1,
"uid": "hdishd83djaidwnduwk28bcsb"
},
"log_directory": "/tmp",
"linux": {
}
}
使用 crictl runp 命令应用 JSON 文件并运行沙盒。
root@worker-cluster2-1:/home/libin/containerd# crictl runp pod-config.json
ea67a6ae69dbe11e5fc24dde7d7fbfbbeb48ca3909b16566d75b12d85ce050c8
创建容器
编写下面的 JSON 文件,命名为containerd-config.json:
{
"metadata": {
"name": "busybox"
},
"image":{
"image": "busybox"
},
"command": [
"top"
],
"log_path":"busybox.0.log",
"linux": {
}
}
#创建容器,传递先前创建的 Pod 的 ID、容器配置文件和 Pod 配置文件。返回容器的 ID。
root@worker-cluster2-1:/home/libin/containerd# crictl create ea67a6ae69dbe11e5fc24dde7d7fbfbbeb48ca3909b16566d75b12d85ce050c8 container-config.json pod-config.json
5421569f12d6b2aac9fcf1fea37e617756d4e11ef70e873139306641dc7cbb10
#查询所有容器并确认新创建的容器状态为 Created。
root@worker-cluster2-1:/home/libin/containerd# crictl ps -a
CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID
5421569f12d6b busybox 8 seconds ago Created busybox 0 ea67a6ae69dbe
启动容器
#要启动容器,要将容器 ID 传给 crictl start:
root@worker-cluster2-1:/home/libin/containerd# crictl start 5421569f12d6b2aac9fcf1fea37e617756d4e11ef70e873139306641dc7cbb10
5421569f12d6b2aac9fcf1fea37e617756d4e11ef70e873139306641dc7cbb10
#确认容器的状态为 Running。
root@worker-cluster2-1:/home/libin/containerd# crictl ps
CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID
5421569f12d6b busybox 11 minutes ago Running busybox 0 ea67a6ae69dbe
安装kubelet
apt-get install -y kubelet=1.22.4-00
因为本次测试采用的是containerd,并未使用docker,需要对kubelet进行配置
1、编写kubelet.service配置文件
从已有的k8s集群里copy一份kubelet.service;放置到/lib/systemd/system/
[Unit]
Description=Kubernetes Kubelet Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=containerd.service
Wants=containerd.service
[Service]
User=root
EnvironmentFile=-/etc/kubernetes/kubelet.env
ExecStart=/usr/bin/kubelet \
$KUBE_LOGTOSTDERR \
$KUBE_LOG_LEVEL \
$KUBELET_API_SERVER \
$KUBELET_ADDRESS \
$KUBELET_PORT \
$KUBELET_HOSTNAME \
$KUBELET_ARGS \
$DOCKER_SOCKET \
$KUBELET_NETWORK_PLUGIN \
$KUBELET_VOLUME_PLUGIN \
$KUBELET_CLOUDPROVIDER
Restart=always
RestartSec=10s
[Install]
WantedBy=multi-user.target
2、编写kubelet.env配置文件
copy一份kubelet.env,放置到/etc/kubernetes/
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=2"
KUBELET_ADDRESS="--node-ip=192.168.2.194"
KUBELET_HOSTNAME="--hostname-override=test-ubuntu"
KUBELET_ARGS="--cgroup-driver=systemd \
--pod-manifest-path=/etc/kubernetes/manifests/ \
--container-runtime=remote \
--container-runtime-endpoint=unix:///var/run/containerd/containerd.sock \
"
#KUBELET_NETWORK_PLUGIN="--network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin"
KUBELET_CLOUDPROVIDER=""
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
此处验证单机vm,保留基本的配置,其他的删除
3、重启
systemctl stop kubelet
systemctl daemon-reload
systemctl start kubelet
systemctl status kubelet
查看日志
journalctl -xefu kubelet