chiachan
chiachan
Published on 2025-01-08 / 888 Visits
0

debian12安装k8s环境(minikube)

安装docker

root@debian:~# apt install apt-transport-https ca-certificates curl software-properties-common
...

root@debian:~# curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.asc

root@debian:~#  chmod a+r /etc/apt/keyrings/docker.asc

root@debian:~# echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  tee /etc/apt/sources.list.d/docker.list > /dev/null
root@debian:~# apt-get update
...

root@debian:~# apt install docker-ce

root@debian:~# systemctl status docker
* docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; preset: enabled)
     Active: active (running) since Tue 2025-01-07 17:20:56 CST; 11s ago
TriggeredBy: * docker.socket
       Docs: https://docs.docker.com
   Main PID: 8086 (dockerd)
      Tasks: 8
     Memory: 23.8M
        CPU: 654ms
     CGroup: /system.slice/docker.service
             `-8086 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  • 执行 sudo usermod -aG docker $USER 给用户添加docker权限

给docker配置代理

  • 在/lib/systemd/system/docker.service(这里路径通过docker show docker可以查到)添加下面,
    然后执行systemctl daemon-reload && systemctl restart docker
Environment="HTTP_PROXY=http://192.168.88.200:20171"
Environment="HTTPS_PROXY=http://192.168.88.200:20171"
Environment="NO_PROXY=localhost,127.0.0.1,::1,192.168.0.0/16,10.0.0.0/8"

安装Minikube

root@debian:~# curl -LO https://github.com/kubernetes/minikube/releases/download/v1.32.0/minikube_1.32.0-0_amd64.deb
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 28.9M  100 28.9M    0     0  6070k      0  0:00:04  0:00:04 --:--:-- 12.4M

root@debian:~# dpkg -i minikube_1.32.0-0_amd64.deb

启动Minikube

minikube start --docker-env http_proxy=http://192.168.88.200:20171 --docker-env https_proxy=http://192.168.88.200:20171 --docker-env no_proxy=localhost,127.0.0.1,::1,192.168.0.0/16

如果docker已经配置好代理了,直接执行 minikube start就可以了

  • 注意:这里启动minikube不能使用root用户;如果提示没有权限,可能要重启下服务器才行

image-ikge.png

参考:https://minikube.sigs.k8s.io/docs/start/?arch=%2Flinux%2Fx86-64%2Fstable%2Fdebian+package

安装kubectl

  1. 更新 apt 包索引,并安装使用 Kubernetes apt 仓库所需要的包
root@debian:~# apt-get update
Hit:1 http://security.debian.org/debian-security bookworm-security InRelease
Hit:2 http://deb.debian.org/debian bookworm InRelease
Hit:3 http://deb.debian.org/debian bookworm-updates InRelease
Reading package lists... Done

root@debian:~# apt-get install -y apt-transport-https ca-certificates curl gnupg
...
  1. 下载 Kubernetes 软件包仓库的公共签名密钥。 同一个签名密钥适用于所有仓库,因此你可以忽略 URL 中的版本信息
  • 注意检查 /etc/apt/keyrings是否存在,不存在则执行 mkdir -p /etc/apt/keyrings创建;我这里版本默认是有的
root@debian:~# curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.32/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

root@debian:~# chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg
  1. 添加合适的 Kubernetes apt 仓库
  • k8s版本我也不清楚,就默认用1.32了
root@debian:~# echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.32/deb/ /' | tee /etc/apt/sources.list.d/kubernetes.list

root@debian:~# chmod 644 /etc/apt/sources.list.d/kubernetes.list
  1. 更新 apt 包索引,然后安装 kubectl
root@debian:~# apt-get update
Hit:1 http://security.debian.org/debian-security bookworm-security InRelease
Hit:2 http://deb.debian.org/debian bookworm InRelease
Hit:4 http://deb.debian.org/debian bookworm-updates InRelease
Get:3 https://prod-cdn.packages.k8s.io/repositories/isv:/kubernetes:/core:/stable:/v1.32/deb  InRelease [1186 B]
Get:5 https://prod-cdn.packages.k8s.io/repositories/isv:/kubernetes:/core:/stable:/v1.32/deb  Packages [2731 B]
Fetched 3917 B in 2s (2258 B/s)
Reading package lists... Done

root@debian:~# apt-get install -y kubectl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  kubectl
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 11.3 MB of archives.
After this operation, 57.4 MB of additional disk space will be used.
Get:1 https://prod-cdn.packages.k8s.io/repositories/isv:/kubernetes:/core:/stable:/v1.32/deb  kubectl 1.32.0-1.1 [11.3 MB]
Fetched 11.3 MB in 2s (5394 kB/s)
Selecting previously unselected package kubectl.
(Reading database ... 35045 files and directories currently installed.)
Preparing to unpack .../kubectl_1.32.0-1.1_amd64.deb ...
Unpacking kubectl (1.32.0-1.1) ...
Setting up kubectl (1.32.0-1.1) ...
  1. 这时已经按照完成了
root@debian:~# kubectl version --client
Client Version: v1.32.0
Kustomize Version: v5.5.0

验证kubectl配置

  1. 前面minikube没有成功,即k8s环境不存在时表现如下
root@debian:~# kubectl version --client
Client Version: v1.32.0
Kustomize Version: v5.5.0
root@debian:~# kubectl cluster-info
E0107 15:14:30.847421    1760 memcache.go:265] "Unhandled Error" err="couldn't get current server API group list: Get \"http://localhost:8080/api?timeout=32s\": dial tcp [::1]:8080: connect: connection refused"
E0107 15:14:30.849557    1760 memcache.go:265] "Unhandled Error" err="couldn't get current server API group list: Get \"http://localhost:8080/api?timeout=32s\": dial tcp [::1]:8080: connect: connection refused"
E0107 15:14:30.851304    1760 memcache.go:265] "Unhandled Error" err="couldn't get current server API group list: Get \"http://localhost:8080/api?timeout=32s\": dial tcp [::1]:8080: connect: connection refused"
E0107 15:14:30.853300    1760 memcache.go:265] "Unhandled Error" err="couldn't get current server API group list: Get \"http://localhost:8080/api?timeout=32s\": dial tcp [::1]:8080: connect: connection refused"
E0107 15:14:30.855305    1760 memcache.go:265] "Unhandled Error" err="couldn't get current server API group list: Get \"http://localhost:8080/api?timeout=32s\": dial tcp [::1]:8080: connect: connection refused"

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
The connection to the server localhost:8080 was refused - did you specify the right host or port?
  • 这里的报错,代表kubectl配置出现问题,无法连接到kubernetes。 不着慌,是因为我们kubernetes还没有搭起来。我这里是因为我还没有安装minikube
  1. 成功效果如下
chiachan@debian:~$ kubectl cluster-info
Kubernetes control plane is running at https://192.168.49.2:8443
CoreDNS is running at https://192.168.49.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

参考:https://kubernetes.io/zh-cn/docs/tasks/tools/install-kubectl-linux/

自动补全功能

minikube completion bash | sudo tee /etc/bash_completion.d/minikube > /dev/null
source /etc/bash_completion.d/minikube

kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
source /etc/bash_completion.d/kubectl

常用命令

备忘

kubectl:

  • kubectl get pod
  • kubectl get pods –all-namespaces
  • kubectl get service
  • kubectl describe po hello-minikube-180744149-lj0rd

minikube:

  • minikube dashboard
  • minikube status
  • minikube service hello-minikube –url
  • curl $(minikube service hello-minikube –url)

参考文献