1. 程式人生 > >Serverless Kubernetes容器服務支援pod掛載彈性公網IP

Serverless Kubernetes容器服務支援pod掛載彈性公網IP

近日,阿里雲Serverless Kubernetes服務推出pod掛載彈性公網IP功能,此功能使某些serverless容器應用的部署和服務訪問變得更加簡單和便利。

  • 無需建立VPC NAT閘道器即可讓單個pod訪問公網
  • 無需建立service也可讓單個pod暴露公網服務
  • 可以更加靈活而且動態的繫結pod和eip

目前Serverless Kubernetes支援兩種方法掛載eip,支援自動分配eip,或者指定eip例項進行繫結。

方法一:自動分配彈性公網IP

通過指定Annonation "k8s.aliyun.com/enable-eip"為"true",serverless kubernetes服務會自動為此pod分配一個eip,並且繫結到pod上。

示例:

#cat nginx-enable-eip-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  annotations:
    "k8s.aliyun.com/enable-eip": "true"
spec:
  containers:
  - image: registry-vpc.cn-hangzhou.aliyuncs.com/jovi/nginx:alpine
    imagePullPolicy: Always
    name: nginx
    ports:
    - containerPort: 80
      name: http
      protocol: TCP
  restartPolicy: OnFailure

建立pod:

#kubectl apply -f nginx-enable-eip-pod.yaml
pod "nginx" created

#kubectl get pod
nginx     1/1       Running   0         20s

檢視pod的ip地址:

# kubectl describe pod
Name:         nginx
Namespace:    default
Node:         viking-c7d16b6c584544f65bfa4eba3a8b04d63/
Start Time:   Mon, 07 Jan 2019 13:19:47 +0800
Labels:       <none>
Annotations:  k8s.aliyun.com/allocated-eipAddress=47.96.67.132
              k8s.aliyun.com/allocated-eipInstanceId=eip-bp1wtbt7vp18tgu5g7rb2
              k8s.aliyun.com/enable-eip=true
              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{"k8s.aliyun.com/enable-eip":"true"},"name":"nginx","namespace":"default"},"spec":{"container...
              kubernetes.io/limit-ranger=LimitRanger plugin set: cpu, memory request for container nginx
Status:       Running
IP:           10.1.89.103
Containers:
  nginx:
    Container ID:   eci://779380281b08b325b4b7a1b66c4cb9e706985b25cde0c36345af93a308745b95
    Image:          registry-vpc.cn-hangzhou.aliyuncs.com/jovi/nginx:alpine
    Image ID:
    Port:           80/TCP
    State:          Running
      Started:      Mon, 07 Jan 2019 13:19:47 +0800
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        1
      memory:     2Gi
    Environment:  <none>
    ...
    
# kubectl describe pod|grep allocated-eipAddress
Annotations:  k8s.aliyun.com/allocated-eipAddress=47.96.67.132

我們可以看到在pod的Annotations中顯示了已分配的eip,通過此eip可直接訪問pod。

# curl 47.96.67.132
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...

因為此方式中eip為動態分配,其生命週期與pod相同,當pod被刪除時,動態分配的eip也會被一同刪除。

方法二:指定彈性公網IP例項id

首先使用者需要在eip控制檯購買彈性公網eip。
image

通過指定pod的Annonation "k8s.aliyun.com/eipInstanceId"為eip例項id,如下:

# cat nginx-eipid-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  annotations:
    "k8s.aliyun.com/eipInstanceId": "eip-bp19trewkig3i9pnek99i"
spec:
  containers:
  - image: registry-vpc.cn-hangzhou.aliyuncs.com/jovi/nginx:alpine
    imagePullPolicy: Always
    name: nginx
    ports:
    - containerPort: 80
      name: http
      protocol: TCP
  restartPolicy: OnFailure

建立pod

# kubectl apply -f  nginx-eipid-pod.yaml
pod "nginx" created

# kubectl get pod
NAME      READY     STATUS    RESTARTS   AGE
nginx     1/1       Running   0         20s

通過eip訪問pod:

# curl 47.111.20.92
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
...

此種方式種當pod被刪除時,pod和eip解除繫結。pod重新建立時eip則會重新被繫結。

快速試用Serverless Kubernetes

歡迎登入容器服務控制檯,公測期間免費使用,https://cs.console.aliyun.com/#/k8s

如在使用中有任何問題,歡迎掃碼加入Serverless K8s釘釘群進行討論
image