1 k8s 部署一个nginx的pod
[root@k8s-master1 package]# cat nginx-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:stable
ports:
- containerPort: 80
###配置文件解析###
1.apiVersion当前配置格式的版本
2.kind是要创建的资源类型,这里是Deployment
3.metadata是该资源的元数据,name是必须的元数据项
4.spec部分是该Deployment的规格说明
5.replicas指明副本数量,默认为1
6.template定义pod的模块,这是配置文件的重要部分
7.metadata定义pod的元数据,至少要定义一个label,label的key和value可以任意指定
8.spec描述pod的规格,此部分定义pod中每一个容器的属性,name和image是必需的
[root@k8s-master1 package]# cat nginx-service.yml
kind: Service
apiVersion: v1
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
2 启动 pod,暴露端口
[root@k8s-master1 package]# kubectl apply -f nginx-deploymeht.yml
deployment.apps/nginx-deployment created
[root@k8s-master1 package]# kubectl apply -f nginx-service.yml
service/nginx-service created
3 查看 pod,查看svc(也叫service)
[root@k8s-master1 package]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 22 7d
nginx-deployment-5ff58d798d-r2k8b 1/1 Running 2 8m16s
nginx-deployment-5ff58d798d-zm9fn 1/1 Running 2 8m16s
[root@k8s-master1 package]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 3d21h
nginx-service NodePort 10.0.0.126 <none> 80:32153/TCP 7m43s
4 查看 nginx-deployment
[root@k8s-master1 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 12m
##如果要删除
kubectl delete deployment nginx-deployment 或者 kubectl delete -f nginx-deployment.yml
#查看更详细的信息
[root@k8s-master1 ~]# kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Mon, 06 Sep 2021 15:07:49 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:stable
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-deployment-5ff58d798d (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 14m deployment-controller Scaled up replica set nginx-deployment-5ff58d798d to 2
# 查看replicaset
[root@k8s-master1 ~]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
nginx-deployment-5ff58d798d 2 2 2 35m
通过信息可以看出创建了一个nginx-deployment-5ff58d798d,也验证了deployment是通过replicaset来管理pod的事实,也可以看出有俩个副本
## 查看详细的信息
[root@k8s-master1 ~]#
[root@k8s-master1 ~]# kubectl describe replicaset nginx-deployment-5ff58d798d
Name: nginx-deployment-5ff58d798d
Namespace: default
Selector: app=nginx,pod-template-hash=5ff58d798d
Labels: app=nginx
pod-template-hash=5ff58d798d
Annotations: deployment.kubernetes.io/desired-replicas: 2
deployment.kubernetes.io/max-replicas: 3
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/nginx-deployment
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
pod-template-hash=5ff58d798d
Containers:
nginx:
Image: nginx:stable
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 42m replicaset-controller Created pod: nginx-deployment-5ff58d798d-zm9fn
Normal SuccessfulCreate 42m replicaset-controller Created pod: nginx-deployment-5ff58d798d-r2k8b
查看pod
[root@k8s-master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 23 7d
nginx-deployment-5ff58d798d-r2k8b 1/1 Running 2 44m
nginx-deployment-5ff58d798d-zm9fn 1/1 Running 2 44m
#查看pod的详细信息
[root@k8s-master1 ~]# kubectl describe pod nginx-deployment-5ff58d798d-r2k8b
5 各个组件的协作
1.kubectl发送部署请求到API Server
2.Api Server 通知Controller Manager 创建一个 deployment资源
3.Scheduler执行调度任务,将俩个副本pod发送到k8s-node1和k8s-node2等
4.k8s-node1和k8s-node2上的kubectl在各自的节点上创建并运行pod
补充俩点:
应用的配置和当前状态信息保存在etcd中,执行kubectl get pod时,Api Server 会从etcd中读取这些数据
flannel会为每个pod都分配IP,因为没有创建service,所以目前kube-proxy还没参与进来
6 k8s部署nginx的pod过程
k8s部署pod过程
用户通过kubectl创建deployment
deployment创建replicaset
replicaset创建pod
|