一、Kubectl命令工具
Kubectl 是一个用于操作 Kubernetes 集群的命令行工具,利用 Kubectl 的各种子命令可以实现各种功能,在管理 Kubernetes 集群过程中kubectl是非常实用的工具。
Kube-apiserver是整个Kubernetes集群管理的入口。API Server运行在Kubernetes 集群的主管理节点(Master)上,用户需要通过 API Server 配置和组织集群,同时集群中各节点与 Etcd 存储的交互也是通过 API Server 来进行的。
API Server 实现了一套 RESTfull 的接口,用户可以直接使用 API 与 API Server 进行交互。另外官方还提供了一个客户端 Kubectl 工具集,可直接通过 Kubectl 命令行的方式与集群进行交互。
1.1、kubectl 命令行的语法
kubectl [command] [TYPE] [NAME] [flags]
- command:子命令,用于操作 Kubernetes 集群资源对象的命令。例如:create、delete、describe、get、apply 等。
- TYPE:资源对象的类型,区分大小写,能以单数、复数或者简写形式表示。例如:kubectl get pod pod1、kubectl get pods pod1、kubectl get po pod1 三种 TYPE 是等价的。
- NAME:资源对象名称,区分大小写。如果不指定名称,系统则返回属于 TYPE 的全部对象列表,例如,执行 kubectl get pods 命令即可返回所有 Pod 的列表。
- flags:kubectl 子命令的可选参数,例如使用“-s”指定 API Server 的 URL 地址而不用默认值。
kubectl 的子命令非常丰富,涵盖了对 Kubernetes 集群的主要操作,包括资源对象的创建、删除、查看、修改、配置、运行等操作。
1.2、kubectl命令列表
1.3、使用 Kubectl 工具容器资源
Kubectl 是管理Kubernetes集群的命令行工具,通过生成的 json 格式传递给 API Server 进行创建、查看、管理的操作。
使用 kubectl --help 命令可以查看 Kubectl 帮助命令,其中包括基本命令、部署命令、群集管理命令、调试命令以及高级命令等。
[root@k8s-master ~]# kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at:
https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose 使用 replication controller, service, deployment 或者 pod
并暴露它作为一个 新的 Kubernetes Service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
Basic Commands (Intermediate):
explain 查看资源的文档
get 显示一个或更多 resources
edit 在服务器上编辑一个资源
delete Delete resources by filenames, stdin, resources and names, or by
resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a Deployment, ReplicaSet or Replication
Controller
autoscale 自动调整一个 Deployment, ReplicaSet, 或者
ReplicationController 的副本数量
Cluster Management Commands:
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 and Debugging Commands:
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 Commands:
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 Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or
zsh)
Other Commands:
alpha Commands for features in alpha
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 的版本信息
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all
commands).
1.3.1、创建Pod
kubectl run 命令可以创建并运行一个或多个Pod,也可以创建一个 deployment 或job 来管理Pod。此命令和 docker run 相类似,也是实现Pod的创建,先从仓库中拉取基础镜像,然后对容器进行操作。kubectl run 的命令语法如下所示。
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]
各选项的作用分别如下所示。
- NAME:指定容器运行的名称;
- Image:指定运行的基础镜像;
- env:指定在容器中设置的环境参数;
- port:指定容器暴露的端口;
- replicas:指定启动容器设置的副本数;
- dry-run:dry-run 值如果为 true,则只打印要发送的对象,而不发送它;
- overrides:生成对象的内联 JSON 重写。如果非空,则用于覆盖生成的对象。要求对象提供有效的 apiVersion 字段。
通过 kubectl run 命令创建 Nginx 容器, 指定名称为 nginx-deployment,指定基础镜像为 Nginx 目前最新版本,指定对外暴露的端口为 80 以及副本数为 3。Nginx 容器创建完成后使用 get pod 命令查看 Pod 的信息,可以发现确实有 3 个容器资源,并且处于 Running状态。还可以查看 deployment,也显示的是 3。
[root@k8s-master ~]# kubectl create deployment nginx-deployment --image=nginx:1.14 --port=80 --replicas=3
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5567b746cd-7vq8r 1/1 Running 0 6m19s
nginx-deployment-5567b746cd-9h7qf 1/1 Running 0 9s
nginx-deployment-5567b746cd-nx65b 1/1 Running 0 6m19s
1.3.2、发布服务
容器资源创建完成,就需要完成发布工作,确保 Pod 能够对外提供服务,保证客户端能够正常访问,使用 kubectl expose 命令可以实现该目的。kubectl expose 的命令语法如下所示。
kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target -port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]
上述命令语法中,各选项的作用分别如下所示。
- -f:标识公开服务的资源的文件的文件名,目录或 URL;
- TYPE NAME:指定 deployment 名称;
- port:指定内部通信端口;
- protocol:指定网络协议,tcp 或者 udp;
- target-port:指定暴露在外部的端口;
- name:指定最新创建的对象的名称;
- external-ip:为 service 的外部 IP 地址;
- type:指定此服务的类型 ClusterIP,NodePort,LoadBalancer 或 ExternalName,默认 ClusterIP。
通过 kubectl expose 命令创建 Service,设置内部通信端口和外部暴露的端口均为 80、名称为 nginx-service、类型是 NodePort。创建 Service 完后,就可以通过 kubectl get svc命令查看到对外暴露的端口是30464,内部通信的地址是10.96.215.14。
[root@k8s-master ~]# kubectl expose deployment nginx-deployment --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
[root@k8s-master ~]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-5567b746cd-7vq8r 1/1 Running 0 9m29s
pod/nginx-deployment-5567b746cd-9h7qf 1/1 Running 0 3m19s
pod/nginx-deployment-5567b746cd-nx65b 1/1 Running 0 9m29s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12m
service/nginx-service NodePort 10.96.215.14 <none> 80:30464/TCP 28s
执行 kubectl get endpoints 查看容器自带的负载均衡。从执行结果可以得知容器自带的负载均衡分别是 10.244.1.2、10.244.1.3、10.244.1.4。
[root@k8s-master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.200.111:6443 16m
nginx-service 10.244.1.2:80,10.244.1.3:80,10.244.2.4:80 5m12s
执行 kubectl get pods 命令时加参数 -o wide 可以查看到 Node 真实节点上的分布。
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5567b746cd-7vq8r 1/1 Running 0 15m 10.244.1.2 k8s-node02 <none> <none>
nginx-deployment-5567b746cd-9h7qf 1/1 Running 0 9m3s 10.244.2.4 k8s-node01 <none> <none>
nginx-deployment-5567b746cd-nx65b 1/1 Running 0 15m 10.244.1.3 k8s-node02 <none> <none>
完成上述步骤就已经完成了发布,并且可以进行对其访问。
1.3.3、版本更新
一般来说,生产环境中的线上项目会随着时间的推进不断地进行更新、维护、版本升级、兼容老版本。而此时如果需要对 Nginx 更换版本,就需要滚动更新。
执行以下命令查看nginx版本
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5567b746cd-7vq8r 1/1 Running 0 16m
nginx-deployment-5567b746cd-9h7qf 1/1 Running 0 10m
nginx-deployment-5567b746cd-nx65b 1/1 Running 0 16m
[root@k8s-master ~]# kubectl exec nginx-deployment-5567b746cd-7vq8r -- nginx -v
nginx version: nginx/1.14.2
通过 set 选项将 Nginx 版本换成 1.19,再监使用-w 先处于监听状态进行听,更新速度快。如果不使用 Ctrl+c 中断监听,会一直持续。
[root@k8s-master ~]# kubectl get deploy,pods
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3/3 3 3 17m
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-5567b746cd-7vq8r 1/1 Running 0 17m
pod/nginx-deployment-5567b746cd-9h7qf 1/1 Running 0 11m
pod/nginx-deployment-5567b746cd-nx65b 1/1 Running 0 17m
[root@k8s-master ~]# kubectl set image deployment.apps/nginx-deployment nginx-deployment=nginx:1.19
deployment.apps/nginx-deployment image updated
[root@k8s-master ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-deployment-5567b746cd-7vq8r 1/1 Running 0 18m
nginx-deployment-5567b746cd-9h7qf 1/1 Running 0 12m
nginx-deployment-5567b746cd-nx65b 1/1 Running 0 18m
nginx-deployment-8675544c64-g4kkw 0/1 ContainerCreating 0 23s
nginx-deployment-8675544c64-g4kkw 1/1 Running 0 5m6s
nginx-deployment-5567b746cd-9h7qf 1/1 Terminating 0 17m
nginx-deployment-8675544c64-4zrg2 0/1 Pending 0 0s
nginx-deployment-8675544c64-4zrg2 0/1 Pending 0 0s
nginx-deployment-8675544c64-4zrg2 0/1 ContainerCreating 0 0s
nginx-deployment-5567b746cd-9h7qf 0/1 Terminating 0 17m
nginx-deployment-5567b746cd-9h7qf 0/1 Terminating 0 17m
nginx-deployment-5567b746cd-9h7qf 0/1 Terminating 0 17m
nginx-deployment-8675544c64-4zrg2 0/1 ErrImagePull 0 33s
nginx-deployment-8675544c64-4zrg2 0/1 ImagePullBackOff 0 46s
nginx-deployment-8675544c64-4zrg2 0/1 ErrImagePull 0 87s
nginx-deployment-8675544c64-4zrg2 0/1 ImagePullBackOff 0 101s
nginx-deployment-8675544c64-4zrg2 1/1 Running 0 3m18s
nginx-deployment-5567b746cd-7vq8r 1/1 Terminating 0 26m
nginx-deployment-8675544c64-r2ch7 0/1 Pending 0 0s
nginx-deployment-8675544c64-r2ch7 0/1 Pending 0 0s
nginx-deployment-8675544c64-r2ch7 0/1 ContainerCreating 0 0s
nginx-deployment-5567b746cd-7vq8r 0/1 Terminating 0 26m
nginx-deployment-5567b746cd-7vq8r 0/1 Terminating 0 27m
nginx-deployment-5567b746cd-7vq8r 0/1 Terminating 0 27m
nginx-deployment-8675544c64-r2ch7 1/1 Running 0 18s
nginx-deployment-5567b746cd-nx65b 1/1 Terminating 0 27m
nginx-deployment-5567b746cd-nx65b 0/1 Terminating 0 27m
nginx-deployment-5567b746cd-nx65b 0/1 Terminating 0 27m
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-8675544c64-4zrg2 1/1 Running 0 5m36s
nginx-deployment-8675544c64-g4kkw 1/1 Running 0 10m
nginx-deployment-8675544c64-r2ch7 1/1 Running 0 2m18s
[root@k8s-master ~]# kubectl exec nginx-deployment-8675544c64-4zrg2 -- nginx -v
nginx version: nginx/1.19.6
1.3.4、版本回滚
若想回滚到上一个版本可以使用 rollout 参数。
[root@k8s-master ~]# kubectl rollout --help //使用—help 查看 rollout 帮助信息
Manage the rollout of a resource.
Valid resource types include:
* deployments
* daemonsets
* statefulsets
Examples:
# Rollback to the previous deployment
kubectl rollout undo deployment/abc
# Check the rollout status of a daemonset
kubectl rollout status daemonset/foo
Available Commands:
history 显示 rollout 历史
pause 标记提供的 resource 为中止状态
restart Restart a resource
resume 继续一个停止的 resource
status 显示 rollout 的状态
undo 撤销上一次的 rollout
Usage:
kubectl rollout SUBCOMMAND [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
通过 histtory 查看历史信息
[root@k8s-master ~]# kubectl rollout history deployment.apps/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
通过 undo 执行回滚
[root@k8s-master ~]# kubectl rollout undo deployment.apps/nginx-deployment
deployment.apps/nginx-deployment rolled back
通过 status 检查回滚
[root@k8s-master ~]# kubectl rollout status deployment.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5567b746cd-5flfk 1/1 Running 0 65s
nginx-deployment-5567b746cd-g5tzd 1/1 Running 0 28s
nginx-deployment-5567b746cd-nsjnp 1/1 Running 0 37s
[root@k8s-master ~]# kubectl exec nginx-deployment-5567b746cd-5flfk -- nginx -v
nginx version: nginx/1.14.2
[root@k8s-master ~]# kubectl rollout undo deployment.apps/nginx-deployment --to-revision=1
1.3.5、删除资源
通过以上步骤,完成了创建、发布、更新、回滚操作,接下来进行删除操作。通过 kubectl get deploy 查看 deployment 信息,并且使用 kubectl delete 删除 deployment;通过 kubectl get svc 查看 sevice 信息,并且使用 kubectl delete 删除 sevice。
[root@k8s-master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 34m
[root@k8s-master ~]# kubectl delete deployment/nginx-deployment
deployment.apps "nginx-deployment" deleted
[root@k8s-master ~]# kubectl get deploy
No resources found in default namespace.
[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 38m
nginx-service NodePort 10.96.215.14 <none> 80:30464/TCP 26m
[root@k8s-master ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 38m
1.3.6、查看资源使用情况
通过 describe 可以查看较为详细的容器资源使用情况,包括一些错误信息都能检测出来。
[root@k8s-master ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dd6b5d745-bth2z 1/1 Running 0 53s
nginx-dd6b5d745-hv5h8 1/1 Running 0 53s
nginx-dd6b5d745-n6lx9 1/1 Running 0 53s
[root@k8s-master ~]# kubectl describe pod nginx-dd6b5d745-bth2z
Name: nginx-dd6b5d745-bth2z
Namespace: default
Priority: 0
Node: k8s-node02/192.168.200.113
Start Time: Sun, 20 Dec 2020 23:34:46 +0800
Labels: pod-template-hash=dd6b5d745
run=nginx
Annotations: <none>
Status: Running
IP: 10.244.1.8
IPs:
IP: 10.244.1.8
Controlled By: ReplicaSet/nginx-dd6b5d745
Containers:
nginx:
Container ID: docker://3bef1b0dc9b5d0ef7903e99c0d25489e363241681b7237a88e179957b1956a18
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Sun, 20 Dec 2020 23:35:04 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-4pbch (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-4pbch:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-4pbch
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 88s default-scheduler Successfully assigned default/nginx-dd6b5d745-bth2z to k8s-node02
Normal Pulling 87s kubelet, k8s-node02 Pulling image "nginx:latest"
Normal Pulled 70s kubelet, k8s-node02 Successfully pulled image "nginx:latest"
Normal Created 70s kubelet, k8s-node02 Created container nginx
Normal Started 70s kubelet, k8s-node02 Started container nginx
查看deployment资源
[root@k8s-master ~]# kubectl describe deployment/nginx
Name: nginx
Namespace: default
CreationTimestamp: Sun, 20 Dec 2020 23:34:46 +0800
Labels: run=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-dd6b5d745 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 113s deployment-controller Scaled up replica set nginx-dd6b5d745 to 3
1.3.7、执行容器命令
使用 exec 可以进入pod,完成相关操作
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dd6b5d745-bth2z 1/1 Running 0 2m39s
nginx-dd6b5d745-hv5h8 1/1 Running 0 2m39s
nginx-dd6b5d745-n6lx9 1/1 Running 0 2m39s
[root@k8s-master ~]# kubectl exec -it nginx-dd6b5d745-bth2z bash
root@nginx-dd6b5d745-bth2z:/# /etc/init.d/nginx status
[ ok ] nginx is running.
root@nginx-dd6b5d745-bth2z:/# nginx -v
nginx version: nginx/1.19.6
二、YAML资源清单
在 Kubernetes 中可以使用 YAML 格式的文件来创建符合预期期望的 Pod,这样的YAML 文件一般称之为资源清单。
2.1、YAML 语言
YAML 语言是一个可读性高,用来表达数据序列的语言格式。YAML 是"YAMLAin't a MarkupLanguage"(YAML 不是一种标记语言)的递归缩写。在开发这个语言时,YAML 的意思其实是:"YetAnother Markup Language"(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。
(1)基本语法
- 缩进时不允许使用 Tab 键,只允许使用空格;
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可;
- # 用于标识注释,从这个字符一直到行尾,都会被解释器忽略。
(2)支持的数据结构
- 对象:键值对的集合,又称为映射(mapping)/哈希(hashes)/ 字典(dictionary);
- 数组:一组按次序排列的值,又称为序列(sequence)/ 列表(list);
- 纯量(scalars):单个的、不可再分的值字符串。包括布尔值、整数、浮点数、Null、时间、日期。
2.2、通过资源清单管理容器资源
前面介绍了使用 kubectl 命令创建容器资源方法。基于这种命令方式创建容器资源,优点在于简单直观快捷、上手比较快,适合临时测试或实验。除了 kubectl 命令方式创建资源之外,还可以通过 YAML 配置文件来创建容器资源。
基于 YAML 配置文件创建容器资源的方式,优点在于配置文件提供了创建资源的模板,能够重复部署,可以像管理代码一样管理部署,适合正式的、跨环境的、规模化部署。
YAML 语法格式:
- 缩进标识层级关系;
- 不支持制表符(Tab键)缩进,使用空格缩进;
- 通常开头缩进两个空格;
- 字符后缩进一个空格,如冒号、逗号等;
- “---”表示 YAML 格式,一个文件的开始;
- “#”表示注释。
通过 kubectl api-versions 查看以 group/version 的格式显示服务器所支持的 API 版本。
[root@k8s-master ~]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
discovery.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
在创建 Deployment 资源清单之前先创建 demo 目录,用于存放资源清单的文件。在创建的 nginx-deployment.yaml 资源清单中,定义以下信息。
- kind:指定资源类型为 deployment;
- metadata:为元数据对象;
- name:指定资源 deployment 的名称;
- labels:为标签,定义标签选择器为 app: nginx,和 template 定义的遥相呼应,目的在于明确标识对象的属性。启动 Pod 的时候,通过 nodeSelector 指定要调度到的 node 节点的标签;
- spec:为详细定义对象;
- replicas:定义副本数为 3;
- matchLabels :用 于定 义一 组 Label , 与直 接写 在 Selector 中 作用 相同 。
- matchExpression 用于定义一组基于集合的筛选条件,可用的条件运算符包括:In、NotIn、Exists 和 DoesNotExist;
- template:为模板,这里是 Pod 的定义。定义标签选择器为 app: nginx,容器名称为 nginx,为镜像 1.19.6,对外暴露的端口为 80。
apiVersion: apps/v1 #指定API版本
kind: Deployment #指定资源类型
metadata: #指定属性
name: nginx-deployment #指定名称
labels: #定义标签
app: nginx
spec: #定义详细信息
replicas: 3 #指定副本数量
selector: #定义选择器信息
matchLabels:
app: nginx
template: #指定模板
metadata:
labels:
app: nginx
spec: #定义容器信息
containers:
- name: nginx
image: nginx:1.19.6
ports:
- containerPort: 80
[root@k8s-master ~]# mkdir demo
[root@k8s-master ~]# cd demo
[root@k8s-master demo]# vim nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19.6
ports:
- containerPort: 80
创建完 Deployment 的资源清单之后,使用 create 执行资源清单来创建容器。通过 get pods 可以查看到 Pod 容器资源已经自动创建完成。
[root@k8s-master demo]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@k8s-master demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dd6b5d745-bth2z 1/1 Running 0 19m
nginx-dd6b5d745-hv5h8 1/1 Running 0 19m
nginx-dd6b5d745-n6lx9 1/1 Running 0 19m
nginx-deployment-7c78f7c8bc-mlr4s 1/1 Running 0 18s
nginx-deployment-7c78f7c8bc-r94kf 1/1 Running 0 18s
nginx-deployment-7c78f7c8bc-tvpds 1/1 Running 0 18s
创建Service资源清单
在创建的 nginx-service 资源清单中,定义名称为 nginx-service 的 Service、标签选择器为 app: nginx、type 为 NodePort 指明外部流量可以访问内部容器。在 ports 中定义暴露的端口库号列表,对外暴露访问的端口是 80,容器内部的端口也是 80。
[root@k8s-master demo]# vim nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: nginx
[root@k8s-master demo]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@k8s-master demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 61m
nginx-service NodePort 10.96.198.171 <none> 80:31739/TCP 5s
访问测试
[root@k8s-master ~]# elinks --dump http://192.168.200.112:31739
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to [1]nginx.org.
Commercial support is available at [2]nginx.com.
Thank you for using nginx.
References
Visible links
1. http://nginx.org/
2. http://nginx.com/
[root@k8s-master ~]# elinks --dump http://192.168.200.113:31739
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to [1]nginx.org.
Commercial support is available at [2]nginx.com.
Thank you for using nginx.
References
Visible links
1. http://nginx.org/
2. http://nginx.com/
2.3、使用kubectl命令创建yaml模板
创建deployment模板
[root@k8s-master ~]# kubectl create deployment tomcat --image=tomcat:8 -o yaml --dry-run > tomcat-deployment.yaml
查看创建的模板
[root@k8s-master ~]# vim tomcat-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: tomcat
name: tomcat
spec:
replicas: 3
selector:
matchLabels:
app: tomcat
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: tomcat
spec:
containers:
- image: tomcat:8
name: tomcat
resources: {}
status: {}
创建tomcat容器资源
[root@k8s-master ~]# kubectl create -f tomcat-deployment.yaml
deployment.apps/tomcat created
查看创建状态
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat-7f89f4bb87-6swmt 1/1 Running 0 4m36s
tomcat-7f89f4bb87-rbzfb 1/1 Running 0 4m36s
tomcat-7f89f4bb87-rkmtr 1/1 Running 0 4m36s
创建service模板
[root@k8s-master ~]# kubectl expose deployment tomcat --port=8080 --target-port=8080 --type=NodePort -o yaml --dry-run >tomcat_service.yaml
[root@k8s-master ~]# vim tomcat_service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: tomcat
name: tomcat
spec:
ports:
- port: 8080 #映射到宿主机端口
protocol: TCP
targetPort: 8080 #容器端口
selector:
app: tomcat
type: NodePort
status:
loadBalancer: {}
创建service资源
[root@k8s-master ~]# kubectl create -f tomcat_service.yaml
service/tomcat created
查看创建状态
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9h
tomcat NodePort 10.96.7.194 <none> 8080:32707/TCP 8s