de9542d91c7d5cac451d854ef2e1a3cb.png

K8S 基本组件 - Deployment 和 Servcie

先创建一个ClusterIP服务

创建deployment

kubectl run nginx --image katacoda/docker-http-server:latest --replicas 3

d5619f14c018aa134367f4ca0d9a20d4.png

列出deployment

kubectl get deployment

2deb193869edf421977054d9c6936311.png

观察deployment

kubectl describe deployment nginx

6c6db1ea5c542d12369427f4df5fc658.png

列出replicaset

kubectl get replicaset

3beeca431c6b23875a34e90d84953343.png

观察replicaset

kubectl describe replicaset nginx-******

ac6ed93594dee2f878034db6facd6e7b.png

列出pod

kubectl get pod -o wide

b138a04ffadb3be4df5e8515aa047c34.png

使用ClusterIP的方式发布服务

kubectl expose deployment nginx --port 80 --target-port 80 --type ClusterIP --selector=run=nginx --name nginx

列出服务

kubectl get service

051a817ee9ef1f06b9edb4bff5d099ca.png

观察service

kubectl describe service nginx

b5a5a47e40cb25d80a1f13f1bf5f43a7.png

根据上条命令观察到的clusterip访问服务,同时对比此前观察的pod id

curl http://clusterip

fe049f7aba114717b5ee5854084f3e42.png

重复n次,可以观察三个pod之间的负载均衡效果

扩展服务到5个副本

kubectl scale deployment nginx --replicas=5

8b315c20e3dd1772865a0eee00b0fef1.png

观察deployment变化情况

kubectl describe deployment nginx

观察服务变化情况

kubectl describe service nginx

6e11cc0a0d1989caf5b7c3bcea6d63c1.png

观察pod变化情况

kubectl get pod -o wide

5fedc0e307f6487892d272c7d2850c27.png

收缩服务到2个副本

kubectl scale deployment nginx --replicas=2

bbf43e8f861f58852016f9b3236c87f3.png

列出replicaset

kubectl get replicaset

6f7f76ed840c83b278e8eb1e7695dc12.png

观察replicaset变化

kubectl describe replicaset nginx-******

6020c46332921d13fd9f2e14498ea69e.png

观察pod情况

kubectl get pod -o wide

d8b534af64cef4272a08dad21250e106.png

删除服务

kubectl delete service nginx

删除部署

kubectl delete deployment nginx

22759612325bd3229ea8a840ffc515a6.png

再来创建一个 NodePort 服务

创建 deployment

kubectl run simple-web --replicas=3 --image=katacoda/docker-http-server:latest --port=80

0844c8f26930c5acb8b4a1d8ad5c16b5.png

列出pod

kubectl get pod -o wide

beb6c358f8c3617cf0b114269867139b.png

发布 service

kubectl expose deployment simple-web --type=NodePort --name=simple-web

列出 service

kubectl get service

aff6d965ca818de04f2ef7e3922f643b.png

观察service

kubectl describe service simple-web

07d7b4963b43bb2079a1750640a0f7dd.png

如命令回显所示,该服务的NodePort为31206

使用浏览器访问http://hostip:nodeprot

abba424f8356fd1d78d2dc695cebba7c.png

0c259a8c8b0d76823c89c38e0ab667f8.png

d88c2229acb66e9ddce1b7f78a5bcf0d.png

Ctrl F5 n次,可以观察三个pod之间的负载均衡效果

删除 service

kubectl delete service simple-web

删除 deployment

kubectl delete deployment simple-web

4063fdfcaab74572bec0179512c56294.png
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐