【k8s】使⽤k8s部署⼀个简单的nginx服务名词解释
Namespace 表⽰命名空间
Deployment 表⽰pod发布
Service 表⽰多个pod做为⼀组的集合对外通过服务的表⽰
kubectl 是k8s的命令⾏操作命令,可以创建和更新,删除,列表和查详情等⼀系列的操作
部署步骤
同样的⽅法将deployment改成service,可以实现对service的增删该查等操作。
⼀、创建命名空间namespace
1. 创建命名空间的yaml⽂件 nginx-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: nginx
labels:
name: nginx
配置说明:
kind:Namespace 表⽰yaml⽂件创建的是命名空间
metadata表⽰命名空间的元信息
metadata.name 是命名空间的名称取值nginx
metadata.labels 是命名空间的标签 name=nginx
2. 创建命名空间nginx
kubectl create -f nginx-namespace.yaml
3. 查询命名空间
# 查询所有命名空间,可以看到新创建的命名空间nginx
➜ kube kubectl get namespaces
NAME STATUS AGE
default Active 85d
kube-node-lease Active 85d
kube-public Active 85d
kube-system Active 85d
nginx Active 8d
4. 查询命名空间nginx详情
➜ kube kubectl describe namespace nginx
Name: nginx
Labels: name=nginx
Annotations: <none>
Status: Active
总结:
从命名空间详情中可以看到如下信息
nodeselector命名空间名称: nginx
命名空间的标签:name=nginx
命名空间状态:Active 表⽰命名空间活跃
⼆、创建nginx的发布Deployment 1. 创建deployment的yaml⽂件 nginx-deployment.yaml ➜ kube cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx-deployment1
namespace: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.14.0
ports:
- containerPort: 80
name: nginx
volumeMounts:
- name: conf
mountPath: /etc/f
- name: log
mountPath: /var/log/nginx
- name: html
mountPath: /etc/nginx/html
tolerations:
- key: "key"
operator: "Equal"
value: "nginx"
effect: "NoSchedule"
volumes:
-
name: conf
hostPath:
path: /Users/w/kube/nginx/f
- name: log
hostPath:
path: /Users/w/kube/nginx/logs
type: Directory
- name: html
hostPath:
path: /Users/w/kube/nginx/html
type: Directory
配置说明:
kind: Deployment表⽰yaml⽂件创建的是⼀个Deployment发布
metadata表⽰这个deployment的元信息
metadata.name 是deployment的名称 nginx-deployment1
metadata.labels 是deployment的标签即:app=nginx
metadata.namespace 是deployment的命名空间,此处选择的是第⼀步创建的命名空间nginx
spec: 表⽰deployment的详细参数配置说明
2. 创建deployment的nginx
kubectl create -f nginx-deployment.yaml
3. 查询nginx命名空间下的pods
# ➜ kube kubectl get pods -n nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment1-6cb86fb6b7-kkpbw 1/1 Running 5 7d20h
可以看到有⼀个nginx-deployment的pods,启动了1个pods运⾏Running状态
可以通过修改如上yaml⽂件的plicas启动多个pods,
增加pods节点从1个到3个如下
# ➜ kube kubectl get pods -n nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment1-6cb86fb6b7-kkpbw 1/1 Running 5 7d20h
nginx-deployment1-6cb86fb6b7-txkkj 0/1 ContainerCreating 0 1s
nginx-deployment1-6cb86fb6b7-ztt2t 0/1 ContainerCreating 0 1s
如上可以看到有两个新的pod处于容器创建中状态,容器创建成功后,状态会转移到Running,READY也会变成1/1
减少pods节点从3个到1个如下
# ➜ kube kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment1 configured
# ➜ kube kubectl get pods -n nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment1-6cb86fb6b7-d6z6l 1/1 Terminating 0 98s
nginx-deployment1-6cb86fb6b7-kkpbw 1/1 Running 5 7d20h
nginx-deployment1-6cb86fb6b7-qtx2v 1/1 Terminating 0 98s
如上所⽰,有两个pod状态是终⽌中,只剩余⼀个处于Running状态
4. 查询命名空间nginx下的发布deployment
# ➜ kube kubectl describe deployment nginx -n nginx
Name: nginx-deployment1
Namespace: nginx
CreationTimestamp: Thu, 30 Sep 202111:46:53 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
# ...
可以看到pods节点是1个。
5. 查询命名空间nginx下的发布pod详情
# ➜ kube kubectl describe pod nginx-deployment1-6cb86fb6b7-kkpbw -n nginx
Name: nginx-deployment1-6cb86fb6b7-kkpbw
Namespace: nginx
Priority: 0
Node: docker-desktop/192.168.65.4
Start Time: Thu, 30 Sep 202115:12:48 +0800
Labels: app=nginx
pod-template-hash=6cb86fb6b7
Annotations: <none>
Status: Running
IP: 10.1.0.170
# ...
6. 删除⼀个deployment的pod节点
# ➜ kube kubectl delete pod nginx-deployment1-6cb86fb6b7-txkkj -n nginx
pod "nginx-deployment1-6cb86fb6b7-txkkj" deleted
三、创建nginx的服务Service
1. 创建service的yaml⽂件 nginx-service.yaml
# ➜ kube cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx-deployment1
namespace: nginx
spec:
ports:
- port: 9000
name: nginx-service80
protocol: TCP
targetPort: 80
nodePort: 31090
selector:
app: nginx
type: NodePort
配置说明:
kind: Service表⽰yaml⽂件创建的是⼀个Service
metadata表⽰这个Service的元信息
metadata.name 是Service的名称 nginx-deployment1
metadata.labels 是Service的标签即:app=nginx
metadata.namespace 是Service的命名空间,此处选择的是第⼀步创建的命名空间nginx
sepc是Service的详细配置说明
sepc.selector 表⽰这个Service是将带标签的哪些pods做为⼀个集合对外通过服务
sepc.ports.port 是Service绑定的端⼝
sepc.ports.name: nginx-service80 表⽰Service服务的名称
sepc.ports.protocol: TCP 表⽰Service转发请求到容器的协议是TCP,我们部署的http的nginx服务,因此选择协议为TCP sepc.ports.targetPort: 80 表⽰Service转发外部请求到容器的⽬标端⼝80,即deployment的pod容器对外开放的容器端⼝80 dePort: 31090 表⽰Service对外开放的节点端⼝
2. 创建⼀个服务
# ➜ kube kubectl create -f nginx-service.yaml
service/nginx-deployment1 created
即可以使⽤浏览器打开这个端⼝的页⾯,如下
4. 查询服务列表
# ➜ kube kubectl get services -n nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-deployment1 NodePort 10.96.92.3 <none> 9000:31090/TCP 34s
5. 查询服务详情
# ➜ kube kubectl describe service nginx-deployment1 -n nginx
Name: nginx-deployment1
Namespace: nginx
Labels: app=nginx
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP: 10.96.92.3
LoadBalancer Ingress: localhost
Port: nginx-service80 9000/TCP
TargetPort: 80/TCP
NodePort: nginx-service80 31090/TCP
Endpoints: 10.1.0.170:80,10.1.0.176:80,10.1.0.178:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
6. 删除⼀个service服务
# ➜ kube kubectl delete services nginx-deployment1 -n nginx
service "nginx-deployment1" deleted
如上就是通过k8s简单部署⼀个nginx服务的步骤
其中包括如下步骤
命名空间的创建
标签的增加
deployment的pods发布,增加和减少pods的⽅法
节点端⼝转发的Service创建
删除⼀个服务
删除⼀个pod
done.
祝玩的开⼼~
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论