kubernetes上部署应用实例

实例结构

kubernetes上部署应用实例_第1张图片

创建redis-master-controller.yaml

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

发布到kubernetes集群,自动创建pod

kubectl create -f redis-master-controller.yaml
kubectl get rc
kubectl get pods

创建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

创建service

kubectl create -f redis-master-service.yaml
kubectl get services

创建redis-slave-controller.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-slave
spec:
  replicas: 2
  selector:
    name: redis-slave
  template:
    metadata:
      name: redis-slave
      labels:
        name: redis-slave
    spec:
      containers:
      - name: redis-slave
        image: kubeguide/guestbook-redis-slave
        env:
        - name: GET_HOSTS_FROM
          value: env
        ports:
        - containerPort: 6379

创建

kubectl create -f redis-slave-controller.yaml
kubectl get rc
kubectl get pods

创建redis-slave-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  ports:
  - port: 6379
  selector:
    name: redis-slave

创建

kubectl create -f redis-slave-service.yaml
kubectl get services

创建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: frontend
        image: kubeguide/guestbook-php-frontend
        env:
        - name: GET_HOSTS_FROM
          value: env
        ports:
        - containerPort: 80

创建

kubectl create -f frontend-controller.yaml
kubectl get rc
kubectl get pods

创建frontend-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    name: frontend
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30001
  selector:
    name: frontend

创建

kubectl create -f frontend-service.yaml
kubectl get services

kubernetes上部署应用实例_第2张图片

访问
http://your-host:30001/

kubernetes上部署应用实例_第3张图片

全部启动后的kubernetes-ui

kubernetes上部署应用实例_第4张图片

删除

kubectl delete -f frontend-controller.yaml
replicationcontroller "frontend" deleted
kubectl delete -f redis-master-controller.yaml
replicationcontroller "redis-master" deleted
kubectl delete -f redis-slave-controller.yaml
replicationcontroller "redis-slave" deleted
kubectl delete -f redis-slave-service.yaml
service "redis-slave" deleted
kubectl delete -f redis-master-service.yaml
service "redis-master" deleted
kubectl delete -f frontend-service.yaml

参考

  • kubernetes-example-guestbook

  • kubernetes实战-基于redis和docker的留言簿案例

  • Configuring Your Cloud Provider's Firewalls

你可能感兴趣的:(kubernetes)