多实例运行
参考文档:https://kubernetes.io/docs/tutorials/kubernetes-basics/scale/scale-intro/
1. 创建类型为 LoadBalancer的service
[weiheng@weihengminikube root]$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h
[weiheng@weihengminikube root]$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 13h
[weiheng@weihengminikube root]$ kubectl expose deployment/kubernetes-bootcamp --type="LoadBalancer" --port 8080
service/kubernetes-bootcamp exposed
[weiheng@weihengminikube root]$
Kubernetes 支持pod的自动伸缩,甚至缩减至0实例
Services 会通过 endpoints 持续监控运行中的 pod,确保流量仅发送到可用的pod
你可以在不停机的情况下进行滚动更新(我记得是有滚动更新、蓝绿发布、金丝雀发布、重新创建,这4种更新方式,这里官方并未提及,先做了解吧)
2. 扩展 pod 实例
#扩展至4个副本
[weiheng@weihengminikube root]$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
#查看 deployment,也扩展至了4个
[weiheng@weihengminikube root]$ kubectl get deployments
#查看 pod 也扩展至了4个
[weiheng@weihengminikube root]$ kubectl get pods -o wide
#查看副本数 -> NewReplicaSet: kubernetes-bootcamp-644c5687f4 (4/4 replicas created)
kubectl describe deployments/kubernetes-bootcamp
3. 检查负载均衡
[weiheng@weihengminikube root]$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: LoadBalancer
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.100.129.95
IPs: 10.100.129.95
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32167/TCP
Endpoints: 10.244.0.22:8080,10.244.0.25:8080,10.244.0.26:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[weiheng@weihengminikube root]$ export NODE_PORT="$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')"
[weiheng@weihengminikube root]$ echo NODE_PORT=$NODE_PORT
NODE_PORT=32167
[weiheng@weihengminikube root]$ curl http://"$(minikube ip):$NODE_PORT"
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-644c5687f4-jksvk | v=1
[weiheng@weihengminikube root]$
每一次请求,会路由到不通的 Pod 里,测试OK:
4. 收缩 pod 实例
kubectl scale deployments/kubernetes-bootcamp --replicas=2
kubectl get deployments
kubectl get pods -o wide