kubernetes默认创建的namespace
[root@k8s-master ~]# kubectl get namespace
NAME STATUS AGE
default Active 2d3h
kube-node-lease Active 2d3h
kube-public Active 2d3h
kube-system Active 2d3h
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get ns
也可以指定以json或yaml格式查看结果
[root@k8s-master ~]# kubectl get namespace default -o wide
NAME STATUS AGE
default Active 2d5h
[root@k8s-master ~]#
其中resource quota是针对命名空间做的资源限制。LimitRange resource是针对命名空间中每个组件做的资源限制
[root@k8s-master ~]# kubectl describe namespace default
Name: default
Labels: kubernetes.io/metadata.name=default
Annotations:
Status: Active
No resource quota.
No LimitRange resource.
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl create namespace dev
namespace/dev created
[root@k8s-master ~]#
该namespace下的其它资源也会被删除
[root@k8s-master ~]# kubectl delete ns dev
namespace "dev" deleted
[root@k8s-master ~]#
新建ns-dev.yaml,内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
[root@k8s-master ~]# kubectl create -f ns-dev.yaml
namespace/dev created
[root@k8s-master ~]# kubectl delete -f ns-dev.yaml
namespace "dev" deleted
[root@k8s-master ~]#
Pod是kubernetes集群进行管理的最小单元,程序要运行必须部署在容器中,而容器必须存在于Pod中,一个Pod中可以运行一个或多个容器。其中pause是根容器
查看K8s系统自己运行的Pod。pods资源加s不加s都一样
[root@k8s-master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-57d95cb479-5zppz 1/1 Running 1 (2d8h ago) 2d8h
calico-node-2m8xb 1/1 Running 1 (2d8h ago) 2d8h
calico-node-jnll4 1/1 Running 1 (2d8h ago) 2d8h
calico-node-v6zcv 1/1 Running 1 (2d8h ago) 2d8h
coredns-7f74c56694-snzmv 1/1 Running 1 (2d8h ago) 2d8h
coredns-7f74c56694-whh84 1/1 Running 1 (2d8h ago) 2d8h
etcd-k8s-master 1/1 Running 1 (2d8h ago) 2d8h
kube-apiserver-k8s-master 1/1 Running 1 (2d8h ago) 2d8h
kube-controller-manager-k8s-master 1/1 Running 1 (2d8h ago) 2d8h
kube-proxy-9gc7d 1/1 Running 1 (2d8h ago) 2d8h
kube-proxy-f9w7h 1/1 Running 1 (2d8h ago) 2d8h
kube-proxy-s8rwk 1/1 Running 1 (2d8h ago) 2d8h
kube-scheduler-k8s-master 1/1 Running 1 (2d8h ago) 2d8h
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl create ns dev
namespace/dev created
[root@k8s-master ~]# kubectl run my-nginx --image=nginx:latest --port=80 --namespace=dev
pod/my-nginx created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pods -n dev -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx 1/1 Running 0 70s 10.244.169.134 k8s-node2
可以查看Pod的启动日志
[root@k8s-master ~]# kubectl describe pod my-nginx -n dev
Name: my-nginx
Namespace: dev
Priority: 0
Node: k8s-node2/192.168.23.162
Start Time: Sat, 14 May 2022 22:03:47 +0800
Labels: run=my-nginx
Annotations: cni.projectcalico.org/containerID: 516ad8c4489408f6de6cb0558e266da6eac0600a6fa1ea907818856d8e5e98d5
cni.projectcalico.org/podIP: 10.244.169.134/32
cni.projectcalico.org/podIPs: 10.244.169.134/32
Status: Running
IP: 10.244.169.134
IPs:
IP: 10.244.169.134
Containers:
my-nginx:
Container ID: containerd://fcf04016250a93e27a07b5380d88ce878dc56da4a47107378dbd5d3bb3244d4f
Image: nginx:latest
Image ID: docker.io/library/nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Sat, 14 May 2022 22:04:03 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-srfct (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-srfct:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional:
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors:
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 99s default-scheduler Successfully assigned dev/my-nginx to k8s-node2
Normal Pulling 99s kubelet Pulling image "nginx:latest"
Normal Pulled 84s kubelet Successfully pulled image "nginx:latest" in 15.437873591s
Normal Created 84s kubelet Created container my-nginx
Normal Started 84s kubelet Started container my-nginx
[root@k8s-master ~]#
[root@k8s-master ~]# curl 10.244.169.134:80
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@k8s-master ~]#
[root@k8s-master ~]# kubectl logs -f my-nginx -n dev
[root@k8s-master ~]# kubectl delete pod my-nginx -n dev
pod "my-nginx" deleted
[root@k8s-master ~]#
新建pod-nginx.yaml,内容如下:
[root@k8s-master ~]# cat pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: dev
spec:
containers:
- image: nginx:latest
imagePullPolicy: IfNotPresent
name: nginx-container
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl create -f pod-nginx.yaml
pod/nginx-pod created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl delete -f pod-nginx.yaml
pod "nginx-pod" deleted
[root@k8s-master ~]#
虽然Namespace可以起到隔离,但是不同Namespace的Pod不能相互访问。Label作用就是在资源上添加标识,用来对它们进行区分和选择。实现资源的多纬度分组,以便灵活、方便地进行资源分配、调度、配置和部署等管理工作
Label的特点:
一些常用的Label标签示例如下:
Label的筛选需要用到Label Selector,共两种
基于等式的Label Selector
基于集合的Label Selector
标签的选择条件可以使用多个,此时将多个Label Selector进行组合,使用逗号,
进行分隔即可。
[root@k8s-master ~]# kubectl label pod my-nginx version=1.0 -n dev
pod/my-nginx labeled
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl label pod my-nginx version=2.0 -n dev --overwrite
pod/my-nginx labeled
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
my-nginx 1/1 Running 0 84s run=my-nginx,version=2.0
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -l version=2.0 -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
my-nginx 1/1 Running 0 117s run=my-nginx,version=2.0
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl label pod my-nginx version- -n dev
pod/my-nginx unlabeled
[root@k8s-master ~]#
新建pod-nginx.yaml,内容如下:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: dev
labels:
version: "3.0"
env: "test"
spec:
containers:
- image: nginx:latest
imagePullPolicy: IfNotPresent
name: nginx-container
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
[root@k8s-master ~]# kubectl create -f pod-nginx.yaml
pod/nginx-pod created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-pod 1/1 Running 0 10s env=test,version=3.0
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl delete -f pod-nginx.yaml
pod "nginx-pod" deleted
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl create ns dev
namespace/dev created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl create deployment my-nginx --image=nginx -n dev
deployment.apps/my-nginx created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl scale deployment my-nginx --replicas=4 -n dev
deployment.apps/my-nginx scaled
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get deploy -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 4/4 4 4 4m2s
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
my-nginx-7cddc5685c-5mtfx 1/1 Running 0 104s
my-nginx-7cddc5685c-8wj5d 1/1 Running 0 4m11s
my-nginx-7cddc5685c-hz258 1/1 Running 0 104s
my-nginx-7cddc5685c-zxrnh 1/1 Running 0 104s
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl describe deployment my-nginx -n dev
Name: my-nginx
Namespace: dev
CreationTimestamp: Sun, 15 May 2022 10:05:03 +0800
Labels: app=my-nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=my-nginx
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=my-nginx
Containers:
nginx:
Image: nginx
Port:
Host Port:
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets:
NewReplicaSet: my-nginx-7cddc5685c (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 4m32s deployment-controller Scaled up replica set my-nginx-7cddc5685c to 1
Normal ScalingReplicaSet 2m5s deployment-controller Scaled up replica set my-nginx-7cddc5685c to 4
[root@k8s-master ~]#
deployment控制器删除,deployment下的pod也会被删除
[root@k8s-master ~]# kubectl delete deployment my-nginx -n dev
deployment.apps "my-nginx" deleted
[root@k8s-master ~]#
新建nginx-deploy.yaml,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
run: nginx-pod
template:
metadata:
labels:
run: nginx-pod
spec:
containers:
- image: nginx
name: nginx-container
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
protocol: TCP
name: nginx-port
[root@k8s-master ~]# kubectl create -f nginx-deploy.yaml
deployment.apps/nginx-deploy created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl delete -f nginx-deploy.yaml
deployment.apps "nginx-deploy" deleted
[root@k8s-master ~]#
利用Deployment创建一组Pod提供高可用性的服务,每个Pod都会分配一个单独的IP地址,但却存在如下问题:
Service可以解决这个问题。Service可以看做是一组同类的Pod对外的访问接口,应用可以方便的实现服务发现和负载均衡
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl create deployment nginx-deploy --image=nginx --replicas=3 -n dev
deployment.apps/nginx-deploy created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl expose deployment nginx-deploy --name=nginx-svc --type=ClusterIP --port=80 --target-port=80 -n dev
service/nginx-svc exposed
[root@k8s-master ~]#
说明如下:
--port
参数是Service的端口,--target-port
是Pod中服务的端口--type=NodePort
参数说明:
能够K8s集群外部所在服务器访问,查看Service的信息如下:
[root@k8s-master ~]# kubectl get service -n dev -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx-svc NodePort 10.96.74.31 80:30182/TCP 6s app=nginx-deploy
[root@k8s-master ~]#
访问http://k8s集群任意节点IP:30182,都可以访问
[root@k8s-master ~]# kubectl get service -n dev -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx-svc ClusterIP 10.96.149.66 80/TCP 3m55s app=nginx-deploy
[root@k8s-master ~]#
[root@k8s-master ~]# curl 10.96.149.66:80
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@k8s-master ~]#
[root@k8s-master ~]# kubectl delete service nginx-svc -n dev
service "nginx-svc" deleted
[root@k8s-master ~]#
新建nginx-svc.yaml,内容如下。clusterIP不写会随机创建一个
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
namespace: dev
spec:
clusterIP: 10.96.68.58
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-deploy
type: ClusterIP
[root@k8s-master ~]# kubectl create -f nginx-svc.yaml
service/nginx-svc created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl delete -f nginx-svc.yaml
service "nginx-svc" deleted
[root@k8s-master ~]#