1.Kubernetes对象
1.1 什么是对象
在Kubernetes系统中,我们所操作的资源就是对象,而对象是一个持久化的实体,也就是说会将我们对资源的操作记录下来。Kubernetes需要使用这些实体来表示整个集群的状态。它们描述了如下信息
- 哪些容器化应用在运行,以及这些应用运行在哪些节点上
- 应用程序可以被使用的资源
- 应用程序运行时的策略,比如重启策略、升级策略,以及容错策略
Kubernetes对象是目标性记录,也就是说一旦创建对象,Kubernetes系统将持续工作以确保该对象存在,并达到用户所期望的状态。一旦我们想要操作 Kubernetes 对象,无论是创建、修改,或者删除,都需要使用到Kubernetes API 接口
1.2 对象规范与状态
Kubernetes几乎每个对象都包含两个嵌套的对象字段,对象spec(规范)和对象status(状态)。
- spec:是在创建该对象时设定其内容,通过spec来描述你希望对象所具有的特征:期望状态(DesiredState
- status描述了对象的当前状态(Current State),它是由Kubernetes系统和组件设置并更新的
任何时刻,Kubernetes控制平面都一直积极地管理着对象的实际状态
,与期望状态
相匹配
例如,Kubernetes中的 Deployment 对象能够表示运行在集群中的应用。当创建Deployment 时,可能需要设置DepLoyment的spec,用于指定该应用需要有3个副本运行。Kubernetes系统读取Deployment 规范,并启动我们所期望的应用的3个实例,更新当前状态以与规范中期望状态橡皮匹配。如果这些实例中有的失败了(一种状态变更),Kubernetes系统通过执行修正操作来响应spec
与status
状态间出现的不一致,可能会启动一个新的实例来替换失败的实例。
1.3 理解对象
创建Kubernetes对象时,必须提供对象的 spec(规范),用来描述该对象的期望状态,以及对象的一些基本信息(例如名称)。使用kubectl创建资源对象时,请求KubernetesAPI必须在请求体中包含JSON格式的信息。 1但大多数情况下我们都是使用的YAML格式来创建资源,所以只需要在YAML格式文件中描述对应的spec规范。在kubectl发起API请求时,会将这些信息转换成JSON 格式。
这里有一个.yaml示例文件,展示了Kubernetes Deployment 创建时的必需字段和对象spec规范:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
spec:
replicas: 3 # 期望的副本数
selector: # Deployment要管理的Pod有哪些 只要标签app=nginx就加入进行管理
matchLabels:
app: nginx
template: # Pod模板;有标签;有Pod规范
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- name: http
containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
spec:
replicas: 3 # 期望的副本数
selector: # Deployment要管理的Pod有哪些 只要标签app=nginx就加入进行管理
matchLabels:
app: nginx
template: # Pod模板;有标签;有Pod规范
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- name: http
containerPort: 80
接下来就可以通过kubectl应用该文件,然后kubectl会将该文件转为json提交给KubernetesAPI
1.4 对象必须字段
在想要创建的Kubernetes 对象对应的.yam文件中,需要配置如下的字段
- apiVersion-创建该对象所使用的 Kubernetes API 的版本
- kind-想要创建的对象的类别,pod,deployment,service
- metadataa-标识对象唯一性的一些数据,包括一个name e字符串、UID和可选的Jnamespace
- specC-1你所期望的该对象的状态
创建
kubectl create my-nginx --image=nginx
kubectl get pod my-nginx -o yaml
#⼲跑⼀次
kubectl create my-nginx --image=nginx -dry-run -o yaml
kubectl create my-nginx --image=nginx
kubectl get pod my-nginx -o yaml
#⼲跑⼀次
kubectl create my-nginx --image=nginx -dry-run -o yaml
1.5 对象实践
pod
kubectl explain pod.spec
kubectl explain pod.spec
containers
kubectl explain pod.spec.containers
kubectl explain pod.spec.containers
deployment
kubectl explain deployment
kubectl explain deployment
service
kubectl explain service
kubectl explain service
2. 实战
2.1 部署应用
vi dp_demoapp.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-deploy
namespace: default
spec:
replicas: 3 # 副本数
selector: # 通过标签选择器选择要管理的Pod
matchLabels:
app: demoapp
template: #定义Pod模板
metadata:
labels: #给Pod添加一个标签,标签的key为run,标签的value为app
app: demoapp
spec:
containers:
- name: webserver #容器的名称
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp/demoapp:v1 #容器需要的对象
ports:
- name: http
containerPort: 80 #容器对外的端口是80
vi dp_demoapp.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-deploy
namespace: default
spec:
replicas: 3 # 副本数
selector: # 通过标签选择器选择要管理的Pod
matchLabels:
app: demoapp
template: #定义Pod模板
metadata:
labels: #给Pod添加一个标签,标签的key为run,标签的value为app
app: demoapp
spec:
containers:
- name: webserver #容器的名称
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp/demoapp:v1 #容器需要的对象
ports:
- name: http
containerPort: 80 #容器对外的端口是80
#创建
kubectl apply -f dp_demoapp.yml
#创建
kubectl apply -f dp_demoapp.yml
2.2 访问应用
vim dp_demoapp_svc.yml
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp # Service通过标签选择器将对应的Pod定义为一组backend,而后将所有请求调度到这组Pod上
ports:
- name: http
port: 9090 # 负载均衡的端口(User--> ServiceIP+ServicePort --> PodIP+PodPort)
targetPort: 80 # 后端节点的端口(容器对外的端口)
vim dp_demoapp_svc.yml
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp # Service通过标签选择器将对应的Pod定义为一组backend,而后将所有请求调度到这组Pod上
ports:
- name: http
port: 9090 # 负载均衡的端口(User--> ServiceIP+ServicePort --> PodIP+PodPort)
targetPort: 80 # 后端节点的端口(容器对外的端口)
#创建
kubectl apply -f dp_demoapp_svc.yml
#查看负载后端ip地址
[root@kube-master yaml]# kubectl describe service demoapp-service
Name: demoapp-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=demoapp
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 192.168.63.161
IPs: 192.168.63.161
Port: http 9090/TCP
TargetPort: 80/TCP
Endpoints: 172.17.74.82:80,172.23.127.81:80,172.30.0.137:80
Session Affinity: None
Events: <none>
#创建
kubectl apply -f dp_demoapp_svc.yml
#查看负载后端ip地址
[root@kube-master yaml]# kubectl describe service demoapp-service
Name: demoapp-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=demoapp
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 192.168.63.161
IPs: 192.168.63.161
Port: http 9090/TCP
TargetPort: 80/TCP
Endpoints: 172.17.74.82:80,172.23.127.81:80,172.30.0.137:80
Session Affinity: None
Events: <none>
2.3 Scale应⽤
扩容
kubectl scale deployment demoapp-deploy --replicas=5
#查看
[root@kube-master yaml]# kubectl describe svc demoapp-service
Name: demoapp-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=demoapp
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 192.168.63.161
IPs: 192.168.63.161
Port: http 9090/TCP
TargetPort: 80/TCP
Endpoints: 172.17.74.82:80,172.23.127.81:80,172.23.127.82:80 + 2 more...
#查看Endpoints
[root@kube-master yaml]# kubectl get Endpoints
NAME ENDPOINTS AGE
demoapp-service 172.17.74.82:80,172.23.127.81:80,172.23.127.82:80 + 2 more... 142m
#查看+2 more更多隐藏的ip
[root@kube-master yaml]# kubectl describe endpoints demoapp-service
Name: demoapp-service
Namespace: default
Labels: <none>
Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2024-04-14T12:57:37Z
Subsets:
Addresses: 172.17.74.82,172.23.127.81,172.23.127.82,172.30.0.137,172.30.0.138
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
http 80 TCP
Events: <none>
kubectl scale deployment demoapp-deploy --replicas=5
#查看
[root@kube-master yaml]# kubectl describe svc demoapp-service
Name: demoapp-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=demoapp
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 192.168.63.161
IPs: 192.168.63.161
Port: http 9090/TCP
TargetPort: 80/TCP
Endpoints: 172.17.74.82:80,172.23.127.81:80,172.23.127.82:80 + 2 more...
#查看Endpoints
[root@kube-master yaml]# kubectl get Endpoints
NAME ENDPOINTS AGE
demoapp-service 172.17.74.82:80,172.23.127.81:80,172.23.127.82:80 + 2 more... 142m
#查看+2 more更多隐藏的ip
[root@kube-master yaml]# kubectl describe endpoints demoapp-service
Name: demoapp-service
Namespace: default
Labels: <none>
Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2024-04-14T12:57:37Z
Subsets:
Addresses: 172.17.74.82,172.23.127.81,172.23.127.82,172.30.0.137,172.30.0.138
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
http 80 TCP
Events: <none>
缩减
[root@kube-master yaml]# kubectl scale deployment demoapp-deploy --replicas=3
deployment.apps/demoapp-deploy scaled
[root@kube-master yaml]# kubectl scale deployment demoapp-deploy --replicas=3
deployment.apps/demoapp-deploy scaled
💡 说明
有三种方式可以修改,
kubectl scale
kubectl edit deployment dp_name
vim yaml
2.4 滚动更新
谓滚动更新,更新的是镜像,使用新的镜像逐步更新Pod,回退也是一样的。但对于用户而言是无感知
查看pod使用哪个镜像,通过yaml观察
kubectl get pod demoapp-deploy-56b485fd48-c4kjp -oyaml
kubectl get pod demoapp-deploy-56b485fd48-c4kjp -oyaml
升级
--record ,记录更新变化的详细内容,方便后续回滚
kubectl set image deployment demoapp-deploy *=oldxu3957/demoapp:v1.1 --record
kubectl set image deployment demoapp-deploy *=oldxu3957/demoapp:v1.1 --record
- 查看升级过程
#查看dp
[root@kube-master yaml]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
demoapp-deploy 3/3 3 3 3h1m
#查看升级后的日志
[root@kube-master yaml]# kubectl describe deployments.apps demoapp-deploy
。。。
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 30m deployment-controller Scaled up replica set demoapp-deploy-56b485fd48 to 5
Normal ScalingReplicaSet 23m deployment-controller Scaled down replica set demoapp-deploy-56b485fd48 to 3
#查看dp
[root@kube-master yaml]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
demoapp-deploy 3/3 3 3 3h1m
#查看升级后的日志
[root@kube-master yaml]# kubectl describe deployments.apps demoapp-deploy
。。。
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 30m deployment-controller Scaled up replica set demoapp-deploy-56b485fd48 to 5
Normal ScalingReplicaSet 23m deployment-controller Scaled down replica set demoapp-deploy-56b485fd48 to 3
回滚
#查看回滚版本
root@kube-master yaml]# kubectl rollout history deployment
deployment.apps/demoapp-deploy
REVISION CHANGE-CAUSE
1 <none>
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
#回滚到指定的版本
kubectl rollout undo deploy demoapp-deploy --to-revision=1
#查看回滚版本
root@kube-master yaml]# kubectl rollout history deployment
deployment.apps/demoapp-deploy
REVISION CHANGE-CAUSE
1 <none>
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
#回滚到指定的版本
kubectl rollout undo deploy demoapp-deploy --to-revision=1