测试环境为一个master, 两个worker的k8s集群。系统为centos7.6 , k8s版本为v1.17.9 , docker版本为18.09.7
- 配置redis-master服务
cat > redis-master-deployment.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-master
labels:
app: redis
spec:
selector:
matchLabels:
app: redis
role: master
tier: backend
replicas: 1
template:
metadata:
labels:
app: redis
role: master
tier: backend
spec:
containers:
- name: master
image: kubeguide/redis-master
imagePullPolicy: IfNotPresent
ports:
- containerPort: 6379
EOF
Kubernetes中Service中起到了负载均衡器的作用,通过Proxy和Selector决定服务请求传递给后端提供服务的Pod,对外提供固定的IP,这样的话Redis Master Pod迁移变化也不会影响。
- 配置redis-master-service.yaml
cat > redis-master-service.yaml << EOF
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
EOF
部署
kubectl apply -f redis-master-deployment.yaml
kubectl apply -f redis-master-service.yaml
- 配置redis-slave-deployment
cat > redis-slave-deployment.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-slave
labels:
app: redis
spec:
selector:
matchLabels:
app: redis
role: slave
tier: backend
replicas: 1
template:
metadata:
labels:
app: redis
role: slave
tier: backend
spec:
containers:
- name: slave
image: kubeguide/guestbook-redis-slave
imagePullPolicy: IfNotPresent
env:
- name: GET_HOSTS_FROM
value: dns
ports:
- containerPort: 6379
EOF
- 配置redis-slave-service.yaml
cat > redis-slave-service.yaml << EOF
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
app: redis
role: slave
tier: backend
spec:
ports:
- port: 6379
selector:
app: redis
role: slave
tier: backend
EOF
启动redis-slave服务
kubectl apply -f redis-slave-deployment.yaml
kubectl apply -f redis-slave-service.yaml
- 配置前端frontend-deployment.yaml
cat > frontend-deployment.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
labels:
app: guestbook
spec:
selector:
matchLabels:
app: guestbook
tier: frontend
replicas: 1
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: kubeguide/guestbook-php-frontend
imagePullPolicy: IfNotPresent
env:
- name: GET_HOSTS_FROM
value: dns
ports:
- containerPort: 80
EOF
- 配置 frontend-service.yaml
cat > frontend-service.yaml << EOF
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30001
selector:
app: guestbook
tier: frontend
EOF
启动web服务
kubectl apply -f frontend-deployment.yaml
kubectl apply -f frontend-service.yaml
- 查看
[root@nginx guestbook]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
frontend-56465cb4c6-r4kxz 1/1 Running 0 24m 10.42.0.35 81.68.101.212 <none> <none>
redis-master-884889466-9jzrt 1/1 Running 0 25m 10.42.1.34 81.68.229.215 <none> <none>
redis-slave-55c9767c85-t96nf 1/1 Running 0 24m 10.42.1.35 81.68.229.215 <none> <none>
[root@nginx guestbook]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
frontend 1/1 1 1 25m
redis-master 1/1 1 1 25m
redis-slave 1/1 1 1 25m
[root@nginx guestbook]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
frontend NodePort 10.43.194.57 <none> 80:30001/TCP 25m app=guestbook,tier=frontend
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 4h8m <none>
redis-master ClusterIP 10.43.10.250 <none> 6379/TCP 25m app=redis,role=master,tier=backend
redis-slave ClusterIP 10.43.183.219 <none> 6379/TCP 25m app=redis,role=slave,tier=backend
暴露的端口是30001, 节点ip:30001可访问