需要三个 Docker 镜像:
注意:
node1,node2操作一致
[root@node1 ~]# mkdir /root/k8s
[root@node1 ~]# cd /root/k8s
[root@node1 k8s]# ls
[root@node1 k8s]# ls #将镜像上传到该目录
docker.io-kubeguide-guestbook-php-frontend.tar
docker.io-kubeguide-guestbook-redis-slave.tar
docker.io-kubeguide-redis-master.tar
pod-infrastructure.tar
[root@node1 k8s]# docker load -i pod-infrastructure.tar
[root@node1 k8s]# docker load -i docker.io-kubeguide-redis-master.tar
[root@node1 k8s]# docker load -i docker.io-kubeguide-guestbook-redis-slave.tar
[root@node1 k8s]# docker load -i docker.io-kubeguide-guestbook-php-frontend.tar
[root@node1 k8s]# docker images #查看镜像是否导入成功
在 master 上操作
[root@master ~]# mkdir /etc/kubernetes/yaml
[root@master ~]# cd /etc/kubernetes/yaml/
[root@master yaml]# vim redis-master-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: redis-master
spec:
replicas: 1
template:
metadata:
labels:
app: redis
role: master
tier: backend
spec:
containers:
- name: master
image: docker.io/kubeguide/redis-master:latest
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
[root@master yaml]# vim redis-master-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
app: redis
role: master
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: master
tier: backend
[root@master yaml]# kubectl create -f redis-master-deployment.yaml
[root@master yaml]# kubectl create -f redis-master-service.yaml
[root@master yaml]# kubectl get deployment
[root@master yaml]# kubectl get pods
[root@master yaml]# kubectl get svc
在 master 上操作
[root@master yaml]# vim redis-slave-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: redis-slave
spec:
replicas: 2
template:
metadata:
labels:
app: redis
role: slave
tier: backend
spec:
containers:
- name: slave
image: docker.io/kubeguide/guestbook-redis-slave:latest
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 100Mi
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 6379
[root@master yaml]# vim redis-slave-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
app: redis
role: slave
tier: backend
spec:
ports:
- port: 6379
selector:
app: redis
role: slave
tier: backend
[root@master yaml]# kubectl create -f redis-slave-deployment.yaml
[root@master yaml]# kubectl create -f redis-slave-service.yaml
[root@master yaml]# kubectl get deployment
[root@master yaml]# kubectl get pods
[root@master yaml]# kubectl get pods -o wide
在 master 上操作
这是一个简单的 PHP 访问,用来和 master service(写请求)或 slave service(读请求)交互。
[root@master yaml]# vim frontend-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 3
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: docker.io/kubeguide/guestbook-php-frontend:latest
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 100Mi
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 80
[root@master yaml]# vim frontend-service.yaml
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
type: NodePort
ports:
- port: 80
nodePort: 30001
selector:
app: guestbook
tier: frontend
[root@master yaml]# kubectl create -f frontend-deployment.yaml
[root@master yaml]# kubectl create -f frontend-service.yaml
[root@master yaml]# kubectl get deployment
[root@master yaml]# kubectl get pods -o wide
[root@master yaml]# kubectl get svc -o wide
登录 node1 查看端口:
[root@node1 k8s]# netstat -anpt | grep 30001
使用浏览器访问:http://192.168.1.3:30001
或者访问:http://192.168.1.4:30001
都可以。