Kubernetes支持YAML和JSON格式创建资源对象
JSON格式用于接口之间消息的传递
YAML格式用于配置和管理
YAML是一种简洁的非标记性语言
语法格式:
缩进标识层级关系
不支持制表符缩进,使用空格缩进
通常开头缩进两个空格
字符后缩进一个空格,如冒号,逗号,短横杆等
“—”表示YAML格式,一个文件的开始
“#”表示注释
[root@master01 ~]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
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/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
[root@master01 ~]# mkdir demo
[root@master01 ~]# cd demo/
[root@master01 demo]# vim nginx-deployment.yaml #创建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.15.4
ports:
- containerPort: 80
[root@master01 demo]# kubectl create --help
Create a resource from a file or from stdin.
JSON and YAML formats are accepted.
Examples:
# Create a pod using the data in pod.json.
kubectl create -f ./pod.json
# Create a pod based on the JSON passed into stdin.
cat pod.json | kubectl create -f -
# Edit the data in docker-registry.yaml in JSON then create the resource using
the edited data.
kubectl create -f docker-registry.yaml --edit -o json
Available Commands:
clusterrole Create a ClusterRole.
clusterrolebinding 为一个指定的 ClusterRole 创建一个
ClusterRoleBinding
configmap 从本地 file, directory 或者 literal value
创建一个 configmap
deployment 创建一个指定名称的 deployment.
job Create a job with the specified name.
namespace 创建一个指定名称的 namespace
poddisruptionbudget 创建一个指定名称的 pod disruption budget.
priorityclass Create a priorityclass with the specified name.
quota 创建一个指定名称的 quota.
role Create a role with single rule.
rolebinding 为一个指定的 Role 或者 ClusterRole创建一个
RoleBinding
secret 使用指定的 subcommand 创建一个 secret
service 使用指定的 subcommand 创建一个 service.
serviceaccount 创建一个指定名称的 service account
Options:
--allow-missing-template-keys=true: If true, ignore any errors in
templates when a field or map key is missing in the template. Only applies to
golang and jsonpath output formats.
--dry-run=false: If true, only print the object that would be sent,
without sending it.
--edit=false: Edit the API resource before creating
-f, --filename=[]: Filename, directory, or URL to files to use to create the
resource
-o, --output='': Output format. One of:
json|yaml|name|template|go-template|go-template-file|templatefile|jsonpath|jsonpath-file.
--raw='': Raw URI to POST to the server. Uses the transport specified by
the kubeconfig file.
--record=false: Record current kubectl command in the resource annotation.
If set to false, do not record the command. If set to true, record the command.
If not set, default to updating the existing annotation value only if one
already exists.
-R, --recursive=false: Process the directory used in -f, --filename
recursively. Useful when you want to manage related manifests organized within
the same directory.
--save-config=false: If true, the configuration of current object will be
saved in its annotation. Otherwise, the annotation will be unchanged. This flag
is useful when you want to perform kubectl apply on this object in the future.
-l, --selector='': Selector (label query) to filter on, supports '=', '==',
and '!='.(e.g. -l key1=value1,key2=value2)
--template='': Template string or path to template file to use when
-o=go-template, -o=go-template-file. The template format is golang templates
[http://golang.org/pkg/text/template/#pkg-overview].
--validate=true: If true, use a schema to validate the input before
sending it
--windows-line-endings=false: Only relevant if --edit=true. Defaults to
the line ending native to your platform.
Usage:
kubectl create -f FILENAME [options]
Use "kubectl --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all
commands).
创建
[root@master01 demo]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@master01 demo]# kubectl get pods -w
nginx-deployment-d55b94fd-k7p8h 0/1 ContainerCreating 0 4s
nginx-deployment-d55b94fd-lsz84 0/1 ContainerCreating 0 4s
nginx-deployment-d55b94fd-wnlqm 0/1 ContainerCreating 0 4s
nginx-deployment-d55b94fd-k7p8h 1/1 Running 0 15s
nginx-deployment-d55b94fd-wnlqm 1/1 Running 0 18s
nginx-deployment-d55b94fd-lsz84 1/1 Running 0 34s
[root@master01 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@master01 demo]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@master01 demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 4d7h
nginx-service NodePort 10.0.0.219 80:31003/TCP 26s
[root@master01 demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-deployment created (dry run)
查看生成yaml格式
[root@master01 demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
apiVersion: apps/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: nginx-deployment
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
run: nginx-deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: nginx-deployment
spec:
containers:
- image: nginx
name: nginx-deployment
ports:
- containerPort: 80
resources: {}
status: {}
查看生成json格式
[root@master01 demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
{
"kind": "Deployment",
"apiVersion": "apps/v1beta1",
"metadata": {
"name": "nginx-deployment",
"creationTimestamp": null,
"labels": {
"run": "nginx-deployment"
}
},
"spec": {
"replicas": 3,
"selector": {
"matchLabels": {
"run": "nginx-deployment"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "nginx-deployment"
}
},
"spec": {
"containers": [
{
"name": "nginx-deployment",
"image": "nginx",
"ports": [
{
"containerPort": 80
}
],
"resources": {}
}
]
}
},
"strategy": {}
},
"status": {}
}
生成yaml格式并导出
[root@master01 demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml > my-deployment.yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
[root@master01 demo]# ls
my-deployment.yaml nginx-deployment.yaml nginx-service.yaml
[root@master01 demo]# vim my-deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: nginx-deployment
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
run: nginx-deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: nginx-deployment
spec:
containers:
- image: nginx
name: nginx-deployment
ports:
- containerPort: 80
resources: {}
status: {}
[root@master01 demo]# kubectl get deploy/nginx --export -o yaml #将现有的资源生成模板输出展示
Error from server (NotFound): deployments.extensions "nginx" not found
[root@master01 demo]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-d55b94fd-84x8c 1/1 Running 0 10m
pod/nginx-deployment-d55b94fd-xsh5q 1/1 Running 0 10m
pod/nginx-deployment-d55b94fd-zsb6p 1/1 Running 0 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 443/TCP 4d7h
service/nginx-service NodePort 10.0.0.219 80:31003/TCP 8m53s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3 3 3 3 10m
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-d55b94fd 3 3 3 10m
[root@master01 demo]# kubectl get deploy/nginx-deployment --export -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: null
generation: 1
labels:
app: nginx
name: nginx-deployment
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx-deployment
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.4
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status: {}
[root@master01 demo]# kubectl get deploy/nginx --export -o yaml > my-deploy.yaml
[root@master01 demo]# ls
my-deployment.yaml my-deploy.yaml nginx-deployment.yaml nginx-service.yaml
我们在写yaml文件时有一些不懂得可以通过下面的命令查看帮助
[root@master01 demo]# kubectl explain pods # 查看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
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/api-conventions.md#resources
kind
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/api-conventions.md#types-kinds
metadata
port
port是k8s集群内部访问service的端口,即通过clusterIP: port可以访问到某个service
nodePort
nodePort是外部访问k8s集群中service的端口,通过nodeIP: nodePort可以从外部访问到某个service。
targetPort
targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。
containerPort
containerPort是pod内部容器的端口,targetPort映射到containerPort。