跳转至

5. deployment资源

1. 创建deployment目录

cd /k8s
mkdir deploy
cd deploy

2. 创建deployment的yaml文件

vim nginx-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: 192.168.178.151:5000/nginx:1.13
        ports:
        - containerPort: 80

3. 创建deployment资源

kubectl create -f nginx-deploy.yaml

4. 查看

kubectl get all
[root@k8s-master deploy]# kubectl get all -owide
NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deploy/nginx-deployment   3         3         3            3           15s

NAME        DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
rc/myweb2   2         2         2         6m        myweb2         192.168.178.151:5000/nginx:1.15   app=myweb2

NAME             CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE       SELECTOR
svc/kubernetes   10.254.0.1      <none>        443/TCP        7h        <none>
svc/myweb        10.254.243.14   <nodes>       80:10000/TCP   42m       app=myweb2

NAME                             DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
rs/nginx-deployment-4175348009   3         3         3         15s       nginx          192.168.178.151:5000/nginx:1.13   app=nginx,pod-template-hash=4175348009

NAME                                   READY     STATUS    RESTARTS   AGE       IP            NODE
po/myweb2-3v3d5                        1/1       Running   0          3m        172.16.34.4   k8s-master
po/myweb2-m3hqd                        1/1       Running   0          3m        172.16.70.2   k8s-node2
po/nginx-deployment-4175348009-j7m33   1/1       Running   0          15s       172.16.77.2   k8s-node1
po/nginx-deployment-4175348009-m0z1h   1/1       Running   0          15s       172.16.34.3   k8s-master
po/nginx-deployment-4175348009-xwl19   1/1       Running   0          15s       172.16.70.3   k8s-node2

5. deployment关联svc

kubectl expose deployment nginx-deployment --port=80 --type=NodePort

6. 查看

kubectl get all
[root@k8s-master deploy]# kubectl expose deployment nginx-deployment --port=80 --type=NodePort
service "nginx-deployment" exposed
[root@k8s-master deploy]#
[root@k8s-master deploy]# kubectl get all -owide
NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deploy/nginx-deployment   3         3         3            3           5m

NAME                   CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE       SELECTOR
svc/kubernetes         10.254.0.1     <none>        443/TCP        7h        <none>
svc/nginx-deployment   10.254.6.221   <nodes>       80:22087/TCP   4s        app=nginx

NAME                             DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
rs/nginx-deployment-4175348009   3         3         3         5m        nginx          192.168.178.151:5000/nginx:1.13   app=nginx,pod-template-hash=4175348009

NAME                                   READY     STATUS    RESTARTS   AGE       IP            NODE
po/nginx-deployment-4175348009-j7m33   1/1       Running   0          5m        172.16.77.2   k8s-node1
po/nginx-deployment-4175348009-m0z1h   1/1       Running   0          5m        172.16.34.3   k8s-master
po/nginx-deployment-4175348009-xwl19   1/1       Running   0          5m        172.16.70.3   k8s-node2

7. curl测试

curl -I 192.168.178.151:22087

8. deployment升级

kubectl edit deployment nginx-deployment
#将nginx:1.13 改为 nginx:1.15
[root@k8s-master deploy]# kubectl edit deployment nginx-deployment
deployment "nginx-deployment" edited
[root@k8s-master deploy]#
[root@k8s-master deploy]# kubectl get all -owide
NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deploy/nginx-deployment   3         3         3            2           9m

NAME                   CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE       SELECTOR
svc/kubernetes         10.254.0.1     <none>        443/TCP        7h        <none>
svc/nginx-deployment   10.254.6.221   <nodes>       80:22087/TCP   3m        app=nginx

NAME                             DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
rs/nginx-deployment-4175348009   0         0         0         9m        nginx          192.168.178.151:5000/nginx:1.13   app=nginx,pod-template-hash=4175348009
rs/nginx-deployment-43893035     3         3         2         6s        nginx          192.168.178.151:5000/nginx:1.15   app=nginx,pod-template-hash=43893035

NAME                                 READY     STATUS              RESTARTS   AGE       IP            NODE
po/nginx-deployment-43893035-0c13z   1/1       Running             0          6s        172.16.34.3   k8s-master
po/nginx-deployment-43893035-13wlv   0/1       ContainerCreating   0          6s        <none>        k8s-node1
po/nginx-deployment-43893035-rk0gc   1/1       Running             0          5s        172.16.34.4   k8s-master

9. 再次curl测试

curl -I 192.168.178.151:22087
[root@k8s-master deploy]# curl -I 192.168.178.151:22087
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Sat, 12 Jun 2021 13:35:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes

10. 查看

kubectl get all -owide
[root@k8s-master deploy]# kubectl get all -owide
NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deploy/nginx-deployment   3         3         3            3           36m

NAME                   CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE       SELECTOR
svc/kubernetes         10.254.0.1     <none>        443/TCP        7h        <none>
svc/nginx-deployment   10.254.6.221   <nodes>       80:22087/TCP   30m       app=nginx

NAME                                 READY     STATUS    RESTARTS   AGE       IP            NODE
po/nginx-deployment-43893035-2rnd5   1/1       Running   1          22m       172.16.77.2   k8s-node1
po/nginx-deployment-43893035-stqc5   1/1       Running   1          22m       172.16.70.2   k8s-node2
po/nginx-deployment-43893035-z16vz   1/1       Running   0          22m       172.16.34.5   k8s-master

11. deployment回滚

kubectl rollout undo deployment nginx-deployment
[root@k8s-master deploy]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION        CHANGE-CAUSE
1               <none>
2               <none>

[root@k8s-master deploy]#
[root@k8s-master deploy]#
[root@k8s-master deploy]# kubectl rollout undo deployment nginx-deploy
Error from server (NotFound): deployments.extensions "nginx-deploy" not found

#报错,发现deployment名字写错了
[root@k8s-master deploy]#
[root@k8s-master deploy]# kubectl rollout undo deployment nginx-deployment
deployment "nginx-deployment" rolled back

12. curl测试

curl -I 192.168.178.151:22087
[root@k8s-master deploy]# curl -I 192.168.178.151:22087
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Sat, 12 Jun 2021 13:58:31 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

13. 查看deployment历史版本

kubectl rollout history deployment nginx-deployment
[root@k8s-master deploy]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION        CHANGE-CAUSE
2               <none>
3               <none>

14. deployment最佳实践

1 版本发布

kubectl run nginx --image=192.168.178.151:5000/nginx:1.13 --replicas=3 --record
#报错了。发现是-record前面少了一个横线。
[root@k8s-master deploy]# kubectl run nginx --image=192.168.178.151:5000/nginx:1.13 --replicas=3 -record
Error: invalid argument "ecord" for record: strconv.ParseInt: parsing "ecord": invalid syntax


Aliases:
run, run-container

Examples:
  # Start a single instance of nginx.
  kubectl run nginx --image=nginx

  # Start a single instance of hazelcast and let the container expose port 5701 .
  kubectl run hazelcast --image=hazelcast --port=5701

  # Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container.
  kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"

  # Start a replicated instance of nginx.
  kubectl run nginx --image=nginx --replicas=5

  # Dry run. Print the corresponding API objects without creating them.
  kubectl run nginx --image=nginx --dry-run

  # Start a single instance of nginx, but overload the spec of the deployment with a partial set of values parsed from JSON.
  kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'

  # Start a pod of busybox and keep it in the foreground, don't restart it if it exits.
  kubectl run -i -t busybox --image=busybox --restart=Never

  # Start the nginx container using the default command, but use custom arguments (arg1 .. argN) for that command.
  kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>

  # Start the nginx container using a different command and custom arguments.
  kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>

  # Start the perl container to compute π to 2000 places and print it out.
  kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'

  # Start the cron job to compute π to 2000 places and print it out every 5 minutes.
  kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'

Options:
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
      --attach=false: If true, wait for the Pod to start running, and then attach to the Pod as if 'kubectl attach ...' were called.  Default false, unless '-i/--stdin' is set, in which case the default is true. With '--restart=Never' the exit code of the container process is returned.
      --command=false: If true and extra arguments are present, use them as the 'command' field in the container, rather than the 'args' field which is the default.
      --dry-run=false: If true, only print the object that would be sent, without sending it.
      --env=[]: Environment variables to set in the container
      --expose=false: If true, a public, external service is created for the container(s) which are run
      --generator='': The name of the API generator to use, see http://kubernetes.io/docs/user-guide/kubectl-conventions/#generators for a list.
      --hostport=-1: The host port mapping for the container port. To demonstrate a single-machine container.
      --image='': The image for the container to run.
      --image-pull-policy='': The image pull policy for the container. If left empty, this value will not be specified by the client and defaulted by the server
      --include-extended-apis=true: If true, include definitions of new APIs via calls to the API server. [default true]
  -l, --labels='': Labels to apply to the pod(s).
      --leave-stdin-open=false: If the pod is started in interactive mode or with stdin, leave stdin open after the first attach completes. By default, stdin will be closed after the first attach completes.
      --limits='': The resource requirement limits for this container.  For example, 'cpu=200m,memory=512Mi'.  Note that server side components may assign limits depending on the server configuration, such as limit ranges.
      --no-headers=false: When using the default or custom-column output format, don't print headers.
  -o, --output='': Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
      --output-version='': Output the formatted object with the given group version (for ex: 'extensions/v1beta1').
      --overrides='': An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.
      --port='': The port that this container exposes.  If --expose is true, this is also the port used by the service that is created.
      --quiet=false: If true, suppress prompt messages.
      --record=false: Record current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
  -r, --replicas=1: Number of replicas to create for this container. Default is 1.
      --requests='': The resource requirement requests for this container.  For example, 'cpu=100m,memory=256Mi'.  Note that server side components may assign requests depending on the server configuration, such as limit ranges.
      --restart='Always': The restart policy for this Pod.  Legal values [Always, OnFailure, Never].  If set to 'Always' a deployment is created, if set to 'OnFailure' a job is created, if set to 'Never', a regular pod is created. For the latter two --replicas must be 1.  Default 'Always', for CronJobs `Never`.
      --rm=false: If true, delete resources created in this command for attached containers.
      --save-config=false: If true, the configuration of current object will be saved in its annotation. This is useful when you want to perform kubectl apply on this object in the future.
      --schedule='': A schedule in the Cron format the job should be run with.
      --service-generator='service/v2': The name of the generator to use for creating a service.  Only used if --expose is true
      --service-overrides='': An inline JSON override for the generated service object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.  Only used if --expose is true.
  -a, --show-all=false: When printing, show all resources (default hide terminated pods.)
      --show-labels=false: When printing, show all labels as the last column (default hide labels column)
      --sort-by='': If non-empty, sort list types using this field specification.  The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
  -i, --stdin=false: Keep stdin open on the container(s) in the pod, even if nothing is attached.
      --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
  -t, --tty=false: Allocated a TTY for each container in the pod.

Usage:
  kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

Error: invalid argument "ecord" for record: strconv.ParseInt: parsing "ecord": invalid syntax

2 版本查看

kubectl rollout history deployment nginx
[root@k8s-master deploy]# kubectl run nginx --image=192.168.178.151:5000/nginx:1.13 --replicas=3 --record
deployment "nginx" created
[root@k8s-master deploy]#
[root@k8s-master deploy]#
[root@k8s-master deploy]# kubectl rollout history deployment nginx
deployments "nginx"
REVISION        CHANGE-CAUSE
1               kubectl run nginx --image=192.168.178.151:5000/nginx:1.13 --replicas=3 --record

[root@k8s-master deploy]#

3 版本升级

kubectl set image deploy nginx nginx=192.168.178.151:5000/nginx:1.15

4 历史版本查询

kubectl rollout history deployment nginx
[root@k8s-master deploy]# kubectl set image deploy nginx nginx=192.168.178.151:5000/nginx:1.15
deployment "nginx" image updated
[root@k8s-master deploy]#
[root@k8s-master deploy]# kubectl rollout history deployment nginx
deployments "nginx"
REVISION        CHANGE-CAUSE
1               kubectl run nginx --image=192.168.178.151:5000/nginx:1.13 --replicas=3 --record
2               kubectl set image deploy nginx nginx=192.168.178.151:5000/nginx:1.15

[root@k8s-master deploy]#

5 回退到版本1

kubectl rollout undo deployment nginx --to-revision=1

6 再次查看历史版本

kubectl rollout history deployment nginx
[root@k8s-master deploy]# kubectl rollout undo deployment nginx --to-revision=1
deployment "nginx" rolled back
[root@k8s-master deploy]#
[root@k8s-master deploy]# kubectl rollout history deployment nginx
deployments "nginx"
REVISION        CHANGE-CAUSE
2               kubectl set image deploy nginx nginx=192.168.178.151:5000/nginx:1.15
3               kubectl run nginx --image=192.168.178.151:5000/nginx:1.13 --replicas=3 --record

[root@k8s-master deploy]#

15. 删除deployment

kubectl delete deployment nginx
#那删除svc是不是
kubectl delete svc nginx-svc

#删除rc是不是
kubectl delete rc nginx-rc

#待测试!已测试,确实!
kubectl delete pod --all

最后更新: 2022-02-22 04:55:01