3

实例结构

clipboard.png

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

clipboard.png

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

clipboard.png

全部启动后的kubernetes-ui

clipboard.png

删除

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

参考


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...