service自动发现,yaml文件管理内外部端口访问
service服务发现
[root@k8s-master ~]# vim busybox-5d4f595646-dzjv4.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox
namespace: default
spec:
containers:
- image: busybox
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
name: busybox
restartPolicy: Always
[root@k8s-master ~]# kubectl create -f busybox-5d4f595646-dzjv4.yaml
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 1m
进入创建的pod可以看到负载到的cluster IP 和端口
[root@k8s-master ~]# kubectl exec -it busybox sh
/ # env
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.10.10.1:443
MY_SERVICE_PORT_80_TCP=tcp://10.10.10.212:80
MY_SERVICE_PORT_443_TCP_ADDR=10.10.10.212
NGINX_SERVICE_PORT_88_TCP_ADDR=10.10.10.85
HOSTNAME=busybox
/ # echo ${MY_SERVICE_SERVICE_HOST}
10.10.10.212
/ # echo ${MY_SERVICE_SERVICE_PORT}
80
service暴露端口发布服务
[root@k8s-master ~]# vim nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
selector:
app: nginx
role: web
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 80
type: NodePort
[root@k8s-master ~]# vim nginx-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
role: web
spec:
containers:
- name: nginx
image: nginx:1.10
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl create -f nginx-deployment.yaml
[root@k8s-master ~]# kubectl create -f nginx-service.yaml
暴露内部端口为10.10.10.239:8080
对外暴露端口为192.168.30.22:44860
[root@k8s-master ~]# kubectl get svc
nginx-service NodePort 10.10.10.239 <none> 8080:44860/TCP
[root@k8s-node1 ~]# curl 10.10.10.239:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
[root@k8s-node2 ~]# curl 10.10.10.239:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>