文章目录
前言
Kubernetes支持YAML和JSON格式创建资源对象
JSON格式用于接口之间消息的传递
YAML格式用于配置和管理
YAML是一种简洁的非标记性语言
语法格式:
缩进标识层级关系
不支持制表符(TAB键)缩进,使用空格缩进
通常开头缩进两个空格
字符后缩进一个空格,如冒号,逗号,短横杆等
“—”表示YAML格式,一个文件的开始
“#”表示注释
通过Yaml 文件,进行创建资源
查看版本名称
[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
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
创建 yaml 文件
[root@localhost demo]# vim nginx-deployment.yaml
apiVersion: apps/v1 //应用的名称及具体的版本号
kind: Deployment //类型
metadata: //控制器的元信息
name: nginx-deployment //内容
labels:
app: nginx 关联的哪一个pode
spec:
replicas: 3 副本集个数
selector:
matchLabels: 选择器的标签
app: nginx
template: pode容器的设定
metadata:
labels:
app: nginx 指向标签
spec:
containers: 容器参数
- name: nginx 名称
image: nginx:1.15.4 镜像
ports: 容器的端口
- containerPort: 80
创建资源
[root@master demo]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@master demo]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-7697996758-gzqms 1/1 Running 0 49m
nginx-7697996758-j6tfj 1/1 Running 0 49m
nginx-7697996758-ldfvx 1/1 Running 0 49m
nginx-deployment-d55b94fd-5zhjt 0/1 ContainerCreating 0 15s
nginx-deployment-d55b94fd-6f6hm 0/1 ContainerCreating 0 15s
nginx-deployment-d55b94fd-kr7c6 0/1 ContainerCreating 0 15s
nginx-deployment-d55b94fd-6f6hm 1/1 Running 0 24s
nginx-deployment-d55b94fd-kr7c6 1/1 Running 0 31s
nginx-deployment-d55b94fd-5zhjt 1/1 Running 0 39s
查看资源
[root@master demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-gzqms 1/1 Running 0 50m
nginx-7697996758-j6tfj 1/1 Running 0 50m
nginx-7697996758-ldfvx 1/1 Running 0 50m
nginx-deployment-d55b94fd-5zhjt 1/1 Running 0 71s
nginx-deployment-d55b94fd-6f6hm 1/1 Running 0 71s
nginx-deployment-d55b94fd-kr7c6 1/1 Running 0 71s
[root@master demo]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 57m
nginx-deployment 3 3 3 3 7m38s
通过 yaml 发布资源
创建Yaml文件
[root@localhost 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
Yaml创建service
[root@master demo]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@master demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 26h
nginx-service NodePort 10.0.0.67 <none> 80:38759/TCP 62s
[root@master demo]#
查看负载均衡
[root@node1 ~]# ipvsadm -L -n
访问节点
自动测试命令的正确性,并不执行创建
[root@master 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@master 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: {}
导出yaml 模板文件
[root@master demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml > nginx-deploy.yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
[root@master demo]# ls
nginx-deployment.yaml nginx-deploy.yaml nginx-service.yaml
生成 json 文件 格式 模板
[root@master demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json > nginx-deploy.json
[root@master demo]# vim nginx-deploy.json
{
"kind": "Deployment",
"apiVersion": "apps/v1beta1",
"metadata": {
{
"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": {}
}
]
}
},
将现有的资源生成模板导出到文件
[root@master demo]# kubectl get deploy/nginx --export -o yaml > my-deploy.yaml
[root@master demo]# ls
my-deploy.yaml nginx-deploy.json nginx-deploy.yaml
nginx-deploy.jason nginx-deployment.yaml nginx-service.yaml
[root@master demo]# vim my-deploy.yaml
[root@master demo]#
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@master demo]# kubectl explain pods.spec.containers
KIND: Pod
VERSION: v1
RESOURCE: containers <[]Object>
DESCRIPTION:
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.
..........................