-
添加方式
您可以在创建应用时设置访问方式,也可以应用创建完成后添加访问方式。
l 方式一:创建应用时配置;
l 方式二:应用创建完成后设置。
-
通过界面创建
步骤1:创建无状态应用或创建有状态应用,在“应用访问设置”步骤,单击“添加服务”。
l 服务名称:自定义服务名称,可与应用名称保持一致。
l 访问方式:集群内访问。
l 协议:请根据应用的协议类型选择。
l 容器端口:容器镜像中应用程序实际监听端口,需用户确定。nginx程序实际监听的端口为80。
l 访问端口:容器端口映射到集群虚拟IP上的端口,用虚拟IP访问应用时使用,端口范围为1-65535,可任意指定。
步骤2:单击“下一步”,进入“高级设置”页面,直接单击“创建”。
步骤3:单击“查看应用详情”,在访问方式页签中获取访问地址,例如10.247.74.100:2。
步骤4:登录应用所在集群的任意节点,登录方法请参见SSH密钥登录方式。
步骤5:使用curl命令访问应用验证应用是否可以正常访问。您可以通过IP或者域名的方式来验证。
l 方式一:通过IP地址验证。
curl 10.247.74.100:2
其中10.247.74.100:2为步骤3中获取的访问地址。
回显如下表示应用可正常访问。
<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>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
l 方式二:通过域名验证。
curl nginx.default.svc.cluster.local:2
其中nginx.default.svc.cluster.local为步骤3中获取的域名访问地址。
回显如下表示应用可正常访问。
<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>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
应用创建完成后设置
步骤1:登录CCE控制台,选择左侧导航栏的“资源管理 > 网络管理”,在Service页签下,单击“添加Service”。选择类型为“集群内访问”。
步骤2:设置集群内访问参数。
l 服务名称:自定义服务名称,可与应用名称保持一致。
l 集群名称:服务所在集群。
l 命名空间:服务所在命名空间。
l 关联应用:选择需要添加Service的应用。
l 端口配置:
− 协议:请根据业务的协议类型选择。
− 容器端口:容器镜像中应用程序实际监听的端口,需用户确定。nginx程序实际监听的端口为80。
− 访问端口:容器端口映射到集群虚拟IP上的端口,用虚拟IP访问应用时使用,端口范围为1-65535,可任意指定。
步骤3:单击“创建”。应用已添加“集群内访问”的服务。验证操作与步骤4-步骤5相同。
-
通过kubectl命令行创建
本节以nginx应用为例,说明kubectl命令实现集群内访问的方法。
前提条件
配置kubectl命令,使弹性云主机连接集群。
操作步骤
步骤1:登录已配置好kubectl命令的弹性云主机。
步骤2:创建并编辑nginx-deployment.yaml和nginx-clusterip-svc.yaml文件。
其中,nginx-deployment.yaml和nginx-clusterip-svc.yaml为自定义名称,您可以随意命名。
vi nginx-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
imagePullSecrets:
- name: default-secret
vi nginx-ClusterIp-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx-clusterip
spec:
ports:
- name: service0
port: 2 #对应界面上的访问端口
protocol: TCP
targetPort: 80 #对应界面上的容器端口
selector:
app: nginx
type: ClusterIP #对应界面上的访问类型,ClusterIP表示“集群虚拟IP”
步骤3:创建应用。
kubectl create -f nginx-deployment.yaml
回显如下,表示应用已开始创建。
deployment "nginx" created
kubectl get po
回显如下,应用状态为Running,表示应用已处于运行中状态。
NAME READY STATUS RESTARTS AGE
etcd-0 0/1 ImagePullBackOff 0 27m
icagent-m9dkt 0/0 Running 0 3d
nginx-2601814895-znhbr 1/1 Running 0 15s
步骤4:创建服务。
kubectl create -f nginx-ClusterIp-svc.yaml
回显如下,表示服务已开始创建。
service "nginx-clusterip" created
kubectl get svc
回显如下,表示服务已创建成功,CLUSTER-IP已生成。
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
etcd-svc ClusterIP None <none> 3120/TCP 30m
kubernetes ClusterIP 10.247.0.1 <none> 443/TCP 3d
nginx-clusterip ClusterIP 10.247.200.134 <none> 80/TCP 20s
步骤5:登录应用所在集群的任意节点;
步骤6:采用curl命令访问应用验证应用是否可以正常访问。您可以通过IP或者域名的方式来验证。
l 方式一:通过IP地址验证。
curl 10.247.200.134:2
回显如下表示应用可正常访问。
<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>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
l 方式二:通过域名验证。
curl nginx-clusterip.default.svc.cluster.local:2
回显如下表示应用可正常访问。
<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>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>