# k8s基本使用
# 介绍
[TOC]
主机配置:
主机名 | IP地址 |
---|---|
master | 192.168.153.130 |
node1 | 192.168.153.131 |
node2 | 192.168.153.132 |
系统:CentOS7
docker版本:19.03.5
k8s版本:1.18.0
1. nginx配置
- nginx.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: default
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
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
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
- 发布nginx
kubectl apply -f nginx.yaml
1
- ingress-nginx
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx
namespace: default
labels:
app: nginx
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: server01
http:
paths:
- path: /
backend:
serviceName: nginx
servicePort: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 发布ingress-nginx
kubectl apply -f ingress-nginx.yaml
1
查看ingress-nginx映射端口
kubectl get svc -n ingress-nginx
1
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.10.98.50 <none> 80:30162/TCP,443:30443/TCP 36m
1
2
2
访问nginx
http://server01:30162/
1