• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

⑥.kubernetes 创建资源的方式

开发技术 开发技术 4小时前 2次浏览

Kubernetes 支持两种方式创建资源:

(1)用kubectl命令直接创建,在命令行中通过参数指定资源的属性。此方式简单直观,比较适合临时测试或实验使用。

kubectl run net-test --image=alpine --replicas=2 sleep 36000

(2)通过配置文件和kubectl create创建。在配置文件中描述了应用的信息和需要达到的预期状态。

kubectl create -f nginx-deployment.yaml

声明式创建资源 通过yaml清单

1.准备配置清单
[root@linux-node1 ~]# vim nginx-deployment.yaml  #使用yaml的方式进行创建应用
apiVersion: apps/v1  #apiVersion是当前配置格式的版本
kind: Deployment    #kind是要创建的资源类型,这里是Deploymnet
metadata:        #metadata是该资源的元数据,name是必须的元数据项
  name: nginx-deployment
  labels:
    app: nginx
spec:          #spec部分是该Deployment的规则说明
  replicas: 3      #relicas指定副本数量,默认为1
  selector:
    matchLabels:
      app: nginx
  template:      #template定义Pod的模板,这是配置的重要部分
    metadata:      #metadata定义Pod的元数据,至少要顶一个label,label的key和value可以任意指定
      labels:
        app: nginx
    spec:       #spec描述的是Pod的规则,此部分定义pod中每一个容器的属性,name和image是必需的
      containers:
      - name: nginx
        image:  harbor.rongbiz.cn/public/nginx:alpine
        ports:
        - containerPort: 80

[root@linux-node1 ~]# kubectl create -f nginx-deployment.yaml #创建nginx-deployment应用
deployment.apps "nginx-deployment" created
2.查看deployment
[root@rstx-203 ~]# kubectl get deployment 
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
net-test           0/2     2            0           159m
nginx-deployment   3/3     1            3           18h
[root@rstx-203 ~]# kubectl describe deployment nginx-deployment 
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Mon, 19 Jul 2021 19:24:50 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 3
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx-deployment","namespace":"d...
                        kubernetes.io/change-cause: kubectl set image deployment/nginx-deployment nginx=harbor.rongbiz.cn/public/nginx:v1.7.2 --record=true
Selector:               app=nginx
Replicas:               3 desired | 1 updated | 4 total | 3 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        harbor.rongbiz.cn/public/nginx:v1.7.2
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    False   ProgressDeadlineExceeded
OldReplicaSets:  nginx-deployment-d75f79f4d (3/3 replicas created)
NewReplicaSet:   nginx-deployment-5455f758b9 (1/1 replicas created)
Events:          <none>
3.查看pod
[root@rstx-203 ~]# kubectl get pod -o wide  #查看pod的状态信息  及所在的node节点
NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE                 NOMINATED NODE   READINESS GATES
nginx-deployment-867854d496-rk74x   1/1     Running   0          94s   172.7.241.2   rstx4-241.host.com   <none>           <none>
nginx-deployment-867854d496-rrn5g   1/1     Running   0          89s   172.7.205.2   rstx4-205.host.com   <none>           <none>
nginx-deployment-867854d496-zc2lm   1/1     Running   0          91s   172.7.203.2   rstx4-203.host.com   <none>           <none>
[root@rstx-203 ~]# kubectl describe pod nginx-deployment-867854d496-rk74x #查看pod的详细信息
Name:           nginx-deployment-867854d496-rk74x
Namespace:      default
Priority:       0
Node:           rstx4-241.host.com/192.168.1.241
Start Time:     Tue, 20 Jul 2021 13:49:25 +0800
Labels:         app=nginx
                pod-template-hash=867854d496
Annotations:    <none>
Status:         Running
IP:             172.7.241.2
Controlled By:  ReplicaSet/nginx-deployment-867854d496             #由那个rs控制的
Containers:
  nginx:
    Container ID:   docker://65599828e6deac13f2c5ed02bcf6eac4cfae8c59b48103bd5b6572573a8293dd
    Image:          harbor.rongbiz.cn/public/nginx:v1.7.9
    Image ID:       docker-pullable://harbor.rongbiz.cn/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 20 Jul 2021 13:49:27 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-clf9q (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-clf9q:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-clf9q
    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  Pulled     5m31s  kubelet, rstx4-241.host.com  Container image "harbor.rongbiz.cn/public/nginx:v1.7.9" already present on machine
  Normal  Created    5m31s  kubelet, rstx4-241.host.com  Created container nginx
  Normal  Started    5m30s  kubelet, rstx4-241.host.com  Started container nginx
  Normal  Scheduled  104s   default-scheduler            Successfully assigned default/nginx-deployment-867854d496-rk74x to rstx4-241.host.com
4.更新版本

更换

[root@rstx-203 ~]# kubectl set image deployment/nginx-deployment  nginx=harbor.rongbiz.cn/public/nginx:1.20 --record #nginx= 此信息是pod中更换镜像容器的名字   记录这条命令加参数 --record
deployment.extensions/nginx-deployment image updated
[root@rstx-203 ~]# kubectl get deployment nginx-deployment 
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     2            3           10m
[root@rstx-203 ~]# kubectl get deployment nginx-deployment -o wide
NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                SELECTOR
nginx-deployment   3/3     3            3           10m   nginx        harbor.rongbiz.cn/public/nginx:1.20   app=nginx

查看历史

[root@rstx-203 ~]# kubectl rollout history deployment/nginx-deployment 
deployment.extensions/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
3         <none>
4         <none>
5         kubectl set image deployment/nginx-deployment nginx=harbor.rongbiz.cn/public/nginx:1.20 --record=true
[root@rstx-203 ~]# kubectl rollout history deployment/nginx-deployment --revision=5
deployment.extensions/nginx-deployment with revision #5
Pod Template:
  Labels:	app=nginx
	pod-template-hash=7d79b7b698
  Annotations:	kubernetes.io/change-cause: kubectl set image deployment/nginx-deployment nginx=harbor.rongbiz.cn/public/nginx:1.20 --record=true
  Containers:
   nginx:
    Image:	harbor.rongbiz.cn/public/nginx:1.20
    Port:	80/TCP
    Host Port:	0/TCP
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

快速回滚

[root@rstx-203 ~]# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment rolled back
[root@rstx-203 ~]# kubectl get deployment nginx-deployment -o wide
NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                  SELECTOR
nginx-deployment   3/3     2            3           26m   nginx        harbor.rongbiz.cn/public/nginx:v1.7.9   app=nginx
[root@rstx-203 ~]# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment rolled back
[root@rstx-203 ~]# kubectl get deployment nginx-deployment -o wide
NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                SELECTOR
nginx-deployment   3/3     1            3           26m   nginx        harbor.rongbiz.cn/public/nginx:1.20   app=nginx
5.快速扩容
[root@rstx-203 ~]# kubectl scale deployment nginx-deployment --replicas 5
deployment.extensions/nginx-deployment scaled
[root@rstx-203 ~]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/5     5            3           28m
[root@rstx-203 ~]# kubectl get pod
NAME                                READY   STATUS              RESTARTS   AGE
nginx-deployment-7d79b7b698-9pjbx   0/1     ContainerCreating   0          10s
nginx-deployment-7d79b7b698-jh6xt   1/1     Running             0          10s
nginx-deployment-7d79b7b698-pvkcn   1/1     Running             0          2m23s
nginx-deployment-7d79b7b698-t4gd4   1/1     Running             0          2m28s
nginx-deployment-7d79b7b698-vvnl8   1/1     Running             0          2m20s

程序员灯塔
转载请注明原文链接:⑥.kubernetes 创建资源的方式
喜欢 (0)