From official examples

Kubernetes. IO/docs/tutori…

Build and deploy a simple (non-production-ready), multi-tier Web application using Kubernetes and Docker

This example consists of the following components: single instance Redis multiple Web front-end instances for storing guestbook entries

Operation:

  • Start a Redis leader.

  • Start two Redis followers.

  • Start the message board front end.

  • Expose and view the front-end services.

  • Clean up.

Start the Redis database cluster

Create a Redis Leader application based on deployment

#https://k8s.io/examples/application/guestbook/redis-leader-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: redis-leader labels: app: redis role: leader tier: backend spec: replicas: 1 selector: matchLabels: app: redis template: metadata: labels: app: redis role: leader tier: backend spec: containers: - name: IO /redis:6.0.5" Resources: requests: CPU: 100M Memory: 100Mi Ports: -containerPort: 6379 kubectl apply -f redis-leader-deployment.yaml kubectl get pods kubectl logs -f deployment/redis-leaderCopy the code

Create the Redis Leader Service

# https://k8s.io/examples/application/guestbook/redis-leader-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-leader
  labels:
    app: redis
    role: leader
    tier: backend
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
    role: leader
    tier: backend

kubectl apply -f redis-leader-service.yaml
kubectl get service
Copy the code

Create the Redis Followers application based on deployment

#https://k8s.io/examples/application/guestbook/redis-follower-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-follower
  labels:
    app: redis
    role: follower
    tier: backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
        role: follower
        tier: backend
    spec:
      containers:
      - name: follower
        image: gcr.io/google_samples/gb-redis-follower:v2
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379

kubectl apply -f redis-follower-deployment.yamlkubectl get pods
Copy the code

Create the Redis Follower Service

# https://k8s.io/examples/application/guestbook/redis-follower-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-follower
  labels:
    app: redis
    role: follower
    tier: backend
spec:
  ports:
    # the port that this service should serve on
  - port: 6379
  selector:
    app: redis
    role: follower
    tier: backend


kubectl apply -f redis-follower-service.yaml
kubectl get service
Copy the code

Guestbook deployment

Deploy Guestbook based on deployment

# https://k8s.io/examples/application/guestbook/frontend-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
        app: guestbook
        tier: frontend
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v5
        env:
        - name: GET_HOSTS_FROM
          value: "dns"
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 80

kubectl apply -f frontend-deployment.yaml
kubectl get pods -l app=guestbook -l tier=frontend
Copy the code

Deploy the Guestbook service

Change type to NodePort because the local environment is not LoadBalancer

# https://k8s.io/examples/application/guestbook/frontend-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # if your cluster supports it, uncomment the following to automatically create
  # an external load-balanced IP for the frontend service.
  # type: LoadBalancer
  #type: LoadBalancer
  ports:
    # the port that this service should serve on
  - port: 80
  selector:
    app: guestbook
    tier: frontend

kubectl apply -f frontend-service.yaml
kubectl get services
Copy the code

If the preceding configurations are not changed, you need to use proxy to access the preceding services

kubectl port-forward svc/frontend 8080:80
Copy the code

Load the page http://localhost:8080 in your browser to view your GuestBooks

GuestBook application capacity expansion or reduction

kubectl scale deployment frontend --replicas=5
kubectl scale deployment frontend --replicas=2
Copy the code

Clean up the GuestBook environment

kubectl delete deployment -l app=redis
kubectl delete service -l app=redis
kubectl delete deployment frontend
kubectl delete service frontend
Copy the code