- Published on
Kubernetes 管理应用 | ReplicaSet | Autoscaling | Rolling Update
- Authors
- Name
- Shelton Ma
Manage Application
ReplicaSet
ReplicaSet provides high availability through redundancy, a ReplicaSet enables scaling by creating or deleting pods, you can create a ReplicaSet using the CLI or the YAML descriptor, a ReplicaSet always tries to match the actual state to the desired state.
A best practice
is to use a Deployment instead of a ReplicaSet directly, because a ReplicaSet is automatically created when you create a deployment.
kubectl scale deploy hello-kubernetes --replicas=3
kubectl delete/create pod hello-kubernetes-xxxx-xxx
Autoscaling
Cluster/node level or Pod level
Autoscaler types:
- Horizontal Pod Autoscaler(HPA): 通过根据负载自动调整 Pod 的数量来扩容
- Vertical Pod Autoscaler(VPA): 通过调整 Pod 的 CPU 和内存资源请求来进行扩容或缩容
- Cluster Autoscaler(CA): 集群扩容是通过自动添加或移除节点来扩展集群资源的容量
kubectl autoscale deploy hello-kubernetes --min=2 --max=5 --cpu-percent=50
Rolling Update
- All-at-once rollout/rollback: 通过 Recreate 策略实现的,所有旧的 Pod 会一次性被替换.
- One-at-a-time rollout/rollback, 通过 RollingUpdate 策略实现的,逐个替换 Pod 保证高可用性.
# update image
kubectl set image deployments/hello-kubernetes hello-kubernetes=upkar/hello-kubernetes:2.0
# deployments "hello-kubernetes" successfully rolled out
kubectl rollout status deployments/hello-kubernetes
# rollback
kubectl rollout undo deployments/hello-kubernetes
ConfigMaps and Secrets
A ConfigMap is an API object that stores non-confidential data in key-value pairs
# config by command line kubectl create ConfigMap my-config --from-literal=MESSAGE="hello world from first configmap" # in .yaml file env: -name: MESSAGE valueFrom: configMapKeyRef: name: my-config key: MESSAGE #-------------------------------------------------------------------# # config from properties file instead of listing those variables one by one on the command line kubectl create ConfigMap my-config --from-file=my.properties # in my.properties MESSAGE=hello world from my.properties file # in .yaml file env: -name: MESSAGE valueFrom: configMapKeyRef: name: my-config key: my.properties #-------------------------------------------------------------------# # config from yaml file # my-config.YAML apiVersion: v1 data: my.properties: MESSAGE=hello world from my.properties file kind: ConfigMap metadata: name: my-config name-space: default # kubectl apply kubectl apply -f my-config.YAML kubectl describe cm my-config
Secret is used to provide sensitive information to your application
kubectl create secret generic api-creds --from-literal=key=mysupersecretapikey kubectl get secret kubectl describe secret api-creds # output in YAML format kubectl get secret api-creds -o YAML # use # in .yaml file env: -name: API_CREDS valueFrom: secretKeyRef: name: api-creds key: key #-------------------------------------------------------------------# # config by using volume mounts # secrets by mount as file: /etc/api/api-creds spec: containers: - name: hello-kubernetes image: upkar/hello-kubernetes ports: - containerPort: 8080 volumeMounts: - name: api-creds mountPath: "/etc/api" readOnly: true volumes: - name: api-creds secret: secretName: api-creds
Service Binding
Service binding is the process needed to consume external Services or backing Services, including REST APIs, databases, and event buses in our applications