学习运维之单机部署Docker+k3s+k9s

155 阅读2分钟

环境

  • Ubuntu 24.04 (LTS)
  • Docker Engine - Community 28.1.1
  • k3s version v1.32.4+k3s1
  • K9s version v0.50.6

参考资料

部署环节

Dokcer

在线部署Docker

服务器能访问公网可以直接使用阿里云镜像加速执行以下命令

# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

# step 2: 信任 Docker 的 GPG 公钥
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Step 3: 写入软件源信息
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
 
# Step 4: 安装Docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Step 5: 查看Docker是否安装成功
sudo docker info #输出安装的详细信息

# Step 6: 设置镜像代理加速
sudo nano /etc/docker/daemon.json
{
  "data-root": "/mnt/docker-data", #修改docker数据存储位置 默认在 /var/lib/docker
  "registry-mirrors":["https://docker.1ms.run"] #国内镜像加速地址,还挺稳定的
}
保存后重启docker
sudo systemctl restart docker
sudo docker info #再次查看配置是否更新

image.png

离线安装Docker

  1. 包下载地址https://download.docker.com/linux/ubuntu/dists/.

  2. 在列表中选择您的 Ubuntu 版本。

  3. 转到pool/stable/ 并选择适用的体系结构 amd64 armhf arm64 s390x

  4. 下载以下文件,用于 Docker 引擎、CLI、containerd、 和 Docker Compose 包:deb

    containerd.io_<version>_<arch>.deb
    docker-ce_<version>_<arch>.deb
    docker-ce-cli_<version>_<arch>.deb
    docker-buildx-plugin_<version>_<arch>.deb
    docker-compose-plugin_<version>_<arch>.deb

  5. 安装包。将以下示例中的路径更新为 下载 Docker .deb包的位置。

    sudo dpkg -i ./containerd.io_<version>_<arch>.deb \
      ./docker-ce_<version>_<arch>.deb \
      ./docker-ce-cli_<version>_<arch>.deb \
      ./docker-buildx-plugin_<version>_<arch>.deb \
      ./docker-compose-plugin_<version>_<arch>.deb
    
  6. 设置 Docker 守护程序自动启动

    sudo systemctl enable docker
    sudo systemctl start docker  # 如果尚未运行
    

k3s 单机版

K3s 是轻量级的 Kubernetes。K3s 易于安装,仅需要 Kubernetes 内存的一半,所有组件都在一个小于 100 MB 的二进制文件中。
k3s使用脚本安装的时候会默认安装containerd,使用docker 容器需要执行以下命令

 curl -sfL <https://rancher-mirror.rancher.cn/k3s/k3s-install.sh> | INSTALL\_K3S\_MIRROR=cn sh -s - --docker
 #验证是否安装成功
 k3s -v
 #输出
 >> k3s version v1.32.4+k3s1 (6b330558)
 >> go version go1.23.6

 sudo kubectl version #k8s的版本信息
 >> Client Version: v1.32.4+k3s1
 >> Kustomize Version: v5.5.0
 >> Server Version: v1.32.4+k3s1

k9s 安装

K9s 提供了一个终端 UI 来与Kubernetes 集群进行交互。方便管理K8s.很轻量。

  #在release里面下载对应系统的安装包
  wget https://github.com/derailed/k9s/releases/latest/download/k9s_linux_amd64.deb
  apt install ./k9s_linux_amd64.deb
  
  #验证安装
  sudo k9s info
     >>  ____  __.________
     >> |    |/ _/   __   \______
     >> |      < \____    /  ___/
     >> |    |  \   /    /\___ \
     >> |____|__ \ /____//____  >
     >>          \/            \/
     >> 
     >> Version:           vX.Y.Z
     >> Config:            /Users/fernand/.config/k9s/config.yaml
     >> Logs:              /Users/fernand/.local/state/k9s/k9s.log
     >> Dumps dir:         /Users/fernand/.local/state/k9s/screen-dumps
     >> Benchmarks dir:    /Users/fernand/.local/state/k9s/benchmarks
     >> Skins dir:         /Users/fernand/.local/share/k9s/skins
     >> Contexts dir:      /Users/fernand/.local/share/k9s/clusters
     >> Custom views file: /Users/fernand/.local/share/k9s/views.yaml
     >> Plugins file:      /Users/fernand/.local/share/k9s/plugins.yaml
     >> Hotkeys file:      /Users/fernand/.local/share/k9s/hotkeys.yaml
     >> Alias file:        /Users/fernand/.local/share/k9s/aliases.yaml
  
  #安装成功后复制k8s 配置文件
  sudo cp /etc/rancher/k3s/k3s.yaml $HOME/.kube/config
  
  sudo k9s

image.png

K9S 的常用功能列表

ActionCommandComment
Show active keyboard mnemonics and help?
Show all available resource aliasctrl-a
To bail out of K9s:quit, , :q``ctrl-c
To go up/back to the previous viewescIf you have crumbs on, this will go to the previous one
View a Kubernetes resource using singular/plural or short-name:pod⏎accepts singular, plural, short-name or alias ie pod or pods
View a Kubernetes resource in a given namespace:pod ns-x⏎
View filtered pods (New v0.30.0!):pod /fred⏎View all pods filtered by fred
View labeled pods (New v0.30.0!):pod app=fred,env=dev⏎View all pods with labels matching app=fred and env=dev
View pods in a given context (New v0.30.0!):pod @ctx1⏎View all pods in context ctx1. Switches out your current k9s context!
Filter out a resource view given a filter/filter⏎Regex2 supported ie `fred
Inverse regex filter/! filter⏎Keep everything that doesn't match.
Filter resource view by labels/-l label-selector⏎
Fuzzy find a resource given a filter/-f filter⏎
Bails out of view/command/filter mode<esc>
Key mapping to describe, view, edit, view logs,...d,, , ,...v``e``l
To view and switch to another Kubernetes context (Pod view):ctx⏎
To view and switch directly to another Kubernetes context (Last used view):ctx context-name⏎
To view and switch to another Kubernetes namespace:ns⏎
To switch back to the last active command (like how "cd -" works)-Navigation that adds breadcrumbs to the bottom are not commands
To go back and forward through the command historyback: , forward: [``]Same as above
To view all saved resources:screendump or sd⏎
To delete a resource (TAB and ENTER to confirm)ctrl-d
To kill a resource (no confirmation dialog, equivalent to kubectl delete --now)ctrl-k
Launch pulses view:pulses or pu⏎
Launch XRay view:xray RESOURCE [NAMESPACE]⏎RESOURCE can be one of po, svc, dp, rs, sts, ds, NAMESPACE is optional
Launch Popeye view:popeye or pop⏎See popeye

搭建完成

后续搭建jenkins+gerrit CI/CD集成