文章目录
Namespace
Namespace是kubernetes系统中的一种非常重要资源,它的主要作用是用来实现多套环境的资源隔离或者多租户的资源隔离。
默认情况下,kubernetes集群中的所有的Pod都是可以相互访问的。但是在实际中,可能不想让两个Pod之间进行互相的访问,那此时就可以将两个Pod划分到不同的namespace下。kubernetes通过将集群内部的资源分配到不同的Namespace中,可以形成逻辑上的"组",以方便不同的组的资源进行隔离使用和管理。
可以通过kubernetes的授权机制,将不同的namespace交给不同租户进行管理,这样就实现了多租户的资源隔离。此时还能结合kubernetes的资源配额机制,限定不同租户能占用的资源,例如CPU使用量、内存使用量等等,来实现租户可用资源的管理。
kubernetes在集群启动之后,会默认创建几个namespace
[root@master ~]# kubectl get namespace
NAME STATUS AGE
default Active 10d
kube-flannel Active 10d
kube-node-lease Active 10d
kube-public Active 10d
kube-system Active 10d
- default:所有未指定Namespace的对象都会被分配在default命名空间
- kube-node-lease:集群节点之间的心跳维护,v1.13开始引入
- kube-public:此命名空间下的资源可以被所有人访问(包括未认证用户)
- kube-system:所有由Kubernetes系统创建的资源都处于这个命名空间
namespace资源的具体操作
查看
查看所有namespace
kubectl get namespace
或kubectl get ns
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 10d
kube-flannel Active 10d
kube-node-lease Active 10d
kube-public Active 10d
kube-system Active 10d
查看指定的ns
kubectl get ns [ns名称]
[root@master ~]# kubectl get ns default
NAME STATUS AGE
default Active 10d
指定输出格式
kubectl get ns [ns名称] -o [格式参数]
kubernetes格式参数比较常见的是wide、json、yaml
[root@master ~]# kubectl get ns default -o yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: "2022-11-17T08:12:18Z"
labels:
kubernetes.io/metadata.name: default
name: default
resourceVersion: "190"
uid: 112826bf-19d3-4c2c-9bca-c85112185655
spec:
finalizers:
- kubernetes
status:
phase: Active
查看ns详情
kubectl describe ns [ns名称]
[root@master ~]# kubectl describe ns default
Name: default
Labels: kubernetes.io/metadata.name=default
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
创建
创建namespace
kubectl create ns [ns名称]
[root@master ~]# kubectl create ns test
namespace/test created
删除
删除namespace
kubectl delete ns [ns名称]
[root@master ~]# kubectl delete ns test
namespace "test" deleted
配置方式
[root@master ~]# vim ns-test.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test
创建:kubectl create -f ns-test.yaml
[root@master ~]# kubectl create -f ns-test.yaml
namespace/test created
删除:kubectl delete -f ns-test.yaml
[root@master ~]# kubectl delete -f ns-test.yaml
namespace "test" deleted
Pod
Pod是kubernetes集群进行管理的最小单元,程序要运行必须部署在容器中,而容器必须存在于Pod中。Pod可以认为是容器的封装,一个Pod中可以存在一个或者多个容器。
kubernetes在集群启动之后,集群中的各个组件也都是以Pod方式运行的。
查看
[root@master ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-c676cc86f-9cmxp 1/1 Running 1 (112m ago) 10d
coredns-c676cc86f-kp4lc 1/1 Running 1 (112m ago) 10d
etcd-master.example.com 1/1 Running 1 (112m ago) 10d
kube-apiserver-master.example.com 1/1 Running 1 (112m ago) 10d
kube-controller-manager-master.example.com 1/1 Running 1 (112m ago) 10d
kube-proxy-b7g9s 1/1 Running 1 (112m ago) 10d
kube-proxy-fqz9h 1/1 Running 0 10d
kube-proxy-mfwzk 1/1 Running 0 10d
kube-scheduler-master.example.com 1/1 Running 1 (112m ago) 10d
创建并运行
kubernetes没有提供单独运行Pod的命令,都是通过Pod控制器来实现的
kubectl run (pod控制器名称) [参数]
--image 指定Pod的镜像
--port 指定端口
--namespace 指定namespace
[root@master ~]# kubectl run nginx --image=nginx:latest --port=80 --namespace test
pod/nginx created
查看pod基本信息
kubectl get pods -n [ns名称]
[root@master ~]# kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 5m47s
查看Pod的详细信息
kubectl describe pod [pod名称] -n [ns名称]
[root@master ~]# kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 5m47s
[root@master ~]# kubectl describe pod nginx -n test
Name: nginx
Namespace: test
Priority: 0
Service Account: default
Node: node1.example.com/192.168.159.101
Start Time: Mon, 28 Nov 2022 15:37:37 +0800
Labels: run=nginx
Annotations: <none>
Status: Running
IP: 10.244.1.3
IPs:
IP: 10.244.1.3
Containers:
nginx:
Container ID: containerd://799b7a73ce2445cf2331a009ad358f7a9f441c261b746a004e002862796ccfca
Image: nginx:latest
Image ID: docker.io/library/nginx@sha256:e209ac2f37c70c1e0e9873a5f7231e91dcd83fdf1178d8ed36c2ec09974210ba
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 28 Nov 2022 15:38:56 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-qgjgw (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-qgjgw:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m6s default-scheduler Successfully assigned test/nginx to node1.example.com
Normal Pulling 4m22s (x2 over 4m52s) kubelet Pulling image "nginx:latest"
Normal Pulled 3m46s kubelet Successfully pulled image "nginx:latest" in 35.640627087s
Normal Created 3m46s kubelet Created container nginx
Normal Started 3m46s kubelet Started container nginx
访问Pod
获取podIP
kubectl get pods -n [ns名称] -o wide
[root@master ~]# kubectl get pods -n test -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 9m12s 10.244.1.3 node1.example.com <none> <none>
访问pod
[root@master ~]# curl http://10.244.1.3:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
删除指定Pod
kubectl delete pod [pod名称] -n [ns名称]
[root@master ~]# kubectl delete pod nginx -n test
pod "nginx" deleted
删除Pod控制器
[root@master ~]# kubectl delete deploy nginx -n test
deployment.apps "nginx" deleted
配置操作
[root@master ~]# vim pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: test
spec:
containers:
- image: nginx:latest
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
创建:kubectl create -f pod-nginx.yaml
[root@master ~]# kubectl create -f pod-nginx.yaml
pod/nginx created
删除:kubectl delete -f pod-nginx.yaml
[root@master ~]# kubectl delete -f pod-nginx.yaml
pod "nginx" deleted
Label
Label是kubernetes系统中的一个重要概念。它的作用就是在资源上添加标识,用来对它们进行区分和选择。
Label的特点:
- 一个Label会以key/value键值对的形式附加到各种对象上,如Node、Pod、Service等等
- 一个资源对象可以定义任意数量的Label ,同一个Label也可以被添加到任意数量的资源对象上去
- Label通常在资源对象定义时确定,当然也可以在对象创建后动态添加或者删除
可以通过Label实现资源的多维度分组,以便灵活、方便地进行资源分配、调度、配置、部署等管理工作。
一些常用的Label 示例:
- 版本标签:
"version":"release"
,"version":"stable"
… - 环境标签:
"environment":"dev"
,"environment":"test"
,"environment":"pro"
- 架构标签:
"tier":"frontend"
,"tier":"backend"
标签定义完毕之后,还要考虑到标签的选择,这就要使用到Label Selector
Label用于给某个资源对象定义标识
Label Selector用于查询和筛选拥有某些标签的资源对象
当前有两种Label Selector:
- 基于等式的Label Selector
name = slave: 选择所有包含Label中key="name"且value="slave"的对象
env != production: 选择所有包括Label中的key="env"且value不等于"production"的对象 - 基于集合的Label Selector
name in (master, slave): 选择所有包含Label中的key="name"且value="master"或"slave"的对象
name not in (frontend): 选择所有包含Label中的key="name"且value不等于"frontend"的对象
标签的选择条件可以使用多个,此时将多个Label Selector进行组合,使用逗号,
进行分隔即可
命令方式
为pod资源打标签
[root@master ~]# kubectl label pod nginx version=1.0 -n test
pod/nginx labeled
为pod资源更新标签
[root@master ~]# kubectl label pod nginx version=2.0 -n test --overwrite
pod/nginx labeled
查看标签
[root@master ~]# kubectl get pod nginx -n test --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 62s version=2.0
筛选标签
[root@master ~]# kubectl get pod -n test -l version=2.0 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 13m version=2.0
[root@master ~]# kubectl get pod -n test -l version!=2.0 --show-labels
No resources found in test namespace.
删除标签
[root@master ~]# kubectl label pod nginx version- -n test
pod/nginx unlabeled
配置方式
[root@master ~]# vim pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: dev
labels:
version: "3.0"
env: "test"
spec:
containers:
- image: nginx:latest
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
[root@master ~]# kubectl apply -f pod-nginx.yaml
pod/nginx configured
Deployment
在kubernetes中,Pod是最小的控制单元,但是kubernetes很少直接控制Pod,一般都是通过Pod控制器来完成的。Pod控制器用于pod的管理,确保pod资源符合预期的状态,当pod的资源出现故障时,会尝试进行重启或重建pod。
在kubernetes中Pod控制器的种类有很多,现在只介绍Deployment
命令操作
kubectl create deployment 名称 [参数]
--image 指定pod的镜像
--port 指定端口
--replicas 指定创建pod数量
--namespace 指定namespace
创建
[root@master ~]# kubectl create deploy nginx --image=nginx:latest --port=80 --replicas=3 -n test
deployment.apps/nginx created
查看创建的Pod
[root@master ~]# kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 33m
nginx-cd55c47f5-5wxkv 1/1 Running 0 113s
nginx-cd55c47f5-p9z5c 1/1 Running 0 113s
nginx-cd55c47f5-qxnrc 1/1 Running 0 113s
查看deployment的信息
[root@master ~]# kubectl get deploy -n test
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 2m24s
[root@master ~]# kubectl get deploy -n test -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 3/3 3 3 3m17s nginx nginx:latest app=nginx
- UP-TO-DATE:成功升级的副本数量
- AVAILABLE:可用副本的数量
查看deployment的详细信息
[root@master ~]# kubectl describe deploy nginx -n test
Name: nginx
Namespace: test
CreationTimestamp: Mon, 28 Nov 2022 16:39:58 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-cd55c47f5 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m48s deployment-controller Scaled up replica set nginx-cd55c47f5 to 3
删除
[root@master ~]# kubectl delete deploy nginx -n test
deployment.apps "nginx" deleted
配置操作
[root@master ~]# vim deploy-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: test
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:latest
name: nginx
ports:
- containerPort: 80
protocol: TCP
```
创建:kubectl create -f deploy-nginx.yaml
```
[root@master ~]# kubectl create -f deploy-nginx.yaml
deployment.apps/nginx created
```
删除:kubectl delete -f deploy-nginx.yaml
```
[root@master ~]# kubectl delete -f deploy-nginx.yaml
deployment.apps "nginx" deleted
```