apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
app: redis
role: master
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: master
tier: backend
查询服务列表以验证Redis主服务正在运行:
[root@aniu-k8s guestbook]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6d
redis-master ClusterIP 10.109.193.22 <none> 6379/TCP 6s
虽然Redis master是一个单独的pod,但是可以通过添加副本Redis Slaves来使其高度可用以满足流量需求
创建Redis Slaves部署部署根据清单文件中设置的配置进行缩放。在这种情况下,Deployment对象指定两个副本
如果没有任何副本正在运行,则此部署将在您的容器群集上启动两个副本。相反,如果有两个以上的副本正在运行,则会缩小直到两个副本正在运行
从redis-slave-deployment.yaml文件应用Redis Slave部署:
[root@aniu-k8s guestbook]# kubectl apply -f redis-slave-deployment.yaml
deployment "redis-slave" created
redis-slave-deployment.yam
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
app: redis
role: master
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: master
tier: backend
[root@aniu-k8s guestbook]# cat redis-slave-deployment.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: redis-slave
spec:
selector:
matchLabels:
app: redis
role: slave
tier: backend
replicas: 2
template:
metadata:
labels:
app: redis
role: slave
tier: backend
spec:
containers:
- name: slave
image: gcr.io/google_samples/gb-redisslave:v1
resources:
requests:
cpu: 100m
memory: 100Mi
env:
- name: GET_HOSTS_FROM
value: dns
# Using `GET_HOSTS_FROM=dns` requires your cluster to
# provide a dns service. As of Kubernetes 1.3, DNS is a built-in
# service launched automatically. However, if the cluster you are using
# does not have a built-in DNS service, you can instead
# instead access an environment variable to find the master
# service's host. To do so, comment out the 'value: dns' line above, and
# uncomment the line below:
# value: env
ports:
- containerPort: 6379
查询Pod的列表以验证Redis Slave Pod是否正在运行: