前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Kubernetes 1.19.0——deployment(1)

Kubernetes 1.19.0——deployment(1)

原创
作者头像
gz_naldo
修改2020-09-27 17:44:32
7372
修改2020-09-27 17:44:32
举报
文章被收录于专栏:CloudComputing

Pod是不健壮的,随时都面临挂掉的风险,且本身是没有可再生性的

如果pod个数需要太多,创建起来会比较麻烦

deployment相当于一个机器人,控制器,用来创建pod

如果某个pod挂掉了,deployment会重新生成一个,保证环境里有3个pod
如果某个pod挂掉了,deployment会重新生成一个,保证环境里有3个pod

K8S 1.17.x 之前创建deploy的命令选项很多,但从1.18.x开始就变得少了

deployment创建出来的每个pod使用相同的一个模板

labels表示创建出来的每个pod具备的标签

通过修改yaml文件中replicas为3,自动为我们创建3个副本
通过修改yaml文件中replicas为3,自动为我们创建3个副本
代码语言:javascript
复制
[root@vms61 chap5-deploy]# cat web1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
    spec:
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}
status: {}
[root@vms61 chap5-deploy]# kubectl apply -f web1.yaml 
deployment.apps/web1 created
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-5bfb6d8dcc-7dvxm   1/1     Running   0          7s    10.244.116.47   vms63   <none>           <none>
web1-5bfb6d8dcc-gnvhf   1/1     Running   0          6s    10.244.116.46   vms63   <none>           <none>
web1-5bfb6d8dcc-qz77s   1/1     Running   0          6s    10.244.196.51   vms62   <none>           <none>
[root@vms61 chap5-deploy]# 
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-5bfb6d8dcc-7dvxm   1/1     Running   0          22m   10.244.116.47   vms63   <none>           <none>
web1-5bfb6d8dcc-gnvhf   1/1     Running   0          22m   10.244.116.46   vms63   <none>           <none>
web1-5bfb6d8dcc-qz77s   1/1     Running   0          22m   10.244.196.51   vms62   <none>           <none>
[root@vms61 chap5-deploy]# kubectl delete pod web1-5bfb6d8dcc-7dvxm --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "web1-5bfb6d8dcc-7dvxm" force deleted
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                    READY   STATUS              RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-5bfb6d8dcc-fg8js   0/1     ContainerCreating   0          2s    <none>          vms63   <none>           <none>
web1-5bfb6d8dcc-gnvhf   1/1     Running             0          22m   10.244.116.46   vms63   <none>           <none>
web1-5bfb6d8dcc-qz77s   1/1     Running             0          22m   10.244.196.51   vms62   <none>           <none>
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-5bfb6d8dcc-fg8js   1/1     Running   0          19s   10.244.116.48   vms63   <none>           <none>
web1-5bfb6d8dcc-gnvhf   1/1     Running   0          22m   10.244.116.46   vms63   <none>           <none>
web1-5bfb6d8dcc-qz77s   1/1     Running   0          22m   10.244.196.51   vms62   <none>           <none>

即便我们强行删除pod,deployment也会帮我们重新拉起

Web1这个deployment是通过标签为app=web1的标签来定位管理的是哪些pod
Web1这个deployment是通过标签为app=web1的标签来定位管理的是哪些pod
代码语言:javascript
复制
[root@vms61 chap5-deploy]# cat web1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
        app1: web1
        app2: web2
    spec:
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}
status: {}
[root@vms61 chap5-deploy]# kubectl apply -f web1.yaml 
deployment.apps/web1 created
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-bc6bbd46c-gkfn9   1/1     Running   0          4s    10.244.196.52   vms62   <none>           <none>
web1-bc6bbd46c-n74zn   1/1     Running   0          4s    10.244.116.49   vms63   <none>           <none>
web1-bc6bbd46c-p2cbv   1/1     Running   0          4s    10.244.116.50   vms63   <none>           <none>
[root@vms61 chap5-deploy]# kubectl get pods --show-labels 
NAME                   READY   STATUS    RESTARTS   AGE   LABELS
web1-bc6bbd46c-gkfn9   1/1     Running   0          38s   app1=web1,app2=web2,app=web1,pod-template-hash=bc6bbd46c
web1-bc6bbd46c-n74zn   1/1     Running   0          38s   app1=web1,app2=web2,app=web1,pod-template-hash=bc6bbd46c
web1-bc6bbd46c-p2cbv   1/1     Running   0          38s   app1=web1,app2=web2,app=web1,pod-template-hash=bc6bbd46c

上图中matchLabel里的标签一定要在template下的metada下的labels字段中匹配到,否则会报错

修改yaml使其挂载到pod中/xx目录并传递变量myenv至pod中
修改yaml使其挂载到pod中/xx目录并传递变量myenv至pod中
目录挂载以及变量传递均成功
目录挂载以及变量传递均成功
代码语言:javascript
复制
[root@vms61 chap5-deploy]# cat web1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
        app1: web1
        app2: web2
    spec:
      volumes:
      - name: v1
        emptyDir: {}
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        volumeMounts:
        - name: v1
          mountPath: /xx
        ports:
        - containerPort: 80
        env:
        - name: myenv1
          value: haha1
        - name: myenv2
          value: haha2
        resources: {}
status: {}
[root@vms61 chap5-deploy]# kubectl apply -f web1.yaml 
deployment.apps/web1 created
[root@vms61 chap5-deploy]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
web1-77cc489b4b-89pfh   1/1     Running   0          6s
web1-77cc489b4b-bl8p6   1/1     Running   0          6s
web1-77cc489b4b-q8lgb   1/1     Running   0          6s
[root@vms61 chap5-deploy]# kubectl exec -it web1-77cc489b4b-89pfh -- bash
root@web1-77cc489b4b-89pfh:/# ls /xx
root@web1-77cc489b4b-89pfh:/# echo $myenv1
haha1
root@web1-77cc489b4b-89pfh:/# echo $myenv2
haha2
root@web1-77cc489b4b-89pfh:/# exit
exit

如果想要10个副本,只需要告诉deployment即可(这里不作演示)

kubectl scale deployment nginx --replicas=10

或者

kubectl edit deployment nginx

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档