近日,阿裡雲Serverless Kubernetes服務基于ECI推出pod挂載彈性公網EIP功能,此功能使某些serverless容器應用的部署和服務通路變得更加簡單和便利。
- 無需建立VPC NAT網關即可讓單個pod通路公網
- 無需建立service也可讓單個pod暴露公網服務
目前Serverless Kubernetes支援兩種方法挂載EIP,支援自動配置設定EIP,或者指定EIP執行個體進行綁定。
方法一:自動配置設定彈性公網EIP
通過指定Annonation "k8s.aliyun.com/eci-with-eip"為"true",serverless kubernetes服務會自動為此pod配置設定一個eip,并且綁定到pod上。
示例:
#cat nginx-enable-eip-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
annotations:
"k8s.aliyun.com/eci-with-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/eci-with-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也會被一同删除。
我們也可以通過Annotation “k8s.aliyun.com/eip-bandwidth”指定eip的帶寬,預設值為5,機關為M。
請注意,如果您建立的是deployment,那麼deployment中的每一個pod都将會被挂載不同的eip,請謹慎使用此操作。
方法二:指定彈性公網IP執行個體id
首先使用者需要在eip控制台購買彈性公網eip。

通過指定pod的Annonation "k8s.aliyun.com/eci-eip-instanceid"為eip執行個體id,如下:
# cat nginx-eipid-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
annotations:
"k8s.aliyun.com/eci-eip-instanceid": "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釘釘群進行讨論