概述
k8s支持使用YAML和JSON格式的文件来创建资源对象,相比较而言:
- json格式的文件用于接口之间消息的传递,更适合二次开发
- yaml格式的文件只是一种简洁的非标记性语言,更适合运维
YAML的文件格式和注意事项
- 不支持制表符tab键缩进,需要使用空格缩进,使用缩进表示层级关系
- 通常开头缩进2个空格,缩进的空格数不重要,只要相同层级的元素左对齐即可
- 字符后缩进一个空格,如冒号、逗号、横杆
- 用#号注释
- 如果包含特殊字符用单引号引起来
- 布尔值必须用引号括起来
- —表示yaml文件格式的分割
使用YAML文件创建资源对象
1、查看资源版本标签
[root@master ~]# 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
kuboard.cn/v1
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
2、创建目录,编辑测试文件
[root@master test]# vim nginx-deployment.yaml
apiVersion: apps/v1 '//指定api版本标签'
kind: Deployment '//定义资源的类型/角色,deployment为控制器'
metadata: '//定义资源的元数据'
name: nginx-deployment '//定义资源的名称,在同一个namespace中必须唯一'
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 '//定义容器的对外端口'
3、创建资源对象
[root@master test]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@master test]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-d55b94fd-7rp7w 1/1 Running 0 55s
nginx-deployment-d55b94fd-lzdc8 1/1 Running 0 55s
nginx-deployment-d55b94fd-x2xjq 1/1 Running 0 55s
4、创建service服务对外提供访问并测试
[root@master test]# 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@master test]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@master test]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 13d
nginx-service NodePort 10.0.0.206 <none> 80:32307/TCP 3m41s
自动测试命令的正确性,并不执行创建
[root@master test]# 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@master test]# 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@master test]# 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@master test]# 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@master test]# vim my-deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
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
将现有的资源生成模板导出
[root@master test]# 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@master test]# kubectl get deploy/nginx-deployment --export -o yaml >my-deployment.yaml
查看字段帮助信息
[root@master test]# kubectl explain pods.spec.containers
详解k8s中的port
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。