Deployment
- 在 Kubernetes 中,通过创建 Deployment ,可以创建应用程序(如同 Docker 的 image)的实例(如同 Docker 的容器),这个实例被包含在 Pod 的概念中,Pod 是 Kubernetes 中的最小管理单元。
- 在 Kubernetes 集群中创建 Deployment 之后,Deployment 将指示 Kubernetes 集群如何创建和更新应用程序的实例,Master 节点将应用程序实例调度到集群中的具体的节点上。
- 创建应用程序实例之后,Kubernetes 的 Deployment Controller 将会持续监控这些实例。如果运行这些实例的 Node 节点关机或者被删除,则 Kubernetes 的 Deployment Controller 将在集群中的资源最优的另一个 Node 节点上重新创建一个新的实例,这就提供了一种
自我修复机制
来解决机器故障或维护问题。
- 在容器编排之前的时代,各种安装脚本通常用于启动应用程序,但是却不能使得应用程序从机器故障中恢复。通过创建应用程序实例并确保它们在集群节点中的运行实例个数,Kubernetes 的 Deployment 提供了一种完全不同的方式来管理应用程序。
显示资源列表
创建资源
kubectl create deployment my-nginx --image=nginx --replicas=3 --port=80
- 在 Kubernetes 集群中创建一个名为
my-nginx
的 Deployment。 - 使用官方的
nginx
镜像启动容器。 - 创建 3 个副本的 Nginx 服务,每个副本都会监听端口 80。
执行后,Kubernetes 会自动管理这 3 个 Nginx Pod 的生命周期,确保有 3 个副本在运行,并处理 Pod 的故障恢复、负载均衡等工作。
获取类型为 Deployment 的资源列表
[root@k8s-master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 3/3 3 3 104s
获取类型为 Pod 的资源列表
kubectl get pod
获取类型为 Node 的资源列表
kubectl get node
显示所有名称空间下的 Deployment
[root@k8s-master ~]# kubectl get deployment -A
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
default my-nginx 3/3 3 3 10m
kube-system calico-kube-controllers 1/1 1 1 4h5m
kube-system coredns 2/2 2 2 5d16h
查看 kube-system 名称空间下的 Deployment
[root@k8s-master ~]# kubectl get deployment -n kube-system
NAME READY UP-TO-DATE AVAILABLE AGE
calico-kube-controllers 1/1 1 1 4h6m
coredns 2/2 2 2 5d16h
查看在名称空间下的资源对象
kubectl api-resources --namespaced=true
查询不在名称空间下的资源对象
理解:就是受 Kubernetes 集群统一调度,而不受到具体名称空间的约束
kubectl api-resources --namespaced=false
显示有关资源的详细信息
查询名称为 nginx-pod 的 Pod 的信息
[root@k8s-master ~]# kubectl describe pods nginxpod -n dev
Name: nginxpod
Namespace: dev
Priority: 0
Node: k8s-node2/192.168.58.233
Start Time: Mon, 30 Dec 2024 22:27:38 -0500
Labels: <none>
Annotations: cni.projectcalico.org/containerID: b822f79ad0acfadd0427662e1968bf8ced560a975a6ea45cdb1737368c7cb420
cni.projectcalico.org/podIP: 10.244.169.142/32
cni.projectcalico.org/podIPs: 10.244.169.142/32
Status: Running
IP: 10.244.169.142
IPs:
IP: 10.244.169.142
Containers:
nginx-container:
Container ID: docker://719d4b2ad0a44a96ae61503f8c872aca60379316379b4ba4ddea73607c85db81
Image: nginx:1.17.1
Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 30 Dec 2024 22:27:56 -0500
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-ww59k (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-ww59k:
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: <none>
显示名称为 nginx 的 Deployment 的信息
[root@k8s-master ~]# kubectl describe deployment nginx
Error from server (NotFound): deployments.apps "nginx" not found
[root@k8s-master ~]# kubectl describe deployment my-nginx
Name: my-nginx
Namespace: default
CreationTimestamp: Tue, 31 Dec 2024 00:41:13 -0500
Labels: app=my-nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=my-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=my-nginx
Containers:
nginx:
Image: nginx
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: my-nginx-7576957b7b (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 14m deployment-controller Scaled up replica set my-nginx-7576957b7b to 3
查询 Pod 中的容器的打印日志
查询名称为 mynginx 的 Pod 日志
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-7576957b7b-6rwqd 1/1 Running 0 16m
my-nginx-7576957b7b-92dh7 1/1 Running 0 16m
my-nginx-7576957b7b-ll8wd 1/1 Running 0 16m
[root@k8s-master ~]# kubectl logs -f my-nginx-7576957b7b-6rwqd
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/12/31 05:41:42 [notice] 1#1: using the "epoll" event method
2024/12/31 05:41:42 [notice] 1#1: nginx/1.27.3
2024/12/31 05:41:42 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/12/31 05:41:42 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
2024/12/31 05:41:42 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/12/31 05:41:42 [notice] 1#1: start worker processes
2024/12/31 05:41:42 [notice] 1#1: start worker process 29
2024/12/31 05:41:42 [notice] 1#1: start worker process 30
在 Pod 中的容器环境内执行命令
在名称为 nginx-pod 的容器中执行命令
[root@k8s-master ~]# kubectl exec -it my-nginx-7576957b7b-6rwqd -- /bin/bash
root@my-nginx-7576957b7b-6rwqd:/#
root@my-nginx-7576957b7b-6rwqd:/# pwd
/