k8s

k8s官网留言板demo

https://kubernetes.io/docs/tutorials/stateless-application/guestbook/#start-up-the-redis-master

1、部署redis-master Deployment

  • Step1 编辑redis-master-deployment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# application/guestbook/redis-master-deployment.yaml 
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: redis-master
labels:
app: redis
spec:
selector:
matchLabels:
app: redis
role: master
tier: backend
replicas: 1
template:
metadata:
labels:
app: redis
role: master
tier: backend
spec:
containers:
- name: master
image: redis # or just image: redis
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379

  • Step2 执行命令
1
2
3
4
5
# 创建Deployment
kubectl create -f redis-master-deployment.yaml

# 查看pods
kubectl get pods

2、部署redis-master Service

  • Step1 编辑redis-master-service.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# application/guestbook/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

  • Step2 执行命令
1
2
3
4
5
# 创建DService
kubectl create -f redis-master-service.yaml

# 查看service
kubectl get services

3、部署redis-slave Deployment

  • Step1 编辑redis-slave-deployment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# application/guestbook/redis-slave-deployment.yaml 

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: redis-slave
labels:
app: redis
spec:
selector:
matchLabels:
app: redis
role: slave
tier: backend
replicas: 2
template:
metadata:
labels:
app: redis
role: slave
tier: backend
spec:
containers:
- name: slave
image: gcr.io/google_samples/gb-redisslave:v3
resources:
requests:
cpu: 100m
memory: 100Mi
env:
- name: GET_HOSTS_FROM
value: dns
# Using `GET_HOSTS_FROM=dns` requires your cluster to
# provide a dns service. As of Kubernetes 1.3, DNS is a built-in
# service launched automatically. However, if the cluster you are using
# does not have a built-in DNS service, you can instead
# access an environment variable to find the master
# service's host. To do so, comment out the 'value: dns' line above, and
# uncomment the line below:
# value: env
ports:
- containerPort: 6379

  • Step2 执行命令
1
2
kubectl create -f redis-slave-deployment.yaml
kubectl get pods

4、部署redis slave Service

  • Step1 编辑redis-slave-service.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# application/guestbook/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

  • Step2 执行命令
1
2
kubectl create -f redis-slave-service.yaml
kubectl get services

5、部署guestbook Deployment

  • Step1 编辑frontend-deployment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# application/guestbook/frontend-deployment.yaml 

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: frontend
labels:
app: guestbook
spec:
selector:
matchLabels:
app: guestbook
tier: frontend
replicas: 3
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google-samples/gb-frontend:v6
# 官网上使用的是v4版,在使用中出现ImageInspectErr,镜像出现问题,升级为v6
resources:
requests:
cpu: 100m
memory: 100Mi
env:
- name: GET_HOSTS_FROM
value: dns
# Using `GET_HOSTS_FROM=dns` requires your cluster to
# provide a dns service. As of Kubernetes 1.3, DNS is a built-in
# service launched automatically. However, if the cluster you are using
# does not have a built-in DNS service, you can instead
# access an environment variable to find the master
# service's host. To do so, comment out the 'value: dns' line above, and
# uncomment the line below:
# value: env
ports:
- containerPort: 80

  • Step2 执行命令
1
2
kubectl create -f frontend-deployment.yaml
kubectl get pods

6、部署guestbook Service

  • Step1 编辑frontend-service.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# application/guestbook/frontend-service.yaml 

apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# comment or delete the following line if you want to use a LoadBalancer
type: NodePort
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
# type: LoadBalancer
ports:
- port: 80
selector:
app: guestbook
tier: frontend

  • Step2 执行命令
1
2
kubectl create -f frontend-service.yaml
kubectl get services

然后使用node的IP和端口,就可以访问服务了。

7、清理

1
2
3
4
kubectl delete deployment -l app=redis
kubectl delete service -l app=redis
kubectl delete deployment -l app=guestbook
kubectl delete service -l app=guestbook

8、小结

通过部署一个留言板服务,熟悉了kubernetes的基本的使用,知道一个服务怎么通过kubernetes部署起来。服务虽然部署起来了,但是kubernetes是怎么实现的呢?它都帮我们做了什么呢?不过在搞清楚这些之前,我们下一篇文章先说说kuberneters使用的yaml配置文件。

分享到