k8s-redis集群属性简单解释

练习的docker镜像都是从 kubeguide/xxx 权威指南中给的练习地址哈

redis-RC

[root@salt php-redis]# cat redis-master-controller.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-master
  labels:
    name: redis-master
spec:
  replicas: 1
  selector:
    name: redis-master
  template:
    metadata:
      labels:
        name: redis-master
    spec:
      containers:
      - name: master
        image: kubeguide/redis-master
        ports:
        - containerPort: 6379

kind 值为 ReplicationController 表示是创建的是一个RC
spec.selector是RC的pod选择器,监控和管理拥有这些标签(Lable)的pod实例,确保当前集群上始终有且仅有replicas个pod实例在运行

replicas=1 只运行1个(名为redis-master的)pod实例

当集群中运行的pod数量小于replicas时,RC会根据spec.template段定义的pod模板来生成一个新的pod实例,labels属性指定了该pod的标签,

这里的labels必须匹配RC的spec.selector

redis-service

[root@salt php-redis]# cat redis-master-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    name: redis-master
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    name: redis-master

metadata.name是Service的服务名(ServiceName)

spec.selector确定了选择哪些pod,这里定义为选择name=redis-master标签的pod

port属性定义了service的虚拟端口号,targetPort属性指定后端pod内容器应用的端口号

redis-slaveRC

[root@salt php-redis]# cat redis-slave-controller.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  replicas: 2
  selector:
    name: redis-slave
  template:
    metadata:
      labels:
        name: redis-slave
    spec:
      containers:
      - name: slave
        image: kubeguide/guestbook-redis-slave
        env:
        - name: GET_HOSTS_FROM
          value: env
        ports:
        - containerPort: 6379

镜像中的启动脚本

root@redis-slave-4d2zw:/data# cat /run.sh |grep -v "^#"|grep -v "^$"
if [[ ${GET_HOSTS_FROM:-dns} == "env" ]]; then
  redis-server --slaveof ${REDIS_MASTER_SERVICE_HOST} 6379
else
  redis-server --slaveof redis-master 6379
fi

redis-slave_service

[root@salt php-redis]# cat redis-slave-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  ports:
  - port: 6379
  selector:
    name: redis-slave

kubectl get 

[root@salt php-redis]# kubectl get pod 
NAME                 READY   STATUS    RESTARTS   AGE
redis-master-d5sqn   1/1     Running   0          93m
redis-slave-4d2zw    1/1     Running   0          11m
redis-slave-tbrzt    1/1     Running   0          11m
[root@salt php-redis]# kubectl get svc
NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes     ClusterIP   10.68.0.1      <none>        443/TCP    17h
redis-master   ClusterIP   10.68.153.64   <none>        6379/TCP   26m
redis-slave    ClusterIP   10.68.228.91   <none>        6379/TCP   5m47s

 

posted @ 2021-07-06 11:27  风儿飘  阅读(221)  评论(0编辑  收藏  举报