1.Kubernetes支持YAML和JSON格式创建资源对象
2.JSON格式用于接口之间消息的传递
3.YAML格式用于配置和管理
4.YAML是一种简洁的非标记性语言
5.语法格式:
缩进标识层级关系
不支持制表符缩进,使用空格缩进
通常开头缩进两个空格
字符后缩进一个空格,如冒号,逗号等
“—”表示YAML格式,一个文件的开始
“#”表示注释
[root@localhost k8s]# 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@localhost ~]# mkdir demo
[root@localhost ~]# cd demo/
[root@localhost demo]# vim nginx-deployment.yaml
apiVersion: apps/v1 //api版本
kind: Deployment //指定类型,deployment表示控制器
metadata: //定义资源属性
name: nginx-deployment //资源名称
labels: //资源标签
app: nginx
spec: //资源内容
replicas: 3 //副本
selector: //选择器
matchLabels: //匹配标签
app: nginx //匹配模板名称
template: //从这开始往下,是pod的资源管理
metadata:
labels:
app: nginx
spec: //定义容器模板
containers: //信息
- name: nginx //容器名
image: nginx:1.15.4 //容器使用镜像和版本
ports:
- containerPort: 80 //对外端口
[root@localhost demo]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@localhost demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-ksqr7 1/1 Running 0 40m
nginx-7697996758-s52gb 1/1 Running 0 40m
nginx-7697996758-v9qzw 1/1 Running 0 40m
nginx-deployment-d55b94fd-99qxf 1/1 Running 0 58s
nginx-deployment-d55b94fd-bz76x 1/1 Running 0 58s
nginx-deployment-d55b94fd-tk27h 1/1 Running 0 58s
[root@localhost demo]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 43m
nginx-deployment 3 3 3 3 4m12s
//下三个是刚创建的
[root@localhost demo]# vim nginx-service.yaml
apiVersion: v1 //版本
kind: Service //类型
metadata: //元数据
name: nginx-service //service名称
labels: //标签属性
app: nginx
spec: //详情
type: NodePort //service的类型默认为ClusterIP
ports: //端口
- port: 80 //内部端口
targetPort: 80 //需转发的端口
selector: //选择器
app: nginx //pod资源名称
[root@localhost demo]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@localhost demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 4d22h
nginx-service NodePort 10.0.0.83 80:47336/TCP 30s
//访问查看
[root@localhost 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) //此时,此命令可被执行
[root@localhost 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: {}
[root@localhost 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": {}
}
[root@localhost 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@localhost demo]# ls
my-deployment.yaml nginx-deployment.yaml nginx-service.yaml
[root@localhost 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@localhost demo]# kubectl get deploy/nginx
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 68m
[root@localhost demo]# kubectl get deploy/nginx --export -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: null
generation: 1
labels:
run: nginx
name: nginx
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 2
selector:
matchLabels:
run: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
run: nginx
spec:
containers:
- image: nginx:latest
imagePullPolicy: Always
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@localhost demo]# kubectl get deploy/nginx --export -o yaml > my-deploy.yaml
[root@localhost demo]# ls
my-deployment.yaml my-deploy.yaml nginx-deployment.yaml nginx-service.yaml
//查看字段帮助信息
[root@localhost demo]# kubectl explain pods.spec.containers