写在前面的话

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

第一节学习了一些 K8S 的概念,上一节学了集群安装,这一节就来看看,怎么使用集群。

 

 

增删查改

 

再学习 K8S 之前,为了便于使用,可以配置 K8S 命令补全:

yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

查看帮助:

kubectl -h

结果如下表:

参数 说明
基础命令(Beginner)
create 从文件或者输入创建
expose 使用 replication controller, service, deployment 或 pod 并暴露它作为一个新的 Kubernetes Service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
基础命令(Intermediate)
explain 查看资源的文档
get 显示一个或更多 resources
edit 在服务器上编辑一个资源
delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
Deploy 命令
rollout Manage the rollout of a resource
scale 为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量
autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量
Cluster 管理命令
certificate 修改 certificate 资源
cluster-info 显示集群信息
top Display Resource (CPU/Memory/Storage) usage
cordon 标记 node 为 unschedulable
uncordon 标记 node 为 schedulable
drain Drain node in preparation for maintenance
taint 更新一个或者多个 node 上的 taints
Troubleshooting 命令
describe 显示一个指定 resource 或者 group 的 resources 详情
logs 输出容器在 pod 中的日志
attach Attach 到一个运行中的 container
exec 在一个 container 中执行一个命令
port-forward Forward one or more local ports to a pod
proxy 运行一个 proxy 到 Kubernetes API server
cp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories
auth Inspect authorization
Advanced 命令
diff Diff live version against would-be applied version
apply 通过文件名或标准输入流(stdin)对资源进行配置
patch 使用 strategic merge patch 更新一个资源的 field(s)
replace 通过 filename 或者 stdin替换一个资源
wait Experimental: Wait for a specific condition on one or many resources
convert 在不同的 API versions 转换配置文件
kustomize Build a kustomization target from a directory or a remote url
Settings 命令
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or zsh)
Other 命令
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config 修改 kubeconfig 文件
plugin Provides utilities for interacting with plugins
version 输出 client 和 server 的版本信息

 

创建一个应用程序:

kubectl run nginx-demo --image=nginx:1.14-alpine --port=80 --replicas=1

格式:kubectl run <名字> [参数1 ...]

常用参数说明:

参数 说明
--dry-run 不真正执行,只测试
--image=nginx:1.11 指定镜像
--port=80 Pod 暴露的端口
--replicas=2 副本数量
--restart=Never 指定重启规则
--env="DNS_DOMAIN=cluster" 指定环境变量

结果如图:

【03】Kubernets:K8S 操作入门 随笔 第1张

红色部分表示创建成功,但是在新版本中有一个提示,告诉我们 run 在将来会被废弃,推荐我们使用 create。

删除方法:

kubectl delete deployments.apps nginx-demo

查看创建结果:

kubectl get deployment

默认创建的都属于 deployment,我们可以加 -o wide 查看更详细的信息,结果如下:

【03】Kubernets:K8S 操作入门 随笔 第2张

 

查看创建的 Pod:

kubectl get pods

结果如下:

【03】Kubernets:K8S 操作入门 随笔 第3张

可以看到 Pod 的 IP 在 Pod 网段的 IP 地址,以及这个 Pod 运行在哪个节点。

curl 10.244.2.4:80

集群中测试这个地址,结果如下:

【03】Kubernets:K8S 操作入门 随笔 第4张

现象:能够访问到 nginx。

结论:在第一节中讲过,Pod 的网络并不是真实的网络,没配置在任何网卡上面,所以就目前而言,能够提供的访问也仅仅是当前 K8S 集群中能够访问。

那如何让外部也能访问到服务?

 

创建一个 service

kubectl expose deployment nginx-demo --name=nginx-svc --port=80 --target-port=80 --type=NodePort

结果如图:

【03】Kubernets:K8S 操作入门 随笔 第5张

目的是将我们之前创建的 deployment nginx-demo expose 出来,并给 service 命名为 nginx-svc。

查看创建结果:这里 svc 其实是简写的 service

kubectl get svc

结果如图:

【03】Kubernets:K8S 操作入门 随笔 第6张

可以看到服务映射出来的端口(随机端口),所以可以使用集群中任意 Node 的 IP 加这个随机端口在外部访问测试:

【03】Kubernets:K8S 操作入门 随笔 第7张

结论:如果 Service 的 type 为 NodePort,那么在 expose 之后可以使用任意节点 IP 进行访问。

 

删除 Pod 测试:

kubectl delete pod nginx-demo-549b77b6c5-cs29p

结果如图:

【03】Kubernets:K8S 操作入门 随笔 第8张

再度查看:

kubectl get pod -o wide

结果如图:

【03】Kubernets:K8S 操作入门 随笔 第9张

可以发现,K8S 在删除旧的 Pod 以后,又会在其他节点上创建一个新的 Pod。而且我们外部访问依然没问题。

同时也说明了一个问题,Service 和 Pod 间的关联是基于标签的关联。

 

查看 service 和 Pod 详细信息:

kubectl describe service nginx-svc
kubectl describe pod nginx-demo-549b77b6c5-jtv7f

结果如图:

【03】Kubernets:K8S 操作入门 随笔 第10张

可以发现,service 和 Pod 都拥有相同的 Label(标签)。

 

deployment 扩容和缩减:

kubectl scale --replicas=3 deployment nginx-demo

结果如图:

【03】Kubernets:K8S 操作入门 随笔 第11张

缩减同理!

 

版本升级:

kubectl set image deployment nginx-demo nginx-demo=nginx:1.15-alpine --record

将 1.14 版本升级到 1.15 版本:

【03】Kubernets:K8S 操作入门 随笔 第12张

 

版本回滚:

kubectl rollout undo deployment nginx-demo

可以通过  --to-revision 指定版本,结果如图:

【03】Kubernets:K8S 操作入门 随笔 第13张

 

 

小结

 

这里主要的内容其实就是:

创建 (deployment)Pod --> 查看 (deployment)Pod --> expose (deployment)Pod 让外部访问(创建 Service) --> 升级回滚版本

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄