k8s google sample - guestbook

时间:2022-04-06 23:46:08

Redis读写分离作为存储

PHP网页作为前端

github地址

https://github.com/kubernetes/kubernetes/blob/release-1.1/examples/guestbook/README.md

Create guest book sample on k8s


1. 创建Redis写端RC yaml

apiVersion: v1
kind: ReplicationController
metadata:
name: redis-master
labels:
name: redis-master
spec:
replicas:
selector:
name: redis-master
template:
metadata:
labels:
name: redis-master
spec:
containers:
- name: master
image: redis
ports:
- containerPort:

可以到开头的github地址下载dockfile,自己创建image

2. k8s创建RC redis master

kubectl create -f redis-master-controller.yaml

3. k8s创建 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: 

    targetPort: 

  selector:

    name: redis-master

4. k8s创建SVC

kubectl create -f redis-master-service.yaml

5. 创建Redis读端 RC redis slave yaml

apiVersion: v1

kind: ReplicationController

metadata:

  name: redis-slave

  labels:

    name: redis-slave

spec:

  replicas: 

  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

          # If your cluster config does not include a dns service, then to

          # instead access an environment variable to find the master

          # service's host, comment out the 'value: dns' line above, and

          # uncomment the line below.

          #value: env

        ports:

        - containerPort: 

示例文档中说明如果放在云端并且有DNS服务器可以写入dns,但是实验中是在局域网内,所以使用env

6. k8s创建redis slave RC

kubectl create -f redis-slave-controller.yaml

7. 创建redis slave svc yaml

apiVersion: v1

kind: Service

metadata:

  name: redis-slave

  labels:

    name: redis-slave

spec:

  ports:

    # the port that this service should serve on

  - port: 

  selector:

    name: redis-slave

8. k8s创建redis slave svc

kubectl create -f redis-slave-service.yaml

9. 创建php frontend rc yaml

apiVersion: v1

kind: ReplicationController

metadata:

  name: frontend

  labels:

    name: frontend

spec:

  replicas: 

  selector:

    name: frontend

  template:

    metadata:

      labels:

        name: frontend

    spec:

      containers:

      - name: frontend

        image: peter/php-frontend:v1

        env:

        - name: GET_HOSTS_FROM

          value: env

          # If your cluster config does not include a dns service, then to

          # instead access environment variables to find service host

          # info, comment out the 'value: dns' line above, and uncomment the

          # line below.

          # value: env

        ports:

        - containerPort: 

这是下载了github的dockfile,本地构建的镜像

k8s google sample - guestbook

10. k8s创建 php frontend rc

kubectl create -f frontend-controller.yaml

11. 创建php frontend svc yaml

apiVersion: v1

kind: Service

metadata:

  name: frontend

  labels:

    name: frontend

spec:

  # if your cluster supports it, uncomment the following to automatically create

  # an external load-balanced IP for the frontend service.

  type: NodePort

  ports:

    # the port that this service should serve on

    - port: 

      nodePort: 30001

  selector:

    name: frontend

打开节点机器30001端口

12. k8s创建php frontend svc

kubectl create -f frontend-service.yaml

查看k8s的pods svc rc

k8s google sample - guestbook

输入node机器的ip:30001即可访问

k8s google sample - guestbook

Scale


k8s执行命令

kubectl scale --current-replicas=2 --replicas=3 replicationcontrollers redis-slave

k8s google sample - guestbook

Rolling update


k8s执行命令

kubectl rolling-update frontend --image=peter/php-frontend:v3

k8s google sample - guestbook