ReplicationController

新建一个pod_nginx.yml

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx

spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
          - containerPort: 80

然后运行如下

kubectl create -f pod_nginx.yml
kubectl get rc
NAMEDESIREDCURRENTREADYAGE
nginx3334m
kubectl get pods
NAMEREADYSTATUSRESTARTSAGE
nginx-89s9p1/1Running06m
nginx-8bjnb1/1Running06m
nginx-h49m51/1Running06m
kubectl delete pods nginx-89s9p
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-2mvs9 0/1 ContainerCreating 0 <invalid>
nginx-89s9p 0/1 Terminating 0 9m
nginx-8bjnb 1/1 Running 0 9m
nginx-h49m5 1/1 Running 0 9m

当我们删掉一个pod的时候,kube会自动帮我们创建另一个pod,从而达到动态的平衡

# 动态修改pod数量
kubectl scale rc nginx --replicas=2

ReplicaSet

新建一个pod_nginx.yml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
  labels:
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      name: nginx
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
          - containerPort: 80

执行

# 创建
kubectl create -f pod_nginx.yml

# 查看
kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx 3 3 3 2m

# 扩容
kubectl scale rs nginx --replicas=1

思过崖
65 声望19 粉丝

从转行到入门,记录我的自学成长之路