k8s资源清单描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
ApiVersion:
资源的版本,官方对每个资源规定了不同的版本,我们需要根据资源的版本进行书写。
一般情况下,资源的版本很少变动,除非在升级过程中,随着功能的增强,版本可能也会微调。

kind:
资源的类型,K8S集群中一切皆资源。

metadata:
资源的元数据信息,包括但不限于:
- 资源的名称;(常用)
- 资源所属的名称空间;(常用)
- 资源的标签;(常用)
- 资源的注解;(早期版本常用)
- 资源的创建时间;
- ...

spec:
定义了用户对Pod的期望状态。用户可以做如下操作:
- 指定节点调度;
- 指定调度的亲和性或反亲和性;
- 资源的数据是否进行持久化;
- 资源的配置文件是否需要修改;
- 资源的容器使用具体的镜像;
- 资源的限制
- 资源的认证信息
- 资源的安全信息
- ...

status:
资源实际的状态,比如调度到哪个worker节点,实际分配的IP地址,运行周期,重启次数等。
该资源一般不需要人为定义,而是有K8S集群自身维护。

[root@master231 ~]# kubectl api-resources
查看所有资源

pod资源及资源清单

1.POD资源

kubernetes资源对象操作

资源对象
node kubectl delete node kubectl get nodes
namespace kubectl create ns [资源名] kubectl delete ns [资源名] kubectl edit ns [资源名] kubectl get ns
pod kubectl delete pod [资源名] kubectl edit pod[资源名] kubectl get pod

查看集群节点状态

1
2
3
4
5
6
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 4d13h v1.19.3
node01 Ready <none> 4d13h v1.19.3
node02 Ready <none> 4d13h v1.19.3
node03 Ready <none> 4d13h v1.19.3

查看名称空间

1
2
3
4
5
6
7
8
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 4d13h
kube-flannel Active 4d12h
kube-node-lease Active 4d13h
kube-public Active 4d13h
kube-system Active 4d13h
luay Active 4h17m

查看pod (默认default名称空间)

1
2
3
4
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-565785f75c-tpjmj 1/1 Running 0 6h8m
nginx-pod 1/1 Running 0 5h43m

查看pod (指定名称空间)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@master ~]# kubectl get pods -n luay
NAME READY STATUS RESTARTS AGE
busybox-pod 2/2 Running 0 3m30s


[root@master ~]# kubectl get pod -n luay -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox-pod 2/2 Running 0 4m14s 10.2.2.6 node03 <none> <none>

-n 指定命名空间
-o 指定输出方式
json:输出json格式
yaml:输出yaml格式
wide:输出详细信息

2.pod的资源清单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@master01 ~]# vim nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: default
spec:
containers:
- image: nginx:alpine
imagePullPolicy: IfNotPresent
name: nginx-containers

`注释`
apiVersion: v1 # K8S资源接口
kind: Pod # 资源类型
metadata: #资源的元数据
name: nginx-pod # POD名字
namespace: default # 指定资源启动在哪个名称空间,默认default
spec: #容器相关信息
containers: #容器信息
- image: nginx:alpine #指定容器镜像
imagePullPolicy: IfNotPresent #指定镜像拉取规则
name: nginx-containers #容器名字


Always:总是拉取镜像,不管当前机器上是否有该镜像都拉取 nginx:latest
Never:从不拉取镜像,需要提前docker pull
IfNotPresent:如果镜像不存在,则拉取

资源清单的增,删,查

1.编写资源清单

1
2
3
4
5
6
7
8
9
[root@master231:1 pods]# cat single-pods.yaml 
apiVersion: v1 # 指定资源的api版本信息
kind: Pod # 指定资源的类型
metadata: # 指定Pod的元数据信息
name: xiuxian # 指的Pod的名称
spec: # 定义期望资源
containers: # 定义容器资源
- name: c1 # 容器的名称
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 # 容器的镜像

2.创建资源

1
2
[root@master231:1 pods]# kubectl create -f single-pods.yaml 
pod/xiuxian created

3.查看资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@master231:1 pods]# kubectl get pods
NAME READY STATUS RESTARTS AGE
xiuxian 1/1 Running 0 54s

[root@master231:1 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
xiuxian 1/1 Running 0 86s 10.100.2.4 worker233 <none> <none>

[root@master231:1 pods]# curl 10.0.100.2.4
curl: (6) Could not resolve host: 10.0.100.2.4
[root@master231:1 pods]# curl 10.100.2.4
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>yinzhengjie apps v1</title>
<style>
div img {
width: 900px;
height: 600px;
margin: 0;
}
</style>
</head>

<body>
<h1 style="color: green">凡人修仙传 v1 </h1>
<div>
<img src="1.jpg">
<div>
</body>

</html>

4.删除资源

1
2
3
4
5
6
[root@master231:1 pods]# kubectl delete -f single-pods.yaml 
pod "xiuxian" deleted

[root@master231:1 pods]# kubectl get pods -o wide
No resources found in default namespace.
[root@master231:1 pods]#

5.使用apply命令创建或者修改资源

1
2
3
4
5
6
7
8
9
10
#第一次执行则是创建
[root@master231:1 pods]# kubectl apply -f single-pods.yaml
pod/xiuxian created

[root@master231:1 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
xiuxian 1/1 Running 0 23s 10.100.2.5 worker233 <none> <none>
#再次执行则是修改资源,若没有修改资源清单则提示unchanged
[root@master231:1 pods]# kubectl apply -f single-pods.yaml
pod/xiuxian unchanged

响应式·基于标签管理POD资源

1.查看资源的标签

1
2
3
[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 5m54s <none>

2.给资源打标签

1
2
3
4
5
[root@master231:1 pods]# kubectl label pod xiuxian name=wanglin level=huashen
pod/xiuxian labeled
[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 9m17s level=huashen,name=wanglin

3.将资源移除标签

1
2
3
4
5
6
[root@master231:1 pods]# kubectl label pod xiuxian name-
pod/xiuxian unlabeled

[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 10m level=huashen

4.修改标签

1
2
3
4
5
6
7
8
9
10
11
[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 10m level=huashen

[root@master231:1 pods]# kubectl label pod xiuxian level=jindan --overwrite
pod/xiuxian labeled
[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 12m level=jindan

`--overwrite`不加上则报错level的值已存在

5.为所有Pod资源打标签

1
2
3
4
5
6
7
8
[root@master231:1 pods]# kubectl label pods --all app=xiuxian
pod/xiuxian labeled
pod/xiuxian-v1 labeled

[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 20m app=xiuxian,level=jindan
xiuxian-v1 1/1 Running 0 101s app=xiuxian

基于标签过滤资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 25m app=xiuxian,level=jindan
xiuxian-v1 1/1 Running 0 6m41s app=xiuxian,name=wanglin

#过滤key里含有name的标签
[root@master231:1 pods]# kubectl get pods --show-labels -l name
NAME READY STATUS RESTARTS AGE LABELS
xiuxian-v1 1/1 Running 0 6m21s app=xiuxian,name=wanglin

#过滤level=jindan的标签
[root@master231:1 pods]# kubectl get pods --show-labels -l level=jindan
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 26m app=xiuxian,level=jindan

#过滤level不等于jindan的标签
[root@master231:1 pods]# kubectl get pods --show-labels -l level!=jindan
NAME READY STATUS RESTARTS AGE LABELS
xiuxian-v1 1/1 Running 0 8m54s app=xiuxian,name=wanglin

7.基于标签删除资源

1
2
3
4
5
6
7
8
9
10
11
[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 30m app=xiuxian,level=jindan
xiuxian-v1 1/1 Running 0 11m app=xiuxian,name=wanglin

[root@master231:1 pods]# kubectl delete pods -l name
pod "xiuxian-v1" deleted

[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 31m app=xiuxian,level=jindan

声明式·修改资源标签

1.修改资源清单 记得修改后要应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@master231:1 pods]# cat single-pods.yaml 
apiVersion: v1
kind: Pod
metadata:
name: xiuxian
# 为资源打标签
labels:
name: wanglin
level: jindan
spec:
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1

#资源清单修改后要应用一下
[root@master231:1 pods]# kubectl apply -f single-pods.yaml
pod/xiuxian created

2.查看资源清单

1
2
3
[root@master231:1 pods]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian 1/1 Running 0 76s level=jindan,name=wanglin

修改标签和删除标签直接在资源清单里修改即可,修改后应用一下

响应式和声明式的区别

1.相同点

​ 都可以管理资源

2.不同点

  • 响应式修改后立即生效,但是资源被重新创建后又得重新执行相关命令;
  • 声明式修改需要先修改资源清单,在使用apply命令更新应用配置;

连接容器

1.连接指定pod执行命令

1
2
3
4
5
kubectl exec -it 资源名 command
[root@master231:0 kubernetes]# kubectl exec -it mysql80 -- mysql
···
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

2.连接同一个Pod的不同容器执行命令

1
2
3
4
5
6
[root@master231 pods]# kubectl exec -it multiple-wordpress -c db -- ls / -l

温馨提示:
1.kubectl exec和docker exec都是在容器中执行命令;
2.如果一个Pod有多个容器,则可以使用-c连接到指定的容器执行相应的命令;
3.如果一个Pod有多个容器,若不使用-c连接到指定容器执行命令,则默认连接到第一个容器;

案例

基于pod部署mysql数据库

1.编写资源清单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@master231:1 pods]# cat single-pods-mysql.yaml 
apiVersion: v1
kind: Pod
metadata:
name: mysql80
labels:
app: mysql
spec:
# 指定调度到哪个节点,该节点必须在etcd数据库中有记录
nodeName: worker233
containers:
- name: mysqld
image: mysql:8.0
env:
# 指定变量的名称,允许密码为空
- name: MYSQL_ALLOW_EMPTY_PASSWORD
# 指定变量的值
value: "yes"
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: wp_usre
- name: MYSQL_PASSWORD
value: "123"

2.运行msyql实例

1
2
[root@master231:0 kubernetes]# kubectl apply -f single-pods-mysql.yaml 
pod/mysql80 created

3.查看pod信息

1
2
3
4
5
[root@master231:0 kubernetes]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql80 0/1 ImagePullBackOff 0 83s 10.100.2.12 worker233

#创建节点worker233 ip 10.100.2.12

基于一个pod部署wordpress

1.编写资源清单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@master kubernetes]# cat wordpress.yaml 
apiVersion: v1
kind: Pod
metadata:
name: wordpress-pod
spec:
# 使用宿主机网络名称空间
hostNetwork: true
containers:
- name: db
image: mysql:8.0
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "yes"
- name: MYSQL_DATABASE
value: wp
- name: MYSQL_USER
value: wp_usre
- name: MYSQL_PASSWORD
value: '123'
- name: wp
image: wordpress:latest
imagePullPolicy: IfNotPresent
env:
- name: WORDPRESS_DB_HOST
value: "127.0.0.1"
- name: WORDPRESS_DB_NAME
value: wp
- name: WORDPRESS_DB_USER
value: wp_usre
- name: WORDPRESS_DB_PASSWORD
value: '123'

`保证宿主机80端口不被占用`

2.根据资源清单创建pod

1
[root@master kubernetes]# kubectl apply -f wordpress.yaml 

3.查看pod信息

1
2
3
4
5
6
7
8
[root@master kubernetes]# kubectl get pod  -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-565785f75c-tpjmj 1/1 Running 0 8h 10.2.2.2 node03 <none> <none>
nginx-pod 1/1 Running 0 8h 10.2.2.3 node03 <none> <none>
wordpress-pod 2/2 Running 0 26m 10.0.0.203 node03 <none> <none>

#pod创建节点ip为10.0.0.203
浏览器访问:100.0.203