K8s Kubectl基础命令的使用、滚动更新、以及回滚操作

  • scale

  • autoscale

  • cluster-info

  • cordon

  • uncordon

  • describe

  • logs

  • attach

  • exec

  • prot-forward

  • cp

  • label

  • api-resources

  • api-versions

  • version

  • 使用deployment控制器部署镜像

  • 滚动更新、回滚

    • 用Dockerfile编写两个镜像
  • 滚动更新、回滚

kubernetes基本概念

=============================================================================

  • Pod:k8s最小部署单元,一组容器的集合

  • Deployment:最常见的控制器,用于更高级别部署和管理Pod

  • Service:为一组Pod提供负载均衡,对外提供统一访问入口

  • Label:标签,附加到某个资源上,用于关联对象、查询和筛选

  • Namespaces:命名空间。将对象逻辑上隔离,也利于权限控制

kubectl管理命令


| 类型 | 命令 | 描述 |

| — | :-- | :-- |

| 基础命令 | create

expose

run

expose

set

explain

get

edit

delete | 通过文件名或标准输入创建资源;

为Deployment,Pod创建Service;

在集群中运行一个特定的镜像;

在对象上设置特定的功能;

文档参考资料;

显示一个或多个资源;

使用系统编辑器编辑一个资源;

通过文件名,标准输入,资源名称或标签选择器来删除资源 |

| 部署命令 | rollout

rolling-update

scale

autoscale | 管理Deployment,Daemonset资源的发布(例如状态、发布记录、回滚等);

滚动升级,仅限ReplicationController;

对Deployment、ReplicaSet、RC或Job资源扩容或缩容Pod数量;

为Deploy,RS,RC配置自动伸缩规则(依赖metrics-server和hpa) |

| 集群管理命令 | certificate

cluster-info

top

cordon

uncordon drain

taint | 修改证书资源;

显示集群信息;

查看资源利用率(依赖metrics-server);

标记节点不可调度;

标记节点可调度;

驱逐节点上的应用,准备下线维护;

修改节点taint标记 |

kubectl帮助


kubectl命令官方文档

用kubectl help可以查看kubectl相关的命令

[root@master ~]# kubectl --help

kubectl controls the Kubernetes cluster manager.

Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):

create Create a resource from a file or from stdin.

expose 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个新的Kubernetes Service

run 在集群中运行一个指定的镜像

set 为 objects 设置一个指定的特征

Basic Commands (Intermediate):

explain 查看资源的文档

get 显示一个或更多 resources

edit 在服务器上编辑一个资源

delete Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:

rollout Manage the rollout of a resource

scale 为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量

autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量

Cluster Management Commands:

certificate 修改 certificate 资源.

cluster-info 显示集群信息

top Display Resource (CPU/Memory/Storage) usage.

cordon 标记 node 为 unschedulable

uncordon 标记 node 为 schedulable

drain Drain node in preparation for maintenance

taint 更新一个或者多个 node 上的 taints

Troubleshooting and Debugging Commands:

describe 显示一个指定 resource 或者 group 的 resources 详情

logs 输出容器在 pod 中的日志

attach Attach 到一个运行中的 container

exec 在一个 container 中执行一个命令

port-forward Forward one or more local ports to a pod

proxy 运行一个 proxy 到 Kubernetes API server

cp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.

auth Inspect authorization

Advanced Commands:

diff Diff live version against would-be applied version

apply 通过文件名或标准输入流(stdin)对资源进行配置

patch 使用 strategic merge patch 更新一个资源的 field(s)

replace 通过 filename 或者 stdin替换一个资源

wait Experimental: Wait for a specific condition on one or many resources.

convert 在不同的 API versions 转换配置文件

Settings Commands:

label 更新在这个资源上的 labels

annotate 更新一个资源的注解

completion Output shell completion code for the specified shell (bash or zsh)

Other Commands:

alpha Commands for features in alpha

api-resources Print the supported API resources on the server

api-versions Print the supported API versions on the server, in the form of “group/version”

config 修改 kubeconfig 文件

plugin Provides utilities for interacting with plugins.

version 输出 client 和 server 的版本信息

Kubectl常用命令的使用

=============================================================================

explain


获取资源

用法:

kubectl explain RESOURCE

//获取资源及其字段的文档

[root@master ~]# kubectl explain pods

KIND: Pod

VERSION: v1

DESCRIPTION:

Pod is a collection of containers that can run on a host. This resource is

created by clients and scheduled onto hosts.

FIELDS:

apiVersion

APIVersion defines the versioned schema of this representation of an

object. Servers should convert recognized schemas to the latest internal

value, and may reject unrecognized values. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

kind

Kind is a string value representing the REST resource this object

represents. Servers may infer this from the endpoint the client submits

requests to. Cannot be updated. In CamelCase. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

metadata

Standard object’s metadata. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

spec

Specification of the desired behavior of the pod. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

status

Most recently observed status of the pod. This data may not be up to date.

Populated by the system. Read-only. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

edit


编辑资源

//运行一个pod类型的nginx,名字叫nginx,内型是deployment

[root@master ~]# kubectl create deployment nginx --image nginx

deployment.apps/nginx created

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-cpbg4 0/1 ContainerCreating 0 10s

//使用edit命令编辑

[root@master ~]# kubectl edit deployment nginx

kind: Deployment

metadata:

annotations:

deployment.kubernetes.io/revision: “1”

creationTimestamp: “2021-12-20T10:33:49Z”

generation: 1

labels:

app: nginx

name: jjyy #添加name: jjyy

//查看

[root@master ~]# kubectl describe deploy nginx

Name: nginx

Namespace: default

CreationTimestamp: Mon, 20 Dec 2021 18:33:49 +0800

Labels: app=nginx

name=jjyy

Annotations: deployment.kubernetes.io/revision: 1

Selector: app=nginx

Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable

StrategyType: RollingUpdate

MinReadySeconds: 0

RollingUpdateStrategy: 25% max unavailable, 25% max surge

Pod Template:

scale


动态扩展

//使用scale扩展

[root@master ~]# kubectl scale deployment/nginx --replicas 3

deployment.apps/nginx scaled

//扩展后查看多了几个相同类型的pod

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-8bnn8 1/1 Running 0 37s

nginx-6799fc88d8-cpbg4 1/1 Running 0 15m

nginx-6799fc88d8-t6vpw 1/1 Running 0 37s

//如果只需要2个deployment类型的nginx的pod

[root@master ~]# kubectl scale deployment/nginx --replicas 2

deployment.apps/nginx scaled

//查看发现只有两个(删除是随机的)

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-8bnn8 1/1 Running 0 3m22s

nginx-6799fc88d8-cpbg4 1/1 Running 0 18m

autoscale


自动扩展,给定一个范围,自动根据业务的访问量增加或减少

//最少3个,当不满足条件时会自动创建

[root@master ~]# kubectl autoscale deployment/nginx --min 3 --max=5

horizontalpodautoscaler.autoscaling/nginx autoscaled

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-8bnn8 1/1 Running 0 8m1s

nginx-6799fc88d8-cpbg4 1/1 Running 0 23m

nginx-6799fc88d8-ctjmt 0/1 ContainerCreating 0 4s

//当大于5个时,先创建,之后也会随机删除,直到满足条件五个

[root@master ~]# kubectl scale deployment/nginx --replicas 7

deployment.apps/nginx scaled

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-84mfw 1/1 Running 0 52s

nginx-6799fc88d8-8bnn8 1/1 Running 0 20m

nginx-6799fc88d8-cpbg4 1/1 Running 0 35m

nginx-6799fc88d8-ctjmt 1/1 Running 0 12m

nginx-6799fc88d8-rf74m 1/1 Running 0 52s

cluster-info


显示集群信息

[root@master ~]# kubectl cluster-info

Kubernetes control plane is running at https://192.168.129.250:6443

KubeDNS is running at https://192.168.129.250:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use ‘kubectl cluster-info dump’.

cordon


可调度

[root@master ~]# kubectl cordon node2

node/node2 already cordoned

[root@master ~]# kubectl get node

NAME STATUS ROLES AGE VERSION

master Ready control-plane,master 2d4h v1.20.0

node1 NotReady 2d4h v1.20.0

node2 Ready,SchedulingDisabled 2d4h v1.20.0

[root@master ~]# kubectl get pod -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

nginx-6799fc88d8-cpbg4 1/1 Running 0 132m 10.244.1.24 node1

nginx-6799fc88d8-plvhf 1/1 Running 0 95m 10.244.1.31 node1

nginx-6799fc88d8-rf74m 1/1 Running 1 97m 10.244.2.4 node2

[root@master ~]# kubectl scale deployment/nginx --replicas 2

[root@master ~]# kubectl get pod -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

nginx-6799fc88d8-9wk4h 0/1 Pending 0 3m46s

nginx-6799fc88d8-rf74m 1/1 Running 1 103m 10.244.2.4 node2

nginx-6799fc88d8-snm2v 0/1 Pending 0 16s

uncordon


不可调度

[root@master ~]# kubectl uncordon node2

node/node2 uncordoned

[root@master ~]# kubectl get node

NAME STATUS ROLES AGE VERSION

master Ready control-plane,master 2d4h v1.20.0

node1 NotReady 2d4h v1.20.0

node2 Ready 2d4h v1.20.0

[root@master ~]# kubectl create deployment nginx2 --image nginx

deployment.apps/nginx2 created

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-9wk4h 1/1 Running 0 8m22s

nginx-6799fc88d8-rf74m 1/1 Running 1 108m

nginx-6799fc88d8-snm2v 1/1 Running 0 4m52s

nginx2-5fc4444698-bf76p 1/1 Running 0 25s

[root@master ~]# kubectl get pod -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

nginx-6799fc88d8-9wk4h 1/1 Running 0 8m59s 10.244.2.5 node2

nginx-6799fc88d8-rf74m 1/1 Running 1 109m 10.244.2.4 node2

nginx-6799fc88d8-snm2v 1/1 Running 0 5m29s 10.244.2.6 node2

nginx2-5fc4444698-bf76p 1/1 Running 0 62s 10.244.2.7 node2

describe


显示指定pod的详细信息

[root@master ~]# kubectl describe pod nginx

Name: nginx-6799fc88d8-cpbg4

Namespace: default

Priority: 0

Node: node1/192.168.129.135

Start Time: Mon, 20 Dec 2021 18:33:49 +0800

Labels: app=nginx

pod-template-hash=6799fc88d8

Annotations:

Status: Running

IP: 10.244.1.24

logs


查看日志

[root@master ~]# kubectl logs deployment/nginx

2021/12/20 10:34:05 [notice] 1#1: start worker processes

2021/12/20 10:34:05 [notice] 1#1: start worker process 32

2021/12/20 10:34:05 [notice] 1#1: start worker process 33

/docker-entrypoint.sh: Configuration complete; ready for start up

[root@master ~]# kubectl get svc

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP 10.96.0.1 443/TCP 2d4h

nginx ClusterIP 10.102.213.63 8080/TCP 24h

[root@master ~]# curl 10.102.213.63:8080

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

nginx.org.

Commercial support is available at

nginx.com.

Thank you for using nginx.

[root@master ~]# kubectl logs deployment/nginx

2021/12/20 12:44:49 [notice] 1#1: start worker processes

2021/12/20 12:44:49 [notice] 1#1: start worker process 31

2021/12/20 12:44:49 [notice] 1#1: start worker process 32

10.244.0.0 - - [20/Dec/2021:13:04:14 +0000] “GET / HTTP/1.1” 200 615 “-” “curl/7.61.1” “-”

10.244.0.0 - - [20/Dec/2021:13:05:26 +0000] “GET / HTTP/1.1” 200 615 “-” “curl/7.61.1” “-”

attach


附加在一个容器里

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-cpbg4 1/1 Running 0 43m

nginx-6799fc88d8-plvhf 1/1 Running 0 6m38s

nginx-6799fc88d8-rf74m 1/1 Running 0 8m57s

[root@master ~]# kubectl attach nginx-6799fc88d8-cpbg4

Defaulting container name to nginx.

Use ‘kubectl describe pod/nginx-6799fc88d8-cpbg4 -n default’ to see all of the containers in this pod.

If you don’t see a command prompt, try pressing enter.

exec


执行容器命令

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-cpbg4 1/1 Running 0 45m

nginx-6799fc88d8-plvhf 1/1 Running 0 8m2s

nginx-6799fc88d8-rf74m 1/1 Running 0 10m

[root@master ~]# kubectl exec nginx-6799fc88d8-cpbg4 – date

Mon Dec 20 11:20:02 UTC 2021

[root@master ~]# kubectl exec -it nginx-6799fc88d8-cpbg4 – /bin/sh

ls

bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var

boot docker-entrypoint.d etc lib media opt root sbin sys usr

exit

[root@master ~]#

prot-forward


转发一个或多个端口到pod里面去

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

web-96d5df5c8-8fzdv 1/1 Running 0 17s

[root@master ~]# kubectl port-forward deployment/web 80 #可以变成’:80’(随机一个端口号,只能本机访问)或者变成指定的端口号’8080:80’(本机访问用80,其它主机访问就可以用8080)

Forwarding from 127.0.0.1:80 -> 80

Forwarding from [::1]:80 -> 80

[root@master ~]# ss -anlt

State Recv-Q Send-Q Local Address:Port Peer Address:Port

… 0.0.0.0:*

LISTEN 0 128 127.0.0.1:2381 0.0.0.0:*

LISTEN 0 128 127.0.0.1:80 0.0.0.0:*

[root@master ~]# curl 127.0.0.1

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

nginx.org.

Commercial support is available at

nginx.com.

Thank you for using nginx.

[root@master ~]# kubectl port-forward --address 0.0.0.0 deployment/web 80

Forwarding from 0.0.0.0:80 -> 80

//允许所有IP访问80端口

[root@master ~]# curl 192.168.129.250

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

nginx.org.

Commercial support is available at

nginx.com.

Thank you for using nginx.

在这里插入图片描述

cp


复制

拷贝文件或目录到容器中,或者从容器内向外拷贝

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-cpbg4 1/1 Terminating 0 3h42m

nginx-6799fc88d8-plvhf 1/1 Terminating 0 3h5m

web-96d5df5c8-8fzdv 1/1 Running 0 5m38s

[root@master ~]# ls

anaconda-ks.cfg init kube-flannel.yml

[root@master ~]# kubectl cp /root/anaconda-ks.cfg web-96d5df5c8-8fzdv:/usr

[root@master ~]# kubectl exec web-96d5df5c8-8fzdv – ls -l /usr/

total 4

-rw------- 1 root root 1184 Dec 20 14:17 anaconda-ks.cfg

drwxr-xr-x 1 root root 261 Dec 2 10:59 bin

drwxr-xr-x 2 root root 6 Oct 3 09:15 games

…略

label


标签

给资源设置、更新标签

[root@master ~]# kubectl run nginx --image nginx

pod/nginx created

[root@master ~]# kubectl describe pod nginx

Name: nginx

Namespace: default

Priority: 0

Node: node2/192.168.129.136

Start Time: Mon, 20 Dec 2021 22:34:38 +0800

Labels: run=nginx

//追加标签

[root@master ~]# kubectl label pods nginx app=test

pod/nginx labeled

//查看标签

[root@master ~]# kubectl describe pod nginx

Name: nginx

Namespace: default

Priority: 0

Node: node2/192.168.129.136

Start Time: Mon, 20 Dec 2021 22:34:38 +0800

Labels: app=test

run=nginx

//更改标签

[root@master ~]# kubectl label --overwrite pods nginx app=hehe

pod/nginx labeled

//查看是否更改成功

[root@master ~]# kubectl describe pod nginx

Name: nginx

Namespace: default

Priority: 0

Node: node2/192.168.129.136

Start Time: Mon, 20 Dec 2021 22:34:38 +0800

Labels: app=hehe

run=nginx

api-resources


查看所有资源

[root@master ~]# kubectl api-resources

NAME SHORTNAMES APIVERSION NAMESPACED KIND

bindings v1 true Binding

componentstatuses cs v1 false ComponentStatus

configmaps cm v1 true ConfigMap

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
22:34:38 +0800

Labels: app=hehe

run=nginx

api-resources


查看所有资源

[root@master ~]# kubectl api-resources

NAME SHORTNAMES APIVERSION NAMESPACED KIND

bindings v1 true Binding

componentstatuses cs v1 false ComponentStatus

configmaps cm v1 true ConfigMap

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-LzN0FBi3-1714863929236)]

[外链图片转存中…(img-bZ2cqnyJ-1714863929237)]

[外链图片转存中…(img-URiiukhx-1714863929237)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 16
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值