Kubernetes进阶 -- 编写资源清单(一)
k8s的资源清单采用标记性语言,写起来是有一定格式的,所以只要记住了格式,写起来是很easy的。
·
资源清单格式:
采用标记性语言.
格式如下:
• apiVersion: group/version : 指明api资源属于哪个群组和版本,同一个组可以有多个版本
[root@server2 manifest]# 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
。。。
v1 里面有很多,一般情况下,我们写v1 就行了
-
kind : 标记创建的资源类型,k8s主要支持以下资源类别
- Pod,ReplicaSet,Deployment,StatefulSet,DaemonSet,Job,Cronjob
-
metadata: 元数据
- name: 对像名称
- namespace: 对象属于哪个命名空间,默认是default
- labels: 指定资源标签,标签是一种键值数据
-
spec: 定义目标资源的期望状态
我们可以使用 kubectl explain 来查询帮助文档:
例如:
[root@server2 manifest]# kubectl explain pod
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object>
Most recently observed status of the pod. This data may not be up to date.
Populated by the system. Read-only. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
更具体的:
[root@server2 manifest]# kubectl explain pod.spec
KIND: Pod
VERSION: v1
RESOURCE: spec <Object>
DESCRIPTION:
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
PodSpec is a description of a pod.
FIELDS:
automountServiceAccountToken <boolean>
AutomountServiceAccountToken indicates whether a service account token
should be automatically mounted.
containers <[]Object> -required- # 加required的是必须要有的
List of containers belonging to the pod. Containers cannot currently be
added or removed. There must be at least one container in a Pod. Cannot be
updated.
-
资源清单中必须存在字段
-
k8s主要对象
- k8s 常用的额外参数
我们先清除之前的一些操作数据:
[root@server2 manifest]# kubectl delete svc myapp
service "myapp" deleted
[root@server2 manifest]# kubectl delete pod demo
pod "demo" deleted
[root@server2 manifest]# kubectl delete deployments.apps myapp
deployment.apps "myapp" deleted
[root@server2 ~]# cd manifest/ # 创建清单目录
[root@server2 manifest]# cd ..
[root@server2 manifest]#
创建自主式pod清单
[root@server2 manifest]# vim pod.yml
[root@server2 manifest]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
# namespace: default # 可以不写,默认指定default 命名空间
# labels: # 标签页可以不写
spec:
containers:
- name: myapp
image: myapp:v1
[root@server2 manifest]# kubectl create -f pod.yml
pod/myapp created
[root@server2 manifest]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 67s 10.244.1.31 server3 <none> <none>
[root@server2 manifest]# curl 10.244.1.31
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
最简单的一个pod资源清单就创建好了。
- 删除:
[root@server2 manifest]# kubectl delete -f pod.yml
pod "myapp" deleted
[root@server2 manifest]# kubectl get pod -o wide
No resources found in default namespace.
- 更新:
[root@server2 manifest]# vim pod.yml
spec:
containers:
- name: myapp
image: myapp:v2 //这里替换为v2
[root@server2 manifest]# kubectl create -f pod.yml
pod/myapp created
[root@server2 manifest]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 9s 10.244.1.32 server3 <none> <none>
[root@server2 manifest]# curl 10.244.1.32
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@server2 manifest]# kubectl delete -f pod.yml
pod "myapp" deleted
生产环境中不加控制器的pod 很少见
加控制器的清单
[root@server2 manifest]# vim pod2.yml
[root@server2 manifest]# cat pod2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
# Unique key of the Deployment instance
name: deployment-example
spec:
# 3 Pods should exist at all times.
replicas: 3 //加了控制器,三个副本
selector: //选择器,控制器选择指定的pod进行控管
matchLabels: //通过匹配标签控管
app: nginx
template: //模板,通过模板创建容器,因为我们有三个副本
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: nginx //创建一个标签
spec:
containers:
- name: nginx
# Run this image
image: myapp:v1
带控制器的清单我们建议用apply 运行:
[root@server2 manifest]# kubectl apply -f pod2.yml
get podeployment.apps/deployment-example created
[root@server2 manifest]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
deployment-example-7d5c95894c-8wmp6 1/1 Running 0 31s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-kjm48 1/1 Running 0 31s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-rwlmf 1/1 Running 0 31s app=nginx,pod-template-hash=7d5c95894c
- 拉伸
[root@server2 manifest]# vim pod2.yml
spec:
# 3 Pods should exist at all times.
replicas: 6 // 改为6
[root@server2 manifest]# kubectl apply -f pod2.yml
deployment.apps/deployment-example configured
[root@server2 manifest]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
deployment-example-7d5c95894c-7wmwl 0/1 ContainerCreating 0 1s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-88cqt 0/1 ContainerCreating 0 1s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-8wmp6 1/1 Running 0 86s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-kjm48 1/1 Running 0 86s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-q4vfw 0/1 ContainerCreating 0 1s app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-rwlmf 1/1 Running 0 86s app=nginx,pod-template-hash=7d5c95894c
- 滚动更新
[root@server2 manifest]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-example-7d5c95894c-7wmwl 1/1 Running 0 88s 10.244.1.37 server3 <none> <none>
...
[root@server2 manifest]# curl 10.244.1.37
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@server2 manifest]# vim pod2.yml
- name: nginx
# Run this image
image: myapp:v2 //镜像换成v2
[root@server2 manifest]# kubectl apply -f pod2.yml
deployment.apps/deployment-example configured
[root@server2 manifest]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-example-86774dbbb-27mvb 1/1 Running 0 41s 10.244.2.15 server4 <none> <none>
deployment-example-86774dbbb-5tpvf 1/1 Running 0 48s 10.244.2.12 server4 <none> <none>
deployment-example-86774dbbb-b65c2 1/1 Running 0 47s 10.244.2.13 server4 <none> <none>
deployment-example-86774dbbb-mx7kz 1/1 Running 0 41s 10.244.2.14 server4 <none> <none>
deployment-example-86774dbbb-pwwmm 1/1 Running 0 48s 10.244.1.38 server3 <none> <none>
deployment-example-86774dbbb-tjcd4 1/1 Running 0 43s 10.244.1.39 server3 <none> <none>
[root@server2 manifest]# curl 10.244.2.15
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
更多推荐
已为社区贡献3条内容
所有评论(0)