提交 3c822188 编写于 作者: F feilong

完成k8s介绍

上级 18311be2
{
"type": "code_options",
"author": null,
"author": "feilong",
"source": "install.md",
"notebook_enable": false,
"exercise_id": "3767250c65a64d4b89f714ed9185a6f2"
......
{
"type": "code_options",
"author": null,
"author": "feilong",
"source": "install_4_learning.md",
"notebook_enable": false,
"exercise_id": "278162c0c42641d3a539f557d7e86f05"
......
{
"type": "code_options",
"author": null,
"author": "feilong",
"source": "install_minikube.md",
"notebook_enable": false,
"exercise_id": "511d5df92425424197302efa19cad475"
......
{
"type": "code_options",
"author": null,
"author": "feilong",
"source": "minikube_commnds.md",
"notebook_enable": false,
"exercise_id": "0ba6346aee9042ea8a44a443be4ed11a"
......
{
"type": "code_options",
"author": null,
"author": "feilong",
"source": "start_k8s.md",
"notebook_enable": false,
"exercise_id": "d9a94bb8d8ff4d149ba2c02481ddef6c"
......
......@@ -5,7 +5,8 @@
"export": [
"start_k8s.json",
"install_kind.json",
"get_info.json"
"get_info.json",
"start_k8s_three_nodes.json"
],
"keywords_must": [],
"keywords_forbid": []
......
{
"type": "code_options",
"author": null,
"author": "feilong",
"source": "get_info.md",
"notebook_enable": false,
"exercise_id": "b267edaeac6d49d2bde26232a9e2cba0"
......
{
"type": "code_options",
"author": null,
"author": "feilong",
"source": "install_kind.md",
"notebook_enable": false,
"exercise_id": "34ae63ea33ab4500844b158d26eb8c6a"
......
{
"type": "code_options",
"author": null,
"author": "feilong",
"source": "start_k8s.md",
"notebook_enable": false,
"exercise_id": "585600fc1b0e4894baf2066148693be6"
......
{
"type": "code_options",
"author": null,
"source": "start_k8s_three_nodes.md",
"notebook_enable": false,
"exercise_id": "dc46afbe45344def85ffb7835cc9fd21"
}
\ No newline at end of file
# 创建一个典型的3节点k8s集群
k8s是一个分布式容器编排引擎,集群的node有`Control Plane``Worker` 两种类型。为了更好地认识 k8s 集群的构架,需要使用 kind 创建一个3节点的k8s集群。
首先添加一个 3 节点 yaml 配置信息,yaml 文件格式是云原生构架的主要配置文件格式。云原生构架下的一个设计哲学就是使用“声明式”方式编程。通过 yaml 配置文件指定各种策略选择。
```yaml
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
```
将上述文件保存到`hello-k8s-config.yaml`,其中
* `apiVersion` 指定里采用的k8s api版本
* `nodes` 配置了一个`control-plane`角色的节点和两个`worker`角色的节点
* 事实上可以创建不只一个 `control-plane` 角色节点
其次,使用如下的命令构建:
```bash
kind create cluster --config hello-k8s-config.yaml --name hello-k8s
```
![](./img/kind_three_nodes.png)
查看集群的节点列表
![](./img/kind_three_nodes_info.png)
以下说法错误的是?
## 答案
一个 k8s 集群只能创建一个 control-plane 角色的节点
## 选项
### A
角色为 worker 的节点是 k8s 的工作节点
### B
角色为 control-plane 的节点是 k8s 的控制节点
### C
YAML配置文件是云原生声明式API的一种方式
{
"node_id": "cloud_native-96843019dff240fab2a76b6d53be1775",
"keywords": [],
"children": [],
"export": [
"config_view.json"
],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": null,
"source": "config_view.md",
"notebook_enable": false,
"exercise_id": "e4ed448ebffb4d61b7c95fe763792efb"
}
\ No newline at end of file
# 使用 kubectl 查看和切换 context
前面的节,我们分别使用 minikube 和 kind 创建了不同名字的 k8s 集群,kubectl 是和哪个集群通讯的呢?可以通过以下命令先看下全局信息
```bash
kubectl config view
```
可以看到输出了很多有用的信息:
```yaml
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://127.0.0.1:60712
name: kind-hello-k8s
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://127.0.0.1:50225
name: kind-test
- cluster:
certificate-authority: /Users/feilong/.minikube/ca.crt
extensions:
- extension:
last-update: Wed, 16 Mar 2022 19:13:01 CST
provider: minikube.sigs.k8s.io
version: v1.25.2
name: cluster_info
server: https://127.0.0.1:49371
name: minikube
contexts:
- context:
cluster: kind-hello-k8s
user: kind-hello-k8s
name: kind-hello-k8s
- context:
cluster: kind-test
user: kind-test
name: kind-test
- context:
cluster: minikube
extensions:
- extension:
last-update: Wed, 16 Mar 2022 19:13:01 CST
provider: minikube.sigs.k8s.io
version: v1.25.2
name: context_info
namespace: default
user: minikube
name: minikube
current-context: kind-hello-k8s
kind: Config
preferences: {}
users:
- name: kind-hello-k8s
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
- name: kind-test
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
- name: minikube
user:
client-certificate: /Users/feilong/.minikube/profiles/minikube/client.crt
client-key: /Users/feilong/.minikube/profiles/minikube/client.key
```
主要包括这几类信息:
* `clusters` 列出了所有的 k8s 集群,例如这里有两个kind创建的集群:`kind-hello-k8s``kind-test` 以及一个 minikube 创建的集群`minikube`
* `context` 列出了每个 k8s 集群对应的上下文信息
* `users` 列出了每个 k8s 集群的用户信息
* `current-context` 指定了当前 kubectl 连接的集群上下文,这里是 `kind-hello-k8s`
首先,通过`kubectl config get-contexts` 查看下有哪些集群上下文,通过`kubectl config current-context` 确认当前 k8s 集群上下文
![](./img/config_get.png)
其次,通过`kubectl get nodes`查看当前 kubectl 连接的集群`kind-hello-k8s`的节点列表:
![](./img/kubectl_get_nodes.png)
现在,通过`kubectl config use-context kind-test` 切换连接的 k8s 集群,然后再次查看信息
![](./img/kubectl_switch_context.png)
以下说法错误的是?
## 答案
kubectl 只能连接最后一次创建的 k8s 集群
## 选项
### A
kubectl 可以切换连接的集群上下文
### B
kubectl 查看当前连接的 k8s 集群节点信息
### C
kubectl 可以查看环境里装的不同的集群信息
{
"node_id": "cloud_native-82e0635057c847f99823da536e9eba68",
"keywords": [],
"children": [],
"export": [
"create_deployment.json",
"create_service.json",
"rs_pod.json"
],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": null,
"source": "create_deployment.md",
"notebook_enable": false,
"exercise_id": "9f699ea545304e6dbd354df5e63b3d96"
}
\ No newline at end of file
# 服务部署(1): 使用 kubectl 部署(deployment)
回顾我们在 “使用Dockerfile制作镜像” 一节里制作过的 python docker 镜像程序。镜像推送到镜像仓库的tag名字是`fanfeilong/cloud_native_hello_py`,源码结构如下:
```bash
├── .dockerignore
├── .gitignore
├── Dockerfile
├── README.md
└── src
├── main.py
└── requirements.txt
```
我们在根目录下创建一个 k8s 的部署配置文件,文件是 yaml 格式,文件名为`k8s_python_sample_code.deployment.yml`,内容如下,注意`name`字段不能是下划线命名风格:
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cloud-native-hello-py
labels:
app: cloud-native-hello-py
spec:
replicas: 3
selector:
matchLabels:
app: cloud-native-hello-py
template:
metadata:
labels:
app: cloud-native-hello-py
spec:
containers:
- name: cloud-native-hello-py
image: fanfeilong/cloud_native_hello_py
ports:
- containerPort: 1024
```
通过一组操作来部署和查看状态
* 使用命令 `kubectl create -f k8s.deployment.yaml` 部署到 k8s 集群,“部署” 本身被当一种资源创建过程
* 使用命令 `kubectl get deployments` 查看部署情况
* 使用命令`kubectl rollout status deployment/cloud-native-hello-py`查看状态。
![](./img/deployment.png)
以下说法错误的是?
## 答案
k8s.deployment.yaml 里不需要指定目标部署 Python 服务的 docker 镜像名字
## 选项
### A
kubectl 对一种资源的操作,常见的包含:创建、查看列表、获取状态三种
### B
使用 yaml 配置部署信息,使用`kubectl create -f` 命令创建资源,这就是 k8s 的声明式 api 的做法
### C
deployment 的配置文件yaml里,`name` 字段不能带下划线风格
### D
containerPort: 1024 用来暴露容器导出端口
\ No newline at end of file
{
"type": "code_options",
"author": null,
"source": "create_service.md",
"notebook_enable": false,
"exercise_id": "40ac07260ef34085961c39064ad61b09"
}
\ No newline at end of file
# 服务部署(3): 使用 kubectl 启动服务(service)
经过 deployment,cloud_native_hello_py 项目的目录结构如下:
```bash
.
├── Dockerfile
├── README.md
├── k8s.deployment.yaml
└── src
├── main.py
└── requirements.txt
```
在根目录下增加一个文件`k8s.service.yaml`,内容如下:
```yaml
apiVersion: v1
kind: Service
metadata:
name: cloud-native-hello-py
spec:
selector:
app: cloud-native-hello-py
ports:
- protocol: TCP
port: 1024
targetPort: 1024
```
注意 targetPort 必须和 deployment 步骤里容器的导出端口一致
使用如下的命令来创建服务
```bash
kubectl create -f k8s.service.yaml
```
![](./img/service.png)
对服务做下端口转发
![](./img/port_forward.png)
使用浏览器直接访问
![](./img/it_works_html.png)
HelloWorld! 恭喜你成功地在 K8S 上部署启动了一个 Python Flask 后端服务。
下面说法错误的是?
## 答案
kubectl 只能创建一个服务
## 选项
### A
service 可以配置将端口映射到 deployment 里配置的容器端口
### B
转发本机端口到服务端口后,本机可以直接访问服务
### C
服务配置里的 selector 配置了选择 pod 的策略
{
"type": "code_options",
"author": null,
"source": "rs_pod.md",
"notebook_enable": false,
"exercise_id": "03679801cd5e4b2aad9c10f2adaad982"
}
\ No newline at end of file
# 服务部署(2): 使用 kubectl 查看 ReplicaSet 和 Pods
k8s 通过 deployment 部署,deployment 管理副本集合ReplicaSet ,而 ReplicaSet 包含多个Pod。那么Pod又是什么呢?
Pod是一个或多个容器的组合,这些容器共享存储、网络和命名空间,以及如何运行的规范。Pod是 K8s 的最小可部署单元。
我们先通过命令`kubectl get rs`直观地看下 ReplicaSet
![](./img/rs.png)
我们再通过命令`kubectl get pods`直观地看下 pods
![](./img/pods.png)
可以看到有一个 ReplicaSet,管理三个 Pods。ReplicaSet 管理多个 Pod,确保以指定个数的副本运行部署的服务。
ReplicaSet 的信息解释:
* NAME:ReplicaSet 名字
* DESIRED:需要的 pod 副本个数
* CURRENT:当前运行的 pod 副本个数
* READY: 有多少个副本对用户可用
* AGE:运行开始到现在的时间
Pod的列信息大同小异:
* NAME: 名字
* READY: 就绪状态
* STATUS: 运行状态
* RESTARTS:重启次数
* AGE:运行开始到现在的时间
以下说法错误的是?
## 答案
通常一个 ReplicaSet 只需要一个 pod 即可
## 选项
### A
deployment 可以对 ReplicaSet 管理的 pod 实行滚动升级
### B
Pod 管理内部容器,这些容器共享存储、网络和命名空间
### C
k8s 使用 ReplicaSet 管理多个 Pods
{
"node_id": "cloud_native-3a407b55039e4e29b802e5047caf881b",
"keywords": [],
"children": [],
"export": [
"introduce.json"
],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": null,
"source": "introduce.md",
"notebook_enable": false,
"exercise_id": "067897924b1447589d6232c0b4aca432"
}
\ No newline at end of file
# 生产环境 k8s 三件套:kubelet, kubectl, kubeadm
在生产环境上,例如 centos linux系统上,使用 kubelet, kubectl, kubeadm 三件套可以部署 k8s 集群。
cenos 上安装 k8s 步骤如下
* 安装 docker
* 安装 kubelet, kubectl, kubeadm
* 使用 kubeadm 初始化集群
例如,安装三件套的命令如下
```bash
sudo yum install -y kubectl kubelet kubeadm
sudo systemctl enable kubelet
sudo systemctl start kubelet
```
使用 kubeadm 初始化集群的命令如下
```bash
kubeadm init --apiserver-advertise-address=0.0.0.0 \
--apiserver-cert-extra-sans=127.0.0.1 \
--image-repository=registry.aliyuncs.com/google_containers \
--ignore-preflight-errors=all \
--kubernetes-version=v1.21.1 \
--service-cidr=10.10.0.0/16 \
--pod-network-cidr=10.18.0.0/16
```
以下说法错误的是?
## 答案
kubectl 不能用来连接生产环境 k8s 集群
## 选项
### A
kubeadm 用来初始化 k8s 集群
### B
在 k8s 节点上应该运行 kubelet
### C
生产环境不应该用 minikube 和 kind 创建 k8s 集群
\ No newline at end of file
{
"node_id": "cloud_native-956e6d175eb343468b21131c54afb4c6",
"keywords": [],
"children": [],
"export": [
"introduce.json"
],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": null,
"source": "introduce.md",
"notebook_enable": false,
"exercise_id": "6c420e94227948279466358d4548c0d4"
}
\ No newline at end of file
# k8s 基础组件介绍
到目前为止,我们从 k8s 命令行安装和操作的角度理解到了 k8s 的很多基本概念:
* 容器(docker)里可以跑代码
* pod 可以管理容器(docker)
* ReplicaSet 可以管理多 pod
* 通过 deployment 可以部署一个 ReplicaSet
* deployement 可以通过 service 暴露给集群外
* k8s的集群节点分 control-plane 和 worker 两种节点
我们也掌握了安装 k8s 的概念
* kubectl 可以用来和 k8s 集群通讯,是 k8s 的命令行客户端
* 使用 minikube/kind 可以创建学习环境 k8s 集群
* 使用 kubeadm 可以创建生产环境 k8s
现在,我们简要介绍下 k8s 的核心组件
* etcd : 实现了raft分布式一致性协议的元数据存储
* apiserver: 提供了元数据读写 etcd 的api
* Controller Manager : 集群内部的控制管理中心
* Scheduler: 集群内部的调度器,对 pod 进行调度
* Kubelet:负责 Node 节点上的 pod 的增删查改
* Proxy: Service的代理
自己搜索资料,以下说法错误的是?
## 答案
kubectl 不能连接 kubeadm 创建的 k8s 集群
## 选项
### A
apiserver 的 api 是有版本划分的
### B
minikube 和 kind 会自动安装 kubelet,但是 kubeadm 不会自动安装 kubelet
### C
Scheduler 是k8s的调度器
{
"node_id": "cloud_native-3d43dbd9c50e407a806cb4f7703c4160",
"keywords": [],
"children": [],
"export": [
"introduce.json"
],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file
{
"type": "code_options",
"author": null,
"source": "introduce.md",
"notebook_enable": false,
"exercise_id": "e0d420a053a8499f8e42c3b29e673320"
}
\ No newline at end of file
# k8s 设计理念
以下关于k8s设计说法正确的是?
a. 所有API应该是声明式的
b. API对象是彼此互补而且可组合的
c. 高层API以操作意图为基础设计
d. 低层API根据高层API的控制需要设计
e. 尽量避免简单封装,不要有在外部API无法显式知道的内部隐藏的机制
f. API操作复杂度与对象数量成正比
g. API对象状态不能依赖于网络连接状态
h. 尽量避免让操作机制依赖于全局状态,因为在分布式系统中要保证全局状态的同步是非常困难的
## 答案
全部正确
## 选项
### A
b,c,d,e,f,g,h
### B
a,b,c,d,e,f,g
### C
a,b,c,d,e,f,g,h
\ No newline at end of file
......@@ -50,7 +50,7 @@
}
},
{
"使用docker运行服务": {
"使用docker运行Python程序": {
"node_id": "cloud_native-9d1d7311d4ee49c1aa36eef9f38000fd",
"keywords": [],
"children": [],
......@@ -94,6 +94,24 @@
"keywords_must": [],
"keywords_forbid": []
}
},
{
"kubectl查看和切换context": {
"node_id": "cloud_native-96843019dff240fab2a76b6d53be1775",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
}
},
{
"kubectl部署一个Python程序": {
"node_id": "cloud_native-82e0635057c847f99823da536e9eba68",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
}
}
],
"keywords_must": [],
......@@ -104,7 +122,35 @@
"容器编排(生产环境 kubernates)": {
"node_id": "cloud_native-be3fffb284e242489400a9c65ae1b217",
"keywords": [],
"children": [],
"children": [
{
"CentOS安装kubelet,kubectl,kubeadm": {
"node_id": "cloud_native-3a407b55039e4e29b802e5047caf881b",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
}
},
{
"k8s基础组件": {
"node_id": "cloud_native-956e6d175eb343468b21131c54afb4c6",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
}
},
{
"k8s设计理念": {
"node_id": "cloud_native-3d43dbd9c50e407a806cb4f7703c4160",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
}
}
],
"keywords_must": [],
"keywords_forbid": []
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册