3.1 下载镜像
https://hub.docker.com/u/kubeguide/
docker pull kubeguide/guestbook-redis-slave
docker pull kubeguide/guestbook-php-frontend
docker pull kubeguide/redis-master
docker pull docker.io/kubernetes/pause
# docker tag kubernetes/pause gcr.io/google_containers/pause:0.8.0
# docker tag gcr.io/google_containers/pause:0.8.0 gcr.io/google_containers/pause
#docker pull kubeguide/redis-master
#docker pull kubeguide/guestbook-redis-slave
#docker pull kubeguide/guestbook-php-frontend
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/kubeguide/guestbook-php-frontend latest 38658844a359 7 months ago 509.6 MB
docker.io/kubeguide/redis-master latest 423e126c2ad4 8 months ago 419.1 MB
docker.io/kubeguide/guestbook-redis-slave latest 00206e07dd92 8 months ago 109.5 MB
3.2 设置工作目录
#mkdir kube-guestbook
#cd kube-guestbook
3.3 创建redis-master Pod和服务
#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: docker.io/kubeguide/redis-master
ports:
- containerPort: 6379
创建redis-master Pod
#kubectl create -f redis-master-controller.yaml
#kubectl get pods
一开始pod在pending状态
[root@CentOS7 kube-guestbook]# kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-7i338 0/1 ContainerCreating 0 47s
第一次启动容器时间比较久,如果没什么问题,状态会更新为Running
[root@CentOS7 kube-guestbook]# kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-7i338 1/1 Running 0 11m
#vi redis-master-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
name: redis-master
spec:
ports:
# the port that this service should serve on
- port: 6379
targetPort: 6379
selector:
name: redis-master
创建redis-master-service服务
#kubectl create -f redis-master-service.yaml
#kubectl get services
[root@CentOS7 kube-guestbook]# kubectl get services
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
kubernetes 10.254.0.1 <none> 443/TCP <none> 8d
redis-master 10.254.137.9 <none> 6379/TCP name=redis-master 7s
3.4 创建redis-slave Pod和服务
#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: docker.io/kubeguide/guestbook-redis-slave
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 6379
创建redis-slave Pod
#kubectl create -f redis-slave-controller.yaml
#kubectl get rc
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS AGE
redis-master master docker.io/kubeguide/redis-master name=redis-master 1 26m
redis-slave slave docker.io/kubeguide/guestbook-redis-slave name=redis-slave 2 10s
# kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-7i338 1/1 Running 0 27m
redis-slave-jju81 1/1 Running 0 28s
redis-slave-yzzmd 1/1 Running 0 28s
# vi redis-slave-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
ports:
- port: 6379
selector:
name: redis-slave
创建redis-slave-service服务
#kubectl create -f redis-slave-service.yaml
#kubectl get services
# kubectl get services
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
kubernetes 10.254.0.1 <none> 443/TCP <none> 8d
redis-master 10.254.137.9 <none> 6379/TCP name=redis-master 13m
redis-slave 10.254.181.125 <none> 6379/TCP name=redis-slave 7s
3.5 创建frontend Pod和服务
# vi frontend-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
labels:
name: frontend
spec:
replicas: 3
selector:
name: frontend
template:
metadata:
labels:
name: frontend
spec:
containers:
- name: php-redis
image: docker.io/kubeguide/guestbook-php-frontend
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 80
创建frontend Pod
#kubectl create -f frontend-controller.yaml
#kubectl get rc
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS AGE
frontend php-redis docker.io/kubeguide/guestbook-php-frontend name=frontend 3 7s
redis-master master docker.io/kubeguide/redis-master name=redis-master 1 36m
redis-slave slave docker.io/kubeguide/guestbook-redis-slave name=redis-slave 2 9m
# kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-00drw 1/1 Running 0 2m
frontend-78d16 1/1 Running 0 2m
frontend-z2fmk 1/1 Running 0 2m
redis-master-7i338 1/1 Running 0 38m
redis-slave-jju81 1/1 Running 0 12m
redis-slave-yzzmd 1/1 Running 0 12m
# vim frontend-service.yaml
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
name: frontend
spec:
type: NodePort
ports:
- port: 80
nodePort:30001
selector:
name: frontend
创建frontend-service服务
#kubectl create -f frontend-service.yaml
#kubectl get services
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
frontend 10.254.131.102 nodes 80/TCP name=frontend 19s
kubernetes 10.254.0.1 <none> 443/TCP <none> 8d
redis-master 10.254.137.9 <none> 6379/TCP name=redis-master 30m
redis-slave 10.254.181.125 <none> 6379/TCP name=redis-slave 17m
3.6通过浏览器访问网页
访问主机30001端口,我们看到网页已经默认有一条Hello World!
3.7停止Pod和服务
kubectl stop rc redis-master
kubectl stop rc redis-slave
kubectl delete rc redis-slave
kubectl stop rc frontend
kubectl delete service redis-master
kubectl delete service redis-slave
kubectl delete service frontend
3.8 其他
kubectl get node 获取节点
kubectl describe node xxx 详细信息
网友评论