• 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开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

如何快速更新自己的技术积累?

  • 在现有的项目里,深挖技术,比如用到netty可以把相关底层代码和要点都看起来。
  • 如果不知道目前的努力方向,就看自己的领导或公司里技术强的人在学什么。
  • 知道努力方向后不知道该怎么学,就到处去找相关资料然后练习。
  • 学习以后不知道有没有学成,则可以通过面试去检验。

我个人觉得面试也像是一场全新的征程,失败和胜利都是平常之事。所以,劝各位不要因为面试失败而灰心、丧失斗志。也不要因为面试通过而沾沾自喜,等待你的将是更美好的未来,继续加油!

以上面试专题的答小编案整理成面试文档了,文档里有答案详解,以及其他一些大厂面试题目

八年CRUD,疫情备战三个月,三面头条、四面阿里拿offer面经分享

八年CRUD,疫情备战三个月,三面头条、四面阿里拿offer面经分享

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
tStatus

configmaps cm v1 true ConfigMap

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

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-VueU7UMk-1713211296781)]

[外链图片转存中…(img-YWIC5mnv-1713211296781)]

[外链图片转存中…(img-Geat7KWt-1713211296782)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

如何快速更新自己的技术积累?

  • 在现有的项目里,深挖技术,比如用到netty可以把相关底层代码和要点都看起来。
  • 如果不知道目前的努力方向,就看自己的领导或公司里技术强的人在学什么。
  • 知道努力方向后不知道该怎么学,就到处去找相关资料然后练习。
  • 学习以后不知道有没有学成,则可以通过面试去检验。

我个人觉得面试也像是一场全新的征程,失败和胜利都是平常之事。所以,劝各位不要因为面试失败而灰心、丧失斗志。也不要因为面试通过而沾沾自喜,等待你的将是更美好的未来,继续加油!

以上面试专题的答小编案整理成面试文档了,文档里有答案详解,以及其他一些大厂面试题目

[外链图片转存中…(img-KcErk4hw-1713211296782)]

[外链图片转存中…(img-WBBb7zAV-1713211296782)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐