Kubernetes创建资源的方式

命令创建

17版本可以使用命令直接创建,如:

1
kubectl run httpd-app --image=httpd --replicas=2

会提示:
kubectl run –generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run –generator=run-pod/v1 or kubectl create instead.

18版本之后可以先创建后伸缩

1
2
kubectl create deployment --image=nginx:1.7.9 nginx-deployment
kubectl scale deployment nginx-deployment --replicas=2

yaml/yml文件创建

1
kubectl apply -f xxx.yml

使用Deployment运行应用

k8s作为容器编排引擎,最重要也是最基本的功能当然是运行容器话应用。
Kubernetes通过各种Controller来管理Pod的生命周期,为了满足不同的业务场景,k8s开发了Deployment、ReplicaSet、DaemonSet、StatefuleSet、Job等多种Controller。
Deployment是k8s应用最广的资源
Deployment为Pod和ReplicaSet提供描述性的更新方式,描述想要的目标状态,以达到期望值
由于1.18版本将命令的选项–replicas弃用,所以直接使用先创建后伸缩的方法来创建

创建pod

1
2
3
4
kubectl create deployment --image=nginx:1.7.9 nginx-deployment
deployment.apps/nginx-deployment created
kubectl scale deployment nginx-deployment --replicas=2
deployment.apps/nginx-deployment scaled

查看部署结果

1
2
3
4
5
6
7
8
9
# 查看 Deployment
kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 109s
# 查看 Pod
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-deployment-6dd8bc586b-h4262 1/1 Running 0 42m 10.244.1.5 node3
nginx-deployment-6dd8bc586b-wl4bq 1/1 Running 0 42m 10.244.2.3 node2

可以看到一个名为nginx-deployment的Deployment和两个名为nginx-deployment-xxxxxxx的副本Pod
查看到的这两个地址是可以直接访问的,访问不到主机和路由的,建议去查看是否开启了路由转发。

分析创建Deployment过程中,k8s是怎么运行的

查看Deployment的详细信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@node1 ~]# kubectl describe deployments.apps nginx-deployment 
Name: nginx-deployment # Deployment名字
Namespace: default # 所在命名空间
CreationTimestamp: Thu, 07 May 2020 19:06:18 +0800 # 创建时间
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
# 目标的期望状态,期望2,更新2,总共2,在使用2,坏掉的0
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template: # Pod运行状态
Labels: app=nginx
Containers: # 容器
nginx:
Image: nginx:1.7.9 # 容器使用镜像
Port: <none> # 容器端口
Host Port: <none> # 容器端口在主机上的映射端口
Environment: <none> # 设置过的变量值
Mounts: <none> # 挂载
Volumes: <none> # 映射卷
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-6dd8bc586b (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 14m deployment-controller Scaled up replica set nginx-deployment-6dd8bc586b to 1
Normal ScalingReplicaSet 14m deployment-controller Scaled up replica set nginx-deployment-6dd8bc586b to 2

查看ReplicaSet的详细信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@node1 ~]# kubectl describe replicaset
Name: nginx-deployment-6dd8bc586b
Namespace: default
Selector: app=nginx,pod-template-hash=6dd8bc586b
Labels: app=nginx
pod-template-hash=6dd8bc586b
Annotations: deployment.kubernetes.io/desired-replicas: 2
deployment.kubernetes.io/max-replicas: 3
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/nginx-deployment # 记录了ReplicaSet是由Deployment(nginx-deployment)创建的
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
pod-template-hash=6dd8bc586b
Containers:
nginx:
Image: nginx:1.7.9
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 16m replicaset-controller Created pod: nginx-deployment-6dd8bc586b-h4262
Normal SuccessfulCreate 15m replicaset-controller Created pod: nginx-deployment-6dd8bc586b-wl4bq

查看Pod的详细信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@node1 ~]# kubectl describe pod nginx-deployment-6dd8bc586b-wl4bq 
Name: nginx-deployment-6dd8bc586b-wl4bq
Namespace: default
Priority: 0
Node: node2/192.168.1.12
Start Time: Thu, 07 May 2020 19:06:18 +0800
Labels: app=nginx
pod-template-hash=6dd8bc586b
Annotations: <none>
Status: Running
IP: 10.244.2.3
IPs:
IP: 10.244.2.3
Controlled By: ReplicaSet/nginx-deployment-6dd8bc586b # 该Pod由ReplicaSet创建
Containers:
nginx:
Container ID: docker://fcfa9364fd4f67dfd722fc25dbe734909025fff8a1f1502476503489a109dbfd
Image: nginx:1.7.9
Image ID: docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 07 May 2020 19:06:55 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-cdzqz (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-cdzqz:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-cdzqz
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 17m default-scheduler Successfully assigned default/nginx-deployment-788b8d7b98-6cpwt to node2
Normal Pulled 17m kubelet, node2 Container image "nginx:1.7.9" already present on machine
Normal Created 17m kubelet, node2 Created container nginx
Normal Started 17m kubelet, node2 Started container nginx

Pod创建过程:

1.用户通过kubectl创建Deployment
2.Deployment创建ReplicaSet
3.ReplicaSet创建Pod
4.Pod来启动容器

最终容器名字的组成:k8s_标签名_Deployment名-ReplicaSet名-Pod名_所在命名空间名_容器id
Container:k8s_nginx_nginx-deployment-6dd8bc586b-wl4bq_default_1a84d0c3-829e-4c1c-9ec7-7e0395ce3e81_0