创建nginx-deploy.yaml 【上节课的内容】

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    type: nginx-deploy
    test: 1.0.0
  namespace: default
spec:
  replicas: 2 # 副本数
  revisionHistoryLimit: 10 # 保留的历史版本数
  selector: # 选择器
    matchLabels:
      app: nginx-deploy
      test: 1.0.0
  strategy: # 更新策略
    type: RollingUpdate # 更新策略类型 RollingUpdate、Recreate
    rollingUpdate:
      maxUnavailable: 25% # 更新时最大不可用副本数
      maxSurge: 25% # 更新时最大超出副本数
  template: # 模板
    metadata:
      labels:
        app: nginx-deploy
        test: 1.0.0
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          imagePullPolicy: IfNotPresent
          resources:
            requests:
              cpu: 50m
              memory: 128Mi
            limits:
              cpu: 50m
              memory: 128Mi
      restartPolicy: Always # 重启策略
      terminationGracePeriodSeconds: 30 # pod被删除时的等待时间
# 创建
kubectl create -f nginx-deploy.yaml

创建nginx-svc.yaml 【上节课的内容】

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  labels:
    app: nginx
spec:
  selector:
    app: nginx-deploy
  ports:
    - port: 80
      targetPort: 80
      name: web
  type: NodePort
# 创建
kubectl create -f nginx-svc.yaml

# 查看pod信息
get po -l app=nginx-deploy -o wide

# 结果
NAME                            READY   STATUS    RESTARTS   AGE    IP               NODE        NOMINATED NODE   READINESS GATES
nginx-deploy-5666fcbb6b-6k8mw   1/1     Running   0          5m6s   10.244.107.241   k8s-node3   <none>           <none>
nginx-deploy-5666fcbb6b-h2w4l   1/1     Running   0          5m6s   10.244.122.95    k8s-node4   <none>           <none>

# 查看endpoint
kubectl get ep

#结果
kubernetes   192.168.10.100:6443                  5d19h
nginx-svc    10.244.107.241:80,10.244.122.95:80   6m48s

通过服务名进行访问

# 创建其他的pod通过 service name进行访问(推荐)
kubectl exec -it dns-test -- sh 
wget  http://nginx-svc

# 通过服务名+命名空间
wget  http://nginx-svc.default

代理k8s外部服务

1. 创建一个没有选择器的服务,不指定seletor;作用是内部访问外网

2. 自己创建endpoint

nginx-svc-external.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc-external
  labels:
    app: nginx
spec:
  ports:
    - port: 80
      targetPort: 80
      name: web
  type: ClusterIP
# 部署
kubectl create -f nginx-svc-external.yaml


# 查看服务
[root@k8s-master services]# kubectl get svc
NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes           ClusterIP   10.96.0.1        <none>        443/TCP        5d19h
nginx-svc            NodePort    10.104.51.98     <none>        80:30539/TCP   20m
nginx-svc-external   ClusterIP   10.103.145.219   <none>        80/TCP         7s

# endpoint
[root@k8s-master services]# kubectl get ep
NAME         ENDPOINTS                            AGE
kubernetes   192.168.10.100:6443                  5d19h
nginx-svc    10.244.107.241:80,10.244.122.95:80   20m
# 创建endpoint  nginx-ep-external.yaml

apiVersion: v1
kind: Endpoints
metadata:
  labels:
    app: nginx # 与service一致
  name: nginx-svc-external # 与service一致
  namespace: default # 与service一致
subsets:
  - addresses:
      - ip: 120.78.159.117 #目标服务ip
    ports:
      - name: web
        port: 80
        protocol: TCP



# 创建+查看
[root@k8s-master services]# touch nginx-ep-external.yaml
[root@k8s-master services]# kubectl create -f nginx-ep-external.yaml 
endpoints/nginx-svc-external created
[root@k8s-master services]# kubectl get ep
NAME                 ENDPOINTS                            AGE
kubernetes           192.168.10.100:6443                  5d19h
nginx-svc            10.244.107.241:80,10.244.122.95:80   27m
nginx-svc-external   120.78.159.117:80                    5s


# 测试访问

kubectl exec -it dns-test -- sh

wget http://nginx-svc-external

# 测试成功
/ # wget http://nginx-svc-external
Connecting to nginx-svc-external (10.103.145.219:80)
Connecting to www.wolfcode.cn (120.78.159.117:80)
Connecting to www.wolfcode.cn (120.78.159.117:443)
index.html           100% |***

方向代理外部域名

创建一个service的配置文件

nginx-svc-domain.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc-domain
  labels:
    app: nginx-svc-domain
spec:
  type: ExternalName
  externalName: www.wolfcode.cn

测试效果

# 进入终端
kubectl exec -it dns-test -- sh

# 删除之前的index.html
rm -rf index.html

# 测试
wget nginx-svc-domain

# 结果
wget wolfcode-svc-domain
wget: bad address 'wolfcode-svc-domain'
/ # wget nginx-svc-domain
Connecting to nginx-svc-domain (120.78.159.117:80)
Connecting to www.wolfcode.cn (120.78.159.117:80)
Connecting to www.wolfcode.cn (120.78.159.117:443)
index.html           100% 

ClusterIP

只能在集群内部使用,不配置的话默认就是 ClusterIP

ExternalName

返回定义的CNAME别名,可以配置为域名

NodePort

随机启动一个端口(30000-32767),映射到ports的端口,并且在每个node上都绑定改端口

可以固定写死一个端口

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  labels:
    app: nginx
spec:
  selector:
    app: nginx-deploy
  ports:
    - port: 80
      targetPort: 80
      name: web
      nodePort: 32000 # 固定写死
  type: NodePort

LoadBalancer

云服务上的负载均衡服务

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐