mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-10 11:54:37 +00:00
Release v1.5.0: Restructure chapters and update for Docker v30.x
This commit is contained in:
11
11_orchestration/setup/README.md
Normal file
11
11_orchestration/setup/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# 部署 Kubernetes
|
||||
|
||||
目前,Kubernetes 支持在多种环境下使用,包括本地主机(Ubuntu、Debian、CentOS、Fedora 等)、云服务([腾讯云](https://cloud.tencent.com/act/cps/redirect?redirect=10058&cps_key=3a5255852d5db99dcd5da4c72f05df61)、[阿里云](https://www.aliyun.com/product/kubernetes?source=5176.11533457&userCode=8lx5zmtu&type=copy)、[百度云](https://cloud.baidu.com/product/cce.html) 等)。
|
||||
|
||||
你可以使用以下几种方式部署 Kubernetes:
|
||||
|
||||
* kubeadm
|
||||
* docker-desktop
|
||||
* k3s
|
||||
|
||||
接下来的小节会对以上几种方式进行详细介绍。
|
||||
45
11_orchestration/setup/dashboard.md
Normal file
45
11_orchestration/setup/dashboard.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# Kubernetes Dashboard
|
||||
|
||||
[Kubernetes Dashboard](https://github.com/kubernetes/dashboard) 是基于网页的 Kubernetes 用户界面。
|
||||
|
||||

|
||||
|
||||
## 部署
|
||||
|
||||
执行以下命令即可部署 Dashboard:
|
||||
|
||||
```bash
|
||||
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml
|
||||
```
|
||||
|
||||
## 访问
|
||||
|
||||
通过命令行代理访问,执行以下命令:
|
||||
|
||||
```bash
|
||||
$ kubectl proxy
|
||||
```
|
||||
|
||||
到 http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ 即可访问。
|
||||
|
||||
## 登录
|
||||
|
||||
目前,Dashboard 仅支持使用 Bearer 令牌登录。下面教大家如何创建该令牌:
|
||||
|
||||
```bash
|
||||
$ kubectl create sa dashboard-admin -n kube-system
|
||||
|
||||
$ kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
|
||||
|
||||
$ ADMIN_SECRET=$(kubectl get secrets -n kube-system | grep dashboard-admin | awk '{print $1}')
|
||||
|
||||
$ DASHBOARD_LOGIN_TOKEN=$(kubectl describe secret -n kube-system ${ADMIN_SECRET} | grep -E '^token' | awk '{print $2}')
|
||||
|
||||
echo ${DASHBOARD_LOGIN_TOKEN}
|
||||
```
|
||||
|
||||
将结果粘贴到登录页面,即可登录。
|
||||
|
||||
## 参考文档
|
||||
|
||||
* [官方文档](https://kubernetes.io/zh/docs/tasks/access-application-cluster/web-ui-dashboard/)
|
||||
19
11_orchestration/setup/docker-desktop.md
Normal file
19
11_orchestration/setup/docker-desktop.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Docker Desktop 启用 Kubernetes
|
||||
|
||||
使用 Docker Desktop 可以很方便的启用 Kubernetes。
|
||||
|
||||
## 启用 Kubernetes
|
||||
|
||||
在 Docker Desktop 设置页面,点击 `Kubernetes`,选择 `Enable Kubernetes`,稍等片刻,看到左下方 `Kubernetes` 变为 `running`,Kubernetes 启动成功。
|
||||
|
||||

|
||||
|
||||
> 注意:Kubernetes 的镜像存储在 `registry.k8s.io`,如果国内网络无法直接访问,可以在 Docker Desktop 配置中的 `Docker Engine` 处配置镜像加速器,或者利用国内云服务商的镜像仓库手动拉取镜像并 retag。
|
||||
|
||||
## 测试
|
||||
|
||||
```bash
|
||||
$ kubectl version
|
||||
```
|
||||
|
||||
如果正常输出信息,则证明 Kubernetes 成功启动。
|
||||
52
11_orchestration/setup/k3s.md
Normal file
52
11_orchestration/setup/k3s.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# K3s - 轻量级 Kubernetes
|
||||
|
||||
[K3s](https://k3s.io/) 是一个轻量级的 Kubernetes 发行版,由 Rancher Labs 开发。它专为边缘计算、物联网、CI、ARM 等资源受限的环境设计。K3s 被打包为单个二进制文件,只有不到 100MB,但通过了 CNCF 的一致性测试。
|
||||
|
||||
## 核心特性
|
||||
|
||||
* **轻量级**:移除过时的、非必须的 Kubernetes 功能(如传统的云提供商插件),使用 SQLite 作为默认数据存储(也支持 Etcd/MySQL/Postgres)。
|
||||
* **单一二进制**:所有组件(API Server, Controller Manager, Scheduler, Kubelet, Kube-proxy)打包在一个进程中运行。
|
||||
* **开箱即用**:内置 Helm Controller、Traefik Ingress controller、ServiceLB、Local-Path-Provisioner。
|
||||
* **安全**:默认启用安全配置,基于 TLS 通信。
|
||||
|
||||
## 安装
|
||||
|
||||
### 脚本安装(Linux)
|
||||
|
||||
K3s 提供了极为便捷的安装脚本:
|
||||
|
||||
```bash
|
||||
curl -sfL https://get.k3s.io | sh -
|
||||
```
|
||||
|
||||
安装完成后,K3s 会自动启动并配置好 `systemd` 服务。
|
||||
|
||||
### 查看状态
|
||||
|
||||
```bash
|
||||
sudo k3s kubectl get nodes
|
||||
```
|
||||
|
||||
输出类似:
|
||||
```
|
||||
NAME STATUS ROLES AGE VERSION
|
||||
k3s-master Ready control-plane,master 1m v1.28.2+k3s1
|
||||
```
|
||||
|
||||
## 快速使用
|
||||
|
||||
K3s 内置了 `kubectl` 命令(通过 `k3s kubectl` 调用),为了方便,通常会建立别名或配置 `KUBECONFIG`。
|
||||
|
||||
```bash
|
||||
# 读取 K3s 的配置文件
|
||||
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
|
||||
|
||||
# 现在可以直接使用 kubectl
|
||||
kubectl get pods -A
|
||||
```
|
||||
|
||||
## 清理卸载
|
||||
|
||||
```bash
|
||||
/usr/local/bin/k3s-uninstall.sh
|
||||
```
|
||||
81
11_orchestration/setup/kind.md
Normal file
81
11_orchestration/setup/kind.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# Kind - Kubernetes IN Docker
|
||||
|
||||
[Kind](https://kind.sigs.k8s.io/) (Kubernetes in Docker) 是一个使用 Docker 容器作为节点运行本地 Kubernetes 集群的工具。主要用于测试 Kubernetes 本身,也非常适合本地开发和 CI 环境。
|
||||
|
||||
## 为什么选择 Kind
|
||||
|
||||
* **轻量便捷**:只要有 Docker 环境即可,无需额外虚拟机。
|
||||
* **多集群支持**:可以轻松在本地启动多个集群。
|
||||
* **多版本支持**:支持指定 Kubernetes 版本进行测试。
|
||||
* **HA 支持**:支持模拟高可用集群(多 Control Plane)。
|
||||
|
||||
## 安装 Kind
|
||||
|
||||
### macOS
|
||||
|
||||
```bash
|
||||
brew install kind
|
||||
```
|
||||
|
||||
### Linux / Windows (WSL2)
|
||||
|
||||
可以下载二进制文件:
|
||||
|
||||
```bash
|
||||
# Linux AMD64
|
||||
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
|
||||
chmod +x ./kind
|
||||
sudo mv ./kind /usr/local/bin/kind
|
||||
```
|
||||
|
||||
## 创建集群
|
||||
|
||||
最简单的创建方式:
|
||||
|
||||
```bash
|
||||
kind create cluster
|
||||
```
|
||||
|
||||
指定集群名称:
|
||||
|
||||
```bash
|
||||
kind create cluster --name my-cluster
|
||||
```
|
||||
|
||||
## 与集群交互
|
||||
|
||||
Kind 会自动将 kubeconfig 合并到 `~/.kube/config`。
|
||||
|
||||
```bash
|
||||
kubectl cluster-info --context kind-kind
|
||||
kubectl get nodes
|
||||
```
|
||||
|
||||
## 高级用法:配置集群
|
||||
|
||||
创建一个 `kind-config.yaml` 来定制集群,例如映射端口到宿主机:
|
||||
|
||||
```yaml
|
||||
kind: Cluster
|
||||
apiVersion: kind.x-k8s.io/v1alpha4
|
||||
nodes:
|
||||
- role: control-plane
|
||||
extraPortMappings:
|
||||
- containerPort: 80
|
||||
hostPort: 8080
|
||||
protocol: TCP
|
||||
- role: worker
|
||||
- role: worker
|
||||
```
|
||||
|
||||
应用配置:
|
||||
|
||||
```bash
|
||||
kind create cluster --config kind-config.yaml
|
||||
```
|
||||
|
||||
## 删除集群
|
||||
|
||||
```bash
|
||||
kind delete cluster
|
||||
```
|
||||
187
11_orchestration/setup/kubeadm-docker.md
Normal file
187
11_orchestration/setup/kubeadm-docker.md
Normal file
@@ -0,0 +1,187 @@
|
||||
# 使用 kubeadm 部署 kubernetes(使用 Docker)
|
||||
|
||||
`kubeadm` 提供了 `kubeadm init` 以及 `kubeadm join` 这两个命令作为快速创建 `kubernetes` 集群的最佳实践。
|
||||
|
||||
## 安装 Docker
|
||||
|
||||
参考 [安装 Docker](../../install) 一节安装 Docker。
|
||||
|
||||
## 安装 **kubelet** **kubeadm** **kubectl**
|
||||
|
||||
### Ubuntu/Debian
|
||||
|
||||
```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 <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
|
||||
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
|
||||
EOF
|
||||
|
||||
$ apt-get update
|
||||
$ apt-get install -y kubelet kubeadm kubectl
|
||||
```
|
||||
|
||||
### CentOS/Fedora
|
||||
|
||||
```bash
|
||||
$ cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
|
||||
[kubernetes]
|
||||
name=Kubernetes
|
||||
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
|
||||
enabled=1
|
||||
gpgcheck=1
|
||||
repo_gpgcheck=1
|
||||
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
|
||||
EOF
|
||||
|
||||
$ sudo yum install -y kubelet kubeadm kubectl
|
||||
```
|
||||
|
||||
## 修改内核的运行参数
|
||||
|
||||
```bash
|
||||
$ cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
|
||||
net.bridge.bridge-nf-call-iptables = 1
|
||||
net.ipv4.ip_forward = 1
|
||||
net.bridge.bridge-nf-call-ip6tables = 1
|
||||
EOF
|
||||
|
||||
# 应用配置
|
||||
$ sysctl --system
|
||||
```
|
||||
|
||||
## 配置 kubelet
|
||||
|
||||
### 修改 `kubelet.service`
|
||||
|
||||
`/etc/systemd/system/kubelet.service.d/10-proxy-ipvs.conf` 写入以下内容
|
||||
|
||||
```bash
|
||||
# 启用 ipvs 相关内核模块
|
||||
[Service]
|
||||
ExecStartPre=-/sbin/modprobe ip_vs
|
||||
ExecStartPre=-/sbin/modprobe ip_vs_rr
|
||||
ExecStartPre=-/sbin/modprobe ip_vs_wrr
|
||||
ExecStartPre=-/sbin/modprobe ip_vs_sh
|
||||
```
|
||||
|
||||
执行以下命令应用配置。
|
||||
|
||||
```bash
|
||||
$ sudo systemctl daemon-reload
|
||||
```
|
||||
|
||||
## 部署
|
||||
|
||||
### master
|
||||
|
||||
```bash
|
||||
$ sudo kubeadm init --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
|
||||
--pod-network-cidr 10.244.0.0/16 \
|
||||
--v 5 \
|
||||
--ignore-preflight-errors=all
|
||||
```
|
||||
|
||||
* `--pod-network-cidr 10.244.0.0/16` 参数与后续 CNI 插件有关,这里以 `flannel` 为例,若后续部署其他类型的网络插件请更改此参数。
|
||||
|
||||
> 执行可能出现错误,例如缺少依赖包,根据提示安装即可。
|
||||
|
||||
执行成功会输出
|
||||
|
||||
```bash
|
||||
...
|
||||
[addons] Applied essential addon: CoreDNS
|
||||
I1116 12:35:13.270407 86677 request.go:538] Throttling request took 181.409184ms, request: POST:https://192.168.199.100:6443/api/v1/namespaces/kube-system/serviceaccounts
|
||||
I1116 12:35:13.470292 86677 request.go:538] Throttling request took 186.088112ms, request: POST:https://192.168.199.100:6443/api/v1/namespaces/kube-system/configmaps
|
||||
[addons] Applied essential addon: kube-proxy
|
||||
|
||||
Your Kubernetes control-plane has initialized successfully!
|
||||
|
||||
To start using your cluster, you need to run the following as a regular user:
|
||||
|
||||
mkdir -p $HOME/.kube
|
||||
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
|
||||
sudo chown $(id -u):$(id -g) $HOME/.kube/config
|
||||
|
||||
You should now deploy a pod network to the cluster.
|
||||
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
|
||||
https://kubernetes.io/docs/concepts/cluster-administration/addons/
|
||||
|
||||
Then you can join any number of worker nodes by running the following on each as root:
|
||||
|
||||
kubeadm join 192.168.199.100:6443 --token cz81zt.orsy9gm9v649e5lf \
|
||||
--discovery-token-ca-cert-hash sha256:5edb316fd0d8ea2792cba15cdf1c899a366f147aa03cba52d4e5c5884ad836fe
|
||||
```
|
||||
|
||||
### node 工作节点
|
||||
|
||||
在 **另一主机** 重复 **部署** 小节以前的步骤,安装配置好 kubelet。根据提示,加入到集群。
|
||||
|
||||
```bash
|
||||
$ kubeadm join 192.168.199.100:6443 --token cz81zt.orsy9gm9v649e5lf \
|
||||
--discovery-token-ca-cert-hash sha256:5edb316fd0d8ea2792cba15cdf1c899a366f147aa03cba52d4e5c5884ad836fe
|
||||
```
|
||||
|
||||
## 查看服务
|
||||
|
||||
所有服务启动后,查看本地实际运行的 Docker 容器。这些服务大概分为三类:主节点服务、工作节点服务和其它服务。
|
||||
|
||||
### 主节点服务
|
||||
|
||||
* `apiserver` 是整个系统的对外接口,提供 RESTful 方式供客户端和其它组件调用;
|
||||
|
||||
* `scheduler` 负责对资源进行调度,分配某个 pod 到某个节点上;
|
||||
|
||||
* `controller-manager` 负责管理控制器,包括 endpoint-controller(刷新服务和 pod 的关联信息)和 replication-controller(维护某个 pod 的复制为配置的数值)。
|
||||
|
||||
### 工作节点服务
|
||||
|
||||
* `proxy` 为 pod 上的服务提供访问的代理。
|
||||
|
||||
### 其它服务
|
||||
|
||||
* Etcd 是所有状态的存储数据库;
|
||||
|
||||
## 使用
|
||||
|
||||
将 `/etc/kubernetes/admin.conf` 复制到 `~/.kube/config`
|
||||
|
||||
执行 `$ kubectl get all -A` 查看启动的服务。
|
||||
|
||||
由于未部署 CNI 插件,CoreDNS 未正常启动。如何使用 Kubernetes,请参考后续章节。
|
||||
|
||||
## 部署 CNI
|
||||
|
||||
这里以 `flannel` 为例进行介绍。
|
||||
|
||||
### flannel
|
||||
|
||||
检查 podCIDR 设置
|
||||
|
||||
```bash
|
||||
$ kubectl get node -o yaml | grep CIDR
|
||||
|
||||
# 输出
|
||||
podCIDR: 10.244.0.0/16
|
||||
podCIDRs:
|
||||
```
|
||||
|
||||
```bash
|
||||
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.11.0/Documentation/kube-flannel.yml
|
||||
```
|
||||
|
||||
## master 节点默认不能运行 pod
|
||||
|
||||
如果用 `kubeadm` 部署一个单节点集群,默认情况下无法使用,请执行以下命令解除限制
|
||||
|
||||
```bash
|
||||
$ kubectl taint nodes --all node-role.kubernetes.io/master-
|
||||
|
||||
# 恢复默认值
|
||||
# $ kubectl taint nodes NODE_NAME node-role.kubernetes.io/master=true:NoSchedule
|
||||
```
|
||||
|
||||
## 参考文档
|
||||
|
||||
* [官方文档](https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/)
|
||||
399
11_orchestration/setup/kubeadm.md
Normal file
399
11_orchestration/setup/kubeadm.md
Normal file
@@ -0,0 +1,399 @@
|
||||
# 使用 kubeadm 部署 kubernetes(CRI 使用 containerd)
|
||||
|
||||
`kubeadm` 提供了 `kubeadm init` 以及 `kubeadm join` 这两个命令作为快速创建 `kubernetes` 集群的最佳实践。
|
||||
|
||||
> **版本说明**:Kubernetes 版本更新较快(约每 4 个月一个新版本),本文档基于 Kubernetes 1.35 编写。请访问 [Kubernetes 官方发布页](https://kubernetes.io/releases/) 获取最新版本信息。
|
||||
|
||||
## 安装 containerd
|
||||
|
||||
参考 [安装 Docker](../../install) 一节添加 apt/yum 源,之后执行如下命令。
|
||||
|
||||
```bash
|
||||
# debian 系
|
||||
$ sudo apt install containerd.io
|
||||
|
||||
# rhel 系
|
||||
$ sudo yum install containerd.io
|
||||
```
|
||||
|
||||
## 配置 containerd
|
||||
|
||||
新建 `/etc/systemd/system/cri-containerd.service` 文件
|
||||
|
||||
```
|
||||
[Unit]
|
||||
Description=containerd container runtime for kubernetes
|
||||
Documentation=https://containerd.io
|
||||
After=network.target local-fs.target
|
||||
|
||||
[Service]
|
||||
ExecStartPre=-/sbin/modprobe overlay
|
||||
ExecStart=/usr/bin/containerd --config /etc/cri-containerd/config.toml
|
||||
|
||||
Type=notify
|
||||
Delegate=yes
|
||||
KillMode=process
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
# Having non-zero Limit*s causes performance problems due to accounting overhead
|
||||
# in the kernel. We recommend using cgroups to do container-local accounting.
|
||||
LimitNPROC=infinity
|
||||
LimitCORE=infinity
|
||||
LimitNOFILE=infinity
|
||||
# Comment TasksMax if your systemd version does not supports it.
|
||||
# Only systemd 226 and above support this version.
|
||||
TasksMax=infinity
|
||||
OOMScoreAdjust=-999
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
新建 `/etc/cri-containerd/config.toml` containerd 配置文件
|
||||
|
||||
```toml
|
||||
version = 2
|
||||
# persistent data location
|
||||
root = "/var/lib/cri-containerd"
|
||||
# runtime state information
|
||||
state = "/run/cri-containerd"
|
||||
plugin_dir = ""
|
||||
disabled_plugins = []
|
||||
required_plugins = []
|
||||
# set containerd's OOM score
|
||||
oom_score = 0
|
||||
|
||||
[grpc]
|
||||
address = "/run/cri-containerd/cri-containerd.sock"
|
||||
tcp_address = ""
|
||||
tcp_tls_cert = ""
|
||||
tcp_tls_key = ""
|
||||
# socket uid
|
||||
uid = 0
|
||||
# socket gid
|
||||
gid = 0
|
||||
max_recv_message_size = 16777216
|
||||
max_send_message_size = 16777216
|
||||
|
||||
[debug]
|
||||
address = ""
|
||||
format = "json"
|
||||
uid = 0
|
||||
gid = 0
|
||||
level = ""
|
||||
|
||||
[metrics]
|
||||
address = "127.0.0.1:1338"
|
||||
grpc_histogram = false
|
||||
|
||||
[cgroup]
|
||||
path = ""
|
||||
|
||||
[timeouts]
|
||||
"io.containerd.timeout.shim.cleanup" = "5s"
|
||||
"io.containerd.timeout.shim.load" = "5s"
|
||||
"io.containerd.timeout.shim.shutdown" = "3s"
|
||||
"io.containerd.timeout.task.state" = "2s"
|
||||
|
||||
[plugins]
|
||||
[plugins."io.containerd.gc.v1.scheduler"]
|
||||
pause_threshold = 0.02
|
||||
deletion_threshold = 0
|
||||
mutation_threshold = 100
|
||||
schedule_delay = "0s"
|
||||
startup_delay = "100ms"
|
||||
[plugins."io.containerd.grpc.v1.cri"]
|
||||
disable_tcp_service = true
|
||||
stream_server_address = "127.0.0.1"
|
||||
stream_server_port = "0"
|
||||
stream_idle_timeout = "4h0m0s"
|
||||
enable_selinux = false
|
||||
selinux_category_range = 1024
|
||||
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.10"
|
||||
stats_collect_period = 10
|
||||
# systemd_cgroup = false
|
||||
enable_tls_streaming = false
|
||||
max_container_log_line_size = 16384
|
||||
disable_cgroup = false
|
||||
disable_apparmor = false
|
||||
restrict_oom_score_adj = false
|
||||
max_concurrent_downloads = 3
|
||||
disable_proc_mount = false
|
||||
unset_seccomp_profile = ""
|
||||
tolerate_missing_hugetlb_controller = true
|
||||
disable_hugetlb_controller = true
|
||||
ignore_image_defined_volumes = false
|
||||
[plugins."io.containerd.grpc.v1.cri".containerd]
|
||||
snapshotter = "overlayfs"
|
||||
default_runtime_name = "runc"
|
||||
no_pivot = false
|
||||
disable_snapshot_annotations = false
|
||||
discard_unpacked_layers = false
|
||||
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
|
||||
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
|
||||
runtime_type = "io.containerd.runc.v2"
|
||||
pod_annotations = []
|
||||
container_annotations = []
|
||||
privileged_without_host_devices = false
|
||||
base_runtime_spec = ""
|
||||
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
|
||||
# SystemdCgroup enables systemd cgroups.
|
||||
SystemdCgroup = true
|
||||
# BinaryName is the binary name of the runc binary.
|
||||
# BinaryName = "runc"
|
||||
# BinaryName = "crun"
|
||||
# NoPivotRoot disables pivot root when creating a container.
|
||||
# NoPivotRoot = false
|
||||
|
||||
# NoNewKeyring disables new keyring for the container.
|
||||
# NoNewKeyring = false
|
||||
|
||||
# ShimCgroup places the shim in a cgroup.
|
||||
# ShimCgroup = ""
|
||||
|
||||
# IoUid sets the I/O's pipes uid.
|
||||
# IoUid = 0
|
||||
|
||||
# IoGid sets the I/O's pipes gid.
|
||||
# IoGid = 0
|
||||
|
||||
# Root is the runc root directory.
|
||||
Root = ""
|
||||
|
||||
# CriuPath is the criu binary path.
|
||||
# CriuPath = ""
|
||||
|
||||
# CriuImagePath is the criu image path
|
||||
# CriuImagePath = ""
|
||||
|
||||
# CriuWorkPath is the criu work path.
|
||||
# CriuWorkPath = ""
|
||||
[plugins."io.containerd.grpc.v1.cri".cni]
|
||||
bin_dir = "/opt/cni/bin"
|
||||
conf_dir = "/etc/cni/net.d"
|
||||
max_conf_num = 1
|
||||
conf_template = ""
|
||||
[plugins."io.containerd.grpc.v1.cri".registry]
|
||||
config_path = "/etc/cri-containerd/certs.d"
|
||||
[plugins."io.containerd.grpc.v1.cri".registry.headers]
|
||||
# Foo = ["bar"]
|
||||
[plugins."io.containerd.grpc.v1.cri".image_decryption]
|
||||
key_model = ""
|
||||
[plugins."io.containerd.grpc.v1.cri".x509_key_pair_streaming]
|
||||
tls_cert_file = ""
|
||||
tls_key_file = ""
|
||||
[plugins."io.containerd.internal.v1.opt"]
|
||||
path = "/opt/cri-containerd"
|
||||
[plugins."io.containerd.internal.v1.restart"]
|
||||
interval = "10s"
|
||||
[plugins."io.containerd.metadata.v1.bolt"]
|
||||
content_sharing_policy = "shared"
|
||||
[plugins."io.containerd.monitor.v1.cgroups"]
|
||||
no_prometheus = false
|
||||
[plugins."io.containerd.runtime.v2.task"]
|
||||
platforms = ["linux/amd64"]
|
||||
[plugins."io.containerd.service.v1.diff-service"]
|
||||
default = ["walking"]
|
||||
[plugins."io.containerd.snapshotter.v1.devmapper"]
|
||||
root_path = ""
|
||||
pool_name = ""
|
||||
base_image_size = ""
|
||||
async_remove = false
|
||||
```
|
||||
|
||||
## 安装 **kubelet** **kubeadm** **kubectl** **cri-tools** **kubernetes-cni**
|
||||
|
||||
### Ubuntu/Debian
|
||||
|
||||
```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 <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
|
||||
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
|
||||
EOF
|
||||
|
||||
$ apt-get update
|
||||
$ apt-get install -y kubelet kubeadm kubectl
|
||||
```
|
||||
|
||||
### CentOS/Fedora
|
||||
|
||||
```bash
|
||||
$ cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
|
||||
[kubernetes]
|
||||
name=Kubernetes
|
||||
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
|
||||
enabled=1
|
||||
gpgcheck=1
|
||||
repo_gpgcheck=1
|
||||
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
|
||||
EOF
|
||||
|
||||
$ sudo yum install -y kubelet kubeadm kubectl
|
||||
```
|
||||
|
||||
## 修改内核的运行参数
|
||||
|
||||
```bash
|
||||
$ cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
|
||||
net.bridge.bridge-nf-call-iptables = 1
|
||||
net.ipv4.ip_forward = 1
|
||||
net.bridge.bridge-nf-call-ip6tables = 1
|
||||
EOF
|
||||
|
||||
# 应用配置
|
||||
$ sysctl --system
|
||||
```
|
||||
|
||||
## 配置 kubelet
|
||||
|
||||
### 修改 `kubelet.service`
|
||||
|
||||
`/etc/systemd/system/kubelet.service.d/10-proxy-ipvs.conf` 写入以下内容
|
||||
|
||||
```bash
|
||||
# 启用 ipvs 相关内核模块
|
||||
[Service]
|
||||
ExecStartPre=-/sbin/modprobe ip_vs
|
||||
ExecStartPre=-/sbin/modprobe ip_vs_rr
|
||||
ExecStartPre=-/sbin/modprobe ip_vs_wrr
|
||||
ExecStartPre=-/sbin/modprobe ip_vs_sh
|
||||
```
|
||||
|
||||
执行以下命令应用配置。
|
||||
|
||||
```bash
|
||||
$ sudo systemctl daemon-reload
|
||||
```
|
||||
|
||||
## 部署
|
||||
|
||||
### master
|
||||
|
||||
```bash
|
||||
$ systemctl enable cri-containerd
|
||||
|
||||
$ systemctl start cri-containerd
|
||||
|
||||
$ sudo kubeadm init \
|
||||
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
|
||||
--pod-network-cidr 10.244.0.0/16 \
|
||||
--cri-socket /run/cri-containerd/cri-containerd.sock \
|
||||
--v 5 \
|
||||
--ignore-preflight-errors=all
|
||||
```
|
||||
|
||||
* `--pod-network-cidr 10.244.0.0/16` 参数与后续 CNI 插件有关,这里以 `flannel` 为例,若后续部署其他类型的网络插件请更改此参数。
|
||||
|
||||
> 执行可能出现错误,例如缺少依赖包,根据提示安装即可。
|
||||
|
||||
执行成功会输出
|
||||
|
||||
```bash
|
||||
...
|
||||
[addons] Applied essential addon: CoreDNS
|
||||
I1116 12:35:13.270407 86677 request.go:538] Throttling request took 181.409184ms, request: POST:https://192.168.199.100:6443/api/v1/namespaces/kube-system/serviceaccounts
|
||||
I1116 12:35:13.470292 86677 request.go:538] Throttling request took 186.088112ms, request: POST:https://192.168.199.100:6443/api/v1/namespaces/kube-system/configmaps
|
||||
[addons] Applied essential addon: kube-proxy
|
||||
|
||||
Your Kubernetes control-plane has initialized successfully!
|
||||
|
||||
To start using your cluster, you need to run the following as a regular user:
|
||||
|
||||
mkdir -p $HOME/.kube
|
||||
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
|
||||
sudo chown $(id -u):$(id -g) $HOME/.kube/config
|
||||
|
||||
You should now deploy a pod network to the cluster.
|
||||
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
|
||||
https://kubernetes.io/docs/concepts/cluster-administration/addons/
|
||||
|
||||
Then you can join any number of worker nodes by running the following on each as root:
|
||||
|
||||
kubeadm join 192.168.199.100:6443 --token cz81zt.orsy9gm9v649e5lf \
|
||||
--discovery-token-ca-cert-hash sha256:5edb316fd0d8ea2792cba15cdf1c899a366f147aa03cba52d4e5c5884ad836fe
|
||||
```
|
||||
|
||||
### node 工作节点
|
||||
|
||||
在 **另一主机** 重复 **部署** 小节以前的步骤,安装配置好 kubelet。根据提示,加入到集群。
|
||||
|
||||
```bash
|
||||
$ systemctl enable cri-containerd
|
||||
|
||||
$ systemctl start cri-containerd
|
||||
|
||||
$ kubeadm join 192.168.199.100:6443 \
|
||||
--token cz81zt.orsy9gm9v649e5lf \
|
||||
--discovery-token-ca-cert-hash sha256:5edb316fd0d8ea2792cba15cdf1c899a366f147aa03cba52d4e5c5884ad836fe \
|
||||
--cri-socket /run/cri-containerd/cri-containerd.sock
|
||||
```
|
||||
|
||||
## 查看服务
|
||||
|
||||
所有服务启动后,通过 `crictl` 查看本地实际运行的容器。这些服务大概分为三类:主节点服务、工作节点服务和其它服务。
|
||||
|
||||
```bash
|
||||
CONTAINER_RUNTIME_ENDPOINT=/run/cri-containerd/cri-containerd.sock crictl ps -a
|
||||
```
|
||||
|
||||
### 主节点服务
|
||||
|
||||
* `apiserver` 是整个系统的对外接口,提供 RESTful 方式供客户端和其它组件调用;
|
||||
|
||||
* `scheduler` 负责对资源进行调度,分配某个 pod 到某个节点上;
|
||||
|
||||
* `controller-manager` 负责管理控制器,包括 endpoint-controller(刷新服务和 pod 的关联信息)和 replication-controller(维护某个 pod 的复制为配置的数值)。
|
||||
|
||||
### 工作节点服务
|
||||
|
||||
* `proxy` 为 pod 上的服务提供访问的代理。
|
||||
|
||||
### 其它服务
|
||||
|
||||
* Etcd 是所有状态的存储数据库;
|
||||
|
||||
## 使用
|
||||
|
||||
将 `/etc/kubernetes/admin.conf` 复制到 `~/.kube/config`
|
||||
|
||||
执行 `$ kubectl get all -A` 查看启动的服务。
|
||||
|
||||
由于未部署 CNI 插件,CoreDNS 未正常启动。如何使用 Kubernetes,请参考后续章节。
|
||||
|
||||
## 部署 CNI
|
||||
|
||||
这里以 `flannel` 为例进行介绍。
|
||||
|
||||
### flannel
|
||||
|
||||
检查 podCIDR 设置
|
||||
|
||||
```bash
|
||||
$ kubectl get node -o yaml | grep CIDR
|
||||
|
||||
# 输出
|
||||
podCIDR: 10.244.0.0/16
|
||||
podCIDRs:
|
||||
```
|
||||
|
||||
```bash
|
||||
$ kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/v0.26.1/Documentation/kube-flannel.yml
|
||||
```
|
||||
|
||||
## master 节点默认不能运行 pod
|
||||
|
||||
如果用 `kubeadm` 部署一个单节点集群,默认情况下无法使用,请执行以下命令解除限制
|
||||
|
||||
```bash
|
||||
$ kubectl taint nodes --all node-role.kubernetes.io/master-
|
||||
|
||||
# 恢复默认值
|
||||
# $ kubectl taint nodes NODE_NAME node-role.kubernetes.io/master=true:NoSchedule
|
||||
```
|
||||
|
||||
## 参考文档
|
||||
|
||||
* [官方文档](https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/)
|
||||
* [Container runtimes](https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd)
|
||||
3
11_orchestration/setup/systemd.md
Normal file
3
11_orchestration/setup/systemd.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# 一步步部署 kubernetes 集群
|
||||
|
||||
可以参考 [opsnull/follow-me-install-kubernetes-cluster](https://github.com/opsnull/follow-me-install-kubernetes-cluster) 项目一步步部署 kubernetes 集群。
|
||||
Reference in New Issue
Block a user