备注:本例是按照kubernetes 权威指南第二版 2.3 Guestbook演示实例:Hello World ! 整理
Guestbook 留言板系统部署架构图
备注: 本例中的3个Docker 镜像地址为:https://hub.docker.com/u/kubeguide
相关yaml 已经上传到git 点击我的git
首先先定义Service,然后定义一个RC来创建和控制关联的Pod,或者先定义RC来创建Pod,然后定义与之关联的Service
本例是先定义RC 在定义Service
[root@master yaml]# vi 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
创建完成之后,在master节点上发布到集群中
[root@master yaml]# kubectl create -f redis-master-controller.yaml
replicationcontroller "redis-master" created
用kubectl get 命令确认RC和Pod 创建成功
[root@master yaml]# kubectl get rc
NAME DESIRED CURRENT READY AGE
redis-master 1 1 1 8s
查看Pod
[root@master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-brh9j 1/1 Running 0 4m
redis-master Pod 已创建且正常运行后,在创建与之关联的Service
[root@master yaml]# vi 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
运行kubectl create 命令创建该service
[root@master yaml]# kubectl create -f redis-master-service.yaml
service "redis-master" created
运行kubectl get 命令确认Service 运行
[root@master yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 23h
mysql ClusterIP 10.107.118.174 3306/TCP 19h
myweb NodePort 10.100.179.98 3306:30001/TCP 16h
redis-master ClusterIP 10.107.69.32 6379/TCP 14s
tomcat-service NodePort 10.106.225.208 8080:31003/TCP 16h
已经成功启动了redis-master 服务,下面再创建redis-slave RC 和Service
[root@master yaml]# vi 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
同上,在master节点发布到集群中
[root@master yaml]# kubectl create -f redis-slave-controller.yaml
replicationcontroller "redis-slave" created
运行kubectl create 确认RC 和Pod 启动
[root@master yaml]# kubectl get rc
NAME DESIRED CURRENT READY AGE
redis-master 1 1 1 15m
redis-slave 2 2 2 41s
[root@master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-brh9j 1/1 Running 0 15m
redis-slave-zcdgd 1/1 Running 0 54s
redis-slave-ztznm 1/1 Running 0 54s
创建与redis-slave 相关的Service 服务
[root@master yaml]# vi redis-slave-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
ports:
- port: 6379
targetPort: 6379
selector:
name: redis-slave
运行kubectl create 命令发布该Service
[root@master yaml]# kubectl create -f redis-slave-service.yaml
service "redis-slave" created
运行kubectl create 确认 Service 运行
[root@master yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 1d
redis-master ClusterIP 10.107.69.32 6379/TCP 31m
redis-slave ClusterIP 10.111.164.104 6379/TCP 23s
接下来再创建frontendRC 和Service
[root@master yaml]# vi frontend-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
spec:
replicas: 3
selector:
name: frontend
template:
metadata:
labels:
name: frontend
spec:
containers:
- name: frontend
image: kubeguide/guestbook-php-frontend
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 80
发布
[root@master yaml]# kubectl create -f frontend-controller.yaml
replicationcontroller "frontend" created
运行kubectl get 确认 创建成功
[root@master yaml]# kubectl get rc
NAME DESIRED CURRENT READY AGE
frontend 3 3 3 22s
mysql 1 1 1 20h
myweb 5 5 5 17h
redis-master 1 1 1 43m
redis-slave 2 2 2 29m
[root@master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-f8g2l 0/1 Running 0 52s
frontend-jdhcg 0/1 Running 0 52s
frontend-r6wtr 0/1 Running 0 52s
redis-master-brh9j 1/1 Running 0 44m
redis-slave-zcdgd 1/1 Running 0 29m
redis-slave-ztznm 1/1 Running 0 29m
[root@master yaml]#
创建与frontend 相关联的Service 服务
[root@master yaml]# vi frontend-service.yaml
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
name: frontend
spec:
type: NodePort
ports:
- port: 80
nodePort: 30003
selector:
name: frontend
运行kubectl create 命令发布到集群中
[root@master yaml]# kubectl create -f frontend-service.yaml
service "frontend" created
运行kubectl get 命令确认frontend Service 运行
[root@master yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend NodePort 10.105.54.5 80:30003/TCP 8s
kubernetes ClusterIP 10.96.0.1 443/TCP 1d
redis-master ClusterIP 10.107.69.32 6379/TCP 41m
redis-slave ClusterIP 10.111.164.104 6379/TCP 10m
在web 上登录:10.10.9.11:30003 or 10.10.9.12:30003/10.10.9.12:30003都能访问如下页面