k8s安装nginx
# 1.部署 Nginx pod
[root@master ~]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
resources:
requests:
cpu: 1
memory: 500Mi
limits:
cpu: 2
memory: 1024Mi
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
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
- 查看deployment
[root@master ~]# vim nginx-deployment.yaml
[root@master ~]# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 20s
1
2
3
4
5
6
2
3
4
5
6
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5d59d67564-6k5t5 0/1 ContainerCreating 0 5s
nginx-deployment-5d59d67564-b65tc 0/1 ContainerCreating 0 5s
nginx-deployment-5d59d67564-hxqhd 1/1 Running 0 5s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5d59d67564-6k5t5 0/1 ContainerCreating 0 24s
nginx-deployment-5d59d67564-b65tc 1/1 Running 0 24s
nginx-deployment-5d59d67564-hxqhd 1/1 Running 0 24s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5d59d67564-6k5t5 1/1 Running 0 25s
nginx-deployment-5d59d67564-b65tc 1/1 Running 0 25s
nginx-deployment-5d59d67564-hxqhd 1/1 Running 0 25s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 2.创建 service, 并让外部访问
[root@master ~]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
ports:
- port: 88
targetPort: 80
nodePort: 80
selector:
app: nginx
type: NodePort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@master ~]# vim nginx-service.yaml
[root@master ~]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d8h
nginx-service NodePort 10.111.182.42 <none> 88:80/TCP 8s
1
2
3
4
5
6
7
2
3
4
5
6
7
在浏览器中访问 访问地址格式为 MasterIP:NodePort, NodePort 为kubectl get service/nginx-servic 后 88:80/TCP中 80这个端口;本次为http://任意节点ip:80/
# 3.敲重点:验证k8s强大的自愈能力
- 删除一个pod
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-57787bbfc6-7fwkx 1/1 Running 0 6m32s
nginx-57787bbfc6-j4p5c 1/1 Running 0 6m32s
nginx-57787bbfc6-z79kb 1/1 Running 0 6m32s
[root@master ~]# kubectl delete pods nginx-57787bbfc6-7fwkx
pod "nginx-57787bbfc6-7fwkx" deleted
1
2
3
4
5
6
7
2
3
4
5
6
7
- 自动补上一个pod
[root@master ~]#
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-57787bbfc6-4jxtw 1/1 Running 0 10s
nginx-57787bbfc6-j4p5c 1/1 Running 0 7m12s
nginx-57787bbfc6-z79kb 1/1 Running 0 7m12s
[root@master ~]#
1
2
3
4
5
6
7
2
3
4
5
6
7