Kubernetes部署GuestBook
# Kubernetes部署GuestBook
# 掌握要点
(1)安装redis集群
(2)定义service的Yaml文件
(3)创建service
# 什么是GuestBook
Guestbook它是留言板系统,利用Redis系统来实现消息的推送和接收,我们在本节学习部署Redis Server节点的部署。
# 1.安装Redis集群
⚫首先我们需要部署一个Redis的Master集群和。
⚫利用ReplicationController部署redis集群,作为Pod的控制器。
[root@node1 go]# cat redis-master.yaml
apiVersion: v1
kind: ReplicationController
metadata:
labels:
name: redis-master
name: redis-master
spec:
replicas: 1
selector:
name: redis-master
template:
metadata:
labels:
name: redis-master
spec:
containers:
- image: kubeguide/redis-master
name: redis-master
ports:
- containerPort: 6379
[root@node1 go]# cat redis-slave.yaml
apiVersion: v1
kind: ReplicationController
metadata:
labels:
name: redis-slave
name: redis-slave
spec:
replicas: 2
selector:
name: redis-slave
template:
metadata:
labels:
name: redis-slave
spec:
containers:
- image: kubeguide/guestbook-redis-slave
name: slave
ports:
- containerPort: 6379
env:
- name: GET_HOSTS_FROM
value: env
# 2.检查当前状态
⚫运行两个RC的pod控制器yaml文件。
⚫检查当前的控制器副本。
[root@node1 go]# kubectl apply -f redis-master.yaml
[root@node1 go]# kubectl apply -f redis-slave.yaml
[root@node1 go]# kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-bxnq2 1/1 Running 0 10m
frontend-swhcx 1/1 Running 0 10m
redis-master-bjg8c 1/1 Running 0 16m
redis-slave-b94sm 1/1 Running 0 14m
redis-slave-j8n9v 1/1 Running 0 14m
# 3.暴露端口服务
⚫创建Service服务用于暴露端口。
⚫创建Serivice服务使用ClusterIP的模式,暴露Redis的6379端口。
[root@node1 go]# cat service-master.yaml
apiVersion: v1
kind: Service
metadata:
labels:
name: redis-master
name: redis-master
spec:
ports:
- name: redis
port: 6379
targetPort: 6379
selector:
name: redis-master
type: ClusterIP
[root@node1 go]# cat service-slave.yaml
apiVersion: v1
kind: Service
metadata:
labels:
name: redis-slave
name: redis-slave
spec:
ports:
- name: redis
port: 6379
targetPort: 6379
selector:
name: redis-slave
type: ClusterIP
创建之后查看当前的service服务端口状态。
[root@node1 go]# kubectl apply -f service-master.yaml
[root@node1 go]# kubectl apply -f service-slave.yaml
[root@node1 go]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29h
redis-master ClusterIP 10.97.74.242 <none> 6379/TCP 18m
redis-slave ClusterIP 10.110.38.75 <none> 6379/TCP 15m
# 4.部署Frontend页面
⚫安装Frontend集群
⚫关联service服务
⚫访问界面
⚫使用RC创建Pod控制器
[root@node1 go]# cat frontend.yaml
apiVersion: v1
kind: ReplicationController
metadata:
labels:
name: frontend
name: frontend
spec:
replicas: 2
selector:
name: frontend
template:
metadata:
labels:
name: frontend
spec:
containers:
- image: kubeguide/guestbook-php-frontend
name: slave
ports:
- containerPort: 80
env:
- name: GET_HOSTS_FROM
value: env
[root@node1 go]# kubectl apply -f frontend.yaml
[root@node1 go]# kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-bxnq2 1/1 Running 0 15m
frontend-swhcx 1/1 Running 0 15m
redis-master-bjg8c 1/1 Running 0 21m
redis-slave-b94sm 1/1 Running 0 19m
redis-slave-j8n9v 1/1 Running 0 19m
# 5.对外暴露服务端口
⚫frontend Pod已创建且正常运行后,在创建与之关联的Service。
⚫上面的Redis集群使用的是ClusterIP的方式,这里使用NodePort的方式对外暴露。
[root@node1 go]# cat service-frontend.yaml
apiVersion: v1
kind: Service
metadata:
labels:
name: frontend
name: frontend
spec:
ports:
- name:
port: 80
nodePort: 30003
selector:
name: frontend
type: NodePort
[root@node1 go]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend NodePort 10.107.237.70 <none> 80:30003/TCP 14m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 30h
redis-master ClusterIP 10.97.74.242 <none> 6379/TCP 22m
redis-slave ClusterIP 10.110.38.75 <none> 6379/TCP 19m
# 6.访问页面
⚫通过http://192.168.1.15:30003访问。
上次更新: 2023/11/28, 22:03:59