Add kubeadm, close #438

Signed-off-by: Kang HuaiShuai <khs1994@khs1994.com>
This commit is contained in:
Kang HuaiShuai 2019-12-31 20:26:22 +08:00
parent bbfe14e0af
commit 0dcc67da1c
No known key found for this signature in database
GPG Key ID: 0A380828B1C243A7
8 changed files with 197 additions and 96 deletions

View File

@ -30,7 +30,7 @@ jobs:
PCIT_EMAIL: khs1994@khs1994.com PCIT_EMAIL: khs1994@khs1994.com
PCIT_GIT_TOKEN: ${{ secrets.PCIT_GIT_TOKEN }} PCIT_GIT_TOKEN: ${{ secrets.PCIT_GIT_TOKEN }}
PCIT_GIT_URL: github.com/docker-practice/vuepress PCIT_GIT_URL: github.com/docker-practice/vuepress
PCIT_KEEP_HISTORY: "1" PCIT_KEEP_HISTORY: "true"
PCIT_LOCAL_DIR: .vuepress/dist PCIT_LOCAL_DIR: .vuepress/dist
PCIT_MESSAGE: Sync from yeasy/docker_practice@${{github.sha}} by PCIT PCIT_MESSAGE: Sync from yeasy/docker_practice@${{github.sha}} by PCIT
PCIT_TARGET_BRANCH: master PCIT_TARGET_BRANCH: master
@ -45,7 +45,7 @@ jobs:
PCIT_EMAIL: khs1994@khs1994.com PCIT_EMAIL: khs1994@khs1994.com
PCIT_GIT_TOKEN: ${{ secrets.CODING_GIT_TOKEN }} PCIT_GIT_TOKEN: ${{ secrets.CODING_GIT_TOKEN }}
PCIT_GIT_URL: git.dev.tencent.com/khs1994/docker_practice PCIT_GIT_URL: git.dev.tencent.com/khs1994/docker_practice
PCIT_KEEP_HISTORY: "1" PCIT_KEEP_HISTORY: "true"
PCIT_LOCAL_DIR: .vuepress/dist PCIT_LOCAL_DIR: .vuepress/dist
PCIT_MESSAGE: Sync from yeasy/docker_practice@${{github.sha}} by PCIT PCIT_MESSAGE: Sync from yeasy/docker_practice@${{github.sha}} by PCIT
PCIT_TARGET_BRANCH: master PCIT_TARGET_BRANCH: master

View File

@ -109,8 +109,9 @@ module.exports = {
collapsable: false, collapsable: false,
children: [ children: [
"setup/", "setup/",
"setup/docker", "setup/kubeadm",
"setup/docker-desktop", "setup/docker-desktop",
"setup/systemd",
] ]
}, },
{ {

View File

@ -1,5 +1,7 @@
# 修订记录 # 修订记录
* 1.2.0 2020-12-31
* 1.1.0 2019-12-31 * 1.1.0 2019-12-31
* 全面支持 v19.x 新版本 * 全面支持 v19.x 新版本
* 增加 `BuildKit` * 增加 `BuildKit`

View File

@ -121,8 +121,9 @@
* [基本概念](kubernetes/concepts.md) * [基本概念](kubernetes/concepts.md)
* [架构设计](kubernetes/design.md) * [架构设计](kubernetes/design.md)
* [部署 Kubernetes](kubernetes/setup/README.md) * [部署 Kubernetes](kubernetes/setup/README.md)
* [使用 Docker 容器部署](kubernetes/setup/docker.md) * [使用 kubeadm 部署 kubernetes](kubernetes/setup/kubeadm.md)
* [ Docker Desktop 使用](kubernetes/setup/docker-desktop.md) * [ Docker Desktop 使用](kubernetes/setup/docker-desktop.md)
* [一步步部署 kubernetes 集群](kubernetes/setup/systemd.md)
* [Kubernetes 命令行 kubectl](kubernetes/kubectl/README.md) * [Kubernetes 命令行 kubectl](kubernetes/kubectl/README.md)
* [容器与云计算](cloud/README.md) * [容器与云计算](cloud/README.md)
* [简介](cloud/intro.md) * [简介](cloud/intro.md)

View File

@ -4,6 +4,8 @@
你可以使用以下几种方式部署 Kubernetes 你可以使用以下几种方式部署 Kubernetes
* Docker 容器 * kubeadm
* docker-desktop
* k3s
接下来的小节会对以上几种方式进行详细介绍 接下来的小节会对以上几种方式进行详细介绍

View File

@ -1,91 +0,0 @@
# 使用 Docker 容器部署 Kubernetes
最快速体验 Kubernetes 的方式就是在本地通过 Docker 的方式来启动相关进程
下图展示了在单节点使用 Docker 快速部署一套 Kubernetes 的拓扑
![ Docker 中启动 Kubernetes](_images/k8s-singlenode-docker.png)
Kubernetes 依赖 Etcd 服务来维护所有主节点的状态
## 启动 Etcd 服务
```bash
docker run --net=host -d gcr.io/google_containers/etcd:2.0.9 /usr/local/bin/etcd --addr=127.0.0.1:4001 --bind-addr=0.0.0.0:4001 --data-dir=/var/etcd/data
```
## 启动主节点
启动 kubelet
```bash
docker run --net=host -d -v /var/run/docker.sock:/var/run/docker.sock gcr.io/google_containers/hyperkube:v0.17.0 /hyperkube kubelet --api_servers=http://localhost:8080 --v=2 --address=0.0.0.0 --enable_server --hostname_override=127.0.0.1 --config=/etc/kubernetes/manifests
```
## 启动服务代理
```bash
docker run -d --net=host --privileged gcr.io/google_containers/hyperkube:v0.17.0 /hyperkube proxy --master=http://127.0.0.1:8080 --v=2
```
## 测试状态
在本地访问 `8080` 端口可以获取到如下的结果
```bash
$ curl 127.0.0.1:8080
{
"paths": [
"/api",
"/api/v1beta1",
"/api/v1beta2",
"/api/v1beta3",
"/healthz",
"/healthz/ping",
"/logs/",
"/metrics",
"/static/",
"/swagger-ui/",
"/swaggerapi/",
"/validate",
"/version"
]
}
```
## 查看服务
所有服务启动后查看本地实际运行的 Docker 容器有如下几个
```bash
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee054db2516c gcr.io/google_containers/hyperkube:v0.17.0 "/hyperkube schedule 2 days ago Up 1 days k8s_scheduler.509f29c9_k8s-master-127.0.0.1_default_9941e5170b4365bd4aa91f122ba0c061_e97037f5
3b0f28de07a2 gcr.io/google_containers/hyperkube:v0.17.0 "/hyperkube apiserve 2 days ago Up 1 days k8s_apiserver.245e44fa_k8s-master-127.0.0.1_default_9941e5170b4365bd4aa91f122ba0c061_6ab5c23d
2eaa44ecdd8e gcr.io/google_containers/hyperkube:v0.17.0 "/hyperkube controll 2 days ago Up 1 days k8s_controller-manager.33f83d43_k8s-master-127.0.0.1_default_9941e5170b4365bd4aa91f122ba0c061_1a60106f
30aa7163cbef gcr.io/google_containers/hyperkube:v0.17.0 "/hyperkube proxy -- 2 days ago Up 1 days jolly_davinci
a2f282976d91 gcr.io/google_containers/pause:0.8.0 "/pause" 2 days ago Up 2 days k8s_POD.e4cc795_k8s-master-127.0.0.1_default_9941e5170b4365bd4aa91f122ba0c061_e8085b1f
c060c52acc36 gcr.io/google_containers/hyperkube:v0.17.0 "/hyperkube kubelet 2 days ago Up 1 days serene_nobel
cc3cd263c581 gcr.io/google_containers/etcd:2.0.9 "/usr/local/bin/etcd 2 days ago Up 1 days happy_turing
```
这些服务大概分为三类主节点服务工作节点服务和其它服务
### 主节点服务
* `apiserver` 是整个系统的对外接口提供 RESTful 方式供客户端和其它组件调用
* `scheduler` 负责对资源进行调度分配某个 pod 到某个节点上
* `controller-manager` 负责管理控制器包括 endpoint-controller刷新服务和 pod 的关联信息 replication-controller维护某个 pod 的复制为配置的数值
### 工作节点服务
* `kubelet` 是工作节点执行操作的 agent负责具体的容器生命周期管理根据从数据库中获取的信息来管理容器并上报 pod 运行状态等
* `proxy` pod 上的服务提供访问的代理
### 其它服务
* Etcd 是所有状态的存储数据库
* `gcr.io/google_containers/pause:0.8.0` Kubernetes 启动后自动 pull 下来的测试镜像

183
kubernetes/setup/kubeadm.md Normal file
View File

@ -0,0 +1,183 @@
# 使用 kubeadm 部署 kubernetes
`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=-modprobe ip_vs
ExecStartPre=-modprobe ip_vs_rr
ExecStartPre=-modprobe ip_vs_wrr
ExecStartPre=-modprobe ip_vs_sh
```
执行以下命令应用配置
```bash
$ sudo systemctl daemon-reload
```
## 部署
### master
```bash
$ sudo kubeadm init --image-repository gcr.azk8s.cn/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/24
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
```

View File

@ -0,0 +1,3 @@
# 一步步部署 kubernetes 集群
可以参考 [opsnull/follow-me-install-kubernetes-cluster](https://github.com/opsnull/follow-me-install-kubernetes-cluster) 项目一步步部署 kubernetes 集群。