Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.
It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon 15 years of experience of running production workloads at Google, combined with best-of-breed ideas and practices from the community.
https://www.pianshen.com/article/8970872160/
Kubernetes支持YAML和JSON格式创建资源对象
1、JSON格式用于接口之间消息的传递
2、YAML格式用于配置和管理
YAML是一种简洁的非标记性语言
语法格式:
缩进标识层级关系
不支持制表符缩进,使用空格缩进
通常开头缩进两个空格
字符后缩进一个空格,如冒号,逗号等
“---”表示YAML格式,一个文件的开始
“#”表示注释
查看具体资源的详细信息
1、创建pod
[root@localhost ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created
2、查看pod,状态为ContainerCreating
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-j5cwn 0/1 ContainerCreating 0 17s
nginx-7697996758-nw2v4 0/1 ContainerCreating 0 17s
nginx-7697996758-sxkp9 0/1 ContainerCreating 0 17s
3、处于动态监听状态
[root@localhost ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-7697996758-j5cwn 1/1 Running 0 39s
nginx-7697996758-nw2v4 1/1 Running 0 39s
nginx-7697996758-sxkp9 1/1 Running 0 39s
4、再查看,状态为Running
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-j5cwn 1/1 Running 0 79s
nginx-7697996758-nw2v4 1/1 Running 0 79s
nginx-7697996758-sxkp9 1/1 Running 0 79s
5、查看镜像资源
[root@localhost ~]# kubectl describe pod nginx-7697996758-j5cwn
Name: nginx-7697996758-j5cwn
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: 192.168.35.102/192.168.35.102
Start Time: Wed, 12 Feb 2020 00:07:58 +0800
Labels: pod-template-hash=7697996758
run=nginx
Annotations: <none>
Status: Running
IP: 172.17.68.2
Controlled By: ReplicaSet/nginx-7697996758
Containers:
nginx:
Container ID: docker://a55be62040ad1bc1667d48a5b4adab626af857bcbb3f4985591381cf19b1f3ff
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 12 Feb 2020 00:08:14 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-r5ql5 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-r5ql5:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-r5ql5
6、查看deployment资源
[root@localhost ~]# kubectl describe deployment/nginx
Name: nginx
Namespace: default
CreationTimestamp: Wed, 12 Feb 2020 00:07:57 +0800
Labels: run=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-7697996758 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 7m1s deployment-controller Scaled up replica set nginx-7697996758 to 3
用YAML格式创建资源对象
1、首先删除之前所创建的资源
[root@localhost ~]# kubectl get deployment 查看deployment资源
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 3d17h
[root@localhost ~]# kubectl delete deployment/nginx #删除镜像资源
deployment.extensions "nginx" deleted
[root@localhost ~]# kubectl get deployment 再次查看deployment资源,确保删除成功
No resources found.[root@localhost ~]# kubectl get svc #查看是否有服务运行,有的话删除
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 7d3h
2、查看版本名称
[root@localhost ~]# 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
3、编写yaml文件进行资源创建
[root@localhost ~]# mkdir demo
[root@localhost ~]# cd demo/
[root@localhost demo]# vim nginx-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
4、查看 kubectl create命令语法
[root@localhost demo]# kubectl create --help
Usage:
kubectl create -f FILENAME [options]
5、创建nginx资源
[root@localhost demo]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
6、查看pod资源状态
[root@localhost demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-d55b94fd-7998c 0/1 ContainerCreating 0 14s
nginx-deployment-d55b94fd-79k65 0/1 ContainerCreating 0 14s
nginx-deployment-d55b94fd-pvjgd 0/1 ContainerCreating 0 14s
[root@localhost demo]# kubectl get pods -w #处于动态监听状态
NAME READY STATUS RESTARTS AGE
nginx-deployment-d55b94fd-7998c 0/1 ContainerCreating 0 29s
nginx-deployment-d55b94fd-79k65 0/1 ContainerCreating 0 29s
nginx-deployment-d55b94fd-pvjgd 0/1 ContainerCreating 0 29s
nginx-deployment-d55b94fd-79k65 1/1 Running 0 36s
nginx-deployment-d55b94fd-pvjgd 1/1 Running 0 40s
nginx-deployment-d55b94fd-7998c 1/1 Running 0 51s
^C[root@localhost demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-d55b94fd-7998c 1/1 Running 0 71s
nginx-deployment-d55b94fd-79k65 1/1 Running 0 71s
nginx-deployment-d55b94fd-pvjgd 1/1 Running 0 71s[root@localhost demo]# kubectl get deploy #查看资源
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 3 3 3 3 7m54s
7、编写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
8、发布并查看
[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 <none> 443/TCP 7d5h
nginx-service NodePort 10.0.0.134 <none> 80:32509/TCP 5s
9、在node节点查看
node1:
[root@localhost ~]# ipvsadm -L -n
TCP 192.168.35.101:32509 rr
-> 172.17.45.2:80 Masq 1 0 0
-> 172.17.45.4:80 Masq 1 0 0
-> 172.17.68.2:80 Masq 1 0 0
node2:
[root@localhost ~]# ipvsadm -L -n
TCP 192.168.35.102:32509 rr
-> 172.17.45.2:80 Masq 1 0 0
-> 172.17.45.4:80 Masq 1 0 0
-> 172.17.68.2:80 Masq 1 0 0
10、在浏览器上可以进行访问
11、自动测试命令的正确性,并不执行创建
[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)
12、查看生成yaml格式
[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: {}
13、查看生成json格式
[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": {}
}
14、因为写的语法比较严格,可以把现成的模板重定向保存出来一份,修修改改就可以了。
[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: {} #删掉
15、将现有的资源生成模板导出
[root@localhost demo]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 3 3 3 3 160m
[root@localhost 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: {}
15、保存到文件中
[root@localhost demo]# kubectl get deploy/nginx-deployment --export -o yaml > my-deploy.yaml
[root@localhost demo]# ls
my-deployment.yaml my-deploy.yaml nginx-deployment.yaml nginx-service.yaml
16、查看字段帮助信息
[root@localhost demo]# kubectl explain pods.spec.containers