十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
vi nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
[root@kubm-02 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-756fb87568-jwgp8 1/1 Running 0 64s
my-nginx-756fb87568-vshxc 1/1 Running 0 64s
[root@kubm-02 ~]#
[root@kubm-02 ~]# kubectl get pods -l run=my-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-756fb87568-jwgp8 1/1 Running 0 2m12s 10.244.3.101 kubnode-01
my-nginx-756fb87568-vshxc 1/1 Running 0 2m12s 10.244.4.123 kubnode-02
[root@kubm-02 ~]# kubectl get pods -l run=my-nginx -o yaml | grep podIP
podIP: 10.244.3.101
podIP: 10.244.4.123
Kubernetes Service 从逻辑上定义了运行在集群中的一组 Pod,这些 Pod 提供了相同的功能。 当每个 Service 创建时,会被分配一个唯一的 IP 地址(也称为 clusterIP)。 这个 IP 地址与一个 Service 的生命周期绑定在一起,当 Service 存在的时候它也不会改变。 可以配置 Pod 使它与 Service 进行通信,Pod 知道与 Service 通信将被自动地负载均衡到该 Service 中的某些 Pod 上。
我们是于2013年成立的成都网站建设公司,提供网站建设,电商网站设计开发,外贸营销网站建设,响应式网页设计,微信小程序开发、等服务。为客户创造有价值的品牌营销体验,让互联网提升企业的竞争力!
kubectl expose 命令为 2个 Nginx 副本创建一个 Service:
[root@kubm-02 ~]# kubectl expose deployment/my-nginx
service/my-nginx exposed
[root@kubm-02 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-nginx ClusterIP 10.245.206.203 80/TCP 2m36s
[root@kubm-02 ~]# kubectl describe svc my-nginx
Name: my-nginx
Namespace: default
Labels:
Annotations:
Selector: run=my-nginx
Type: ClusterIP
IP: 10.245.206.203
Port: 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.3.101:80,10.244.4.123:80
Session Affinity: None
Events:
kubectl describe po my-nginx # 查看my-nginx pod的详细状态
kubectl describe rs my-nginx # 查看my-nginx replica set的详细状态
kubectl describe deployment my-nginx # 查看my-nginx deployment的详细状态
Kubernetes 支持两种主要的服务发现模式 —— 环境变量和 DNS。前者在单个节点上可用使用,然而后者必须使用 kube-dns 集群插件。
[root@kubm-02 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-756fb87568-jwgp8 1/1 Running 0 23m
my-nginx-756fb87568-vshxc 1/1 Running 0 23m
[root@kubm-02 ~]# kubectl exec my-nginx-756fb87568-jwgp8 -- printenv | grep SERVICE
KUBERNETES_SERVICE_HOST=10.245.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
Kubernetes 提供 DNS ,如果它在集群中处于运行状态,可以通过如下命令来检查:
[root@kubm-02 ~]# kubectl get services kube-dns --namespace=kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.245.0.10 53/UDP,53/TCP,9153/TCP 11d
[root@kubm-02 ~]# kubectl run curl --image=radial/busyboxplus:curl -i --tty
[ root@curl-6bf6db5c4f-96nhg:/ ]$ nslookup my-nginx
Server: 10.245.0.10 《==== dns 服务器
Address 1: 10.245.0.10 kube-dns.kube-system.svc.cluster.local
Name: my-nginx
Address 1: 10.245.206.203 my-nginx.default.svc.cluster.local 《==== my-nginx服务的集群IP地址
[ root@curl-6bf6db5c4f-96nhg:/ ]$
[ root@curl-6bf6db5c4f-96nhg:/ ]$ curl my-nginx
........
Welcome to nginx!
.........
[ root@curl-6bf6db5c4f-96nhg:/ ]$ curl 10.245.206.203
........
Welcome to nginx!
......
收缩服务
[root@kubm-02 ~]# kubectl scale deployment my-nginx --replicas=0
验证
[root@kubm-02 ~]# kubectl get pods -o wide
No resources found.
扩容服务至2节点
[root@kubm-02 ~]# kubectl scale deployment my-nginx --replicas=2
deployment.extensions/my-nginx scaled
验证
[root@kubm-02 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-756fb87568-gmgfq 0/1 ContainerCreating 0 1s kubnode-02
my-nginx-756fb87568-gvhbm 0/1 ContainerCreating 0 1s kubnode-01
#直接使用配置文件删除
[root@kubm-02 ~]# kubectl delete -f nginx.yaml
deployment.apps "my-nginx" deleted
#删除服务
[root@kubm-02 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.245.0.1 443/TCP 11d
my-nginx ClusterIP 10.245.206.203 80/TCP 48m
[root@kubm-02 ~]# kubectl delete svc my-nginx
service "my-nginx" deleted
[root@kubm-02 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.245.0.1 443/TCP 11d