前面的文章中介绍了使用Deployment和Service启动nginx服务的方法,为了后续继续介绍Ingress和其他功能,这里需要确定访问服务时能够返回不同的信息,这篇文章继续介绍一下如何指定容器执行时使用的参数。
使用Deployment + Service
使用Deployment和Service,能够让用户直接访问到暴露出来的服务,使用这种方式启动的nginx服务的示例如下所示:
在此基础之上,我们进行一些扩展,首先启动nginx服务并确认Pod
[root@host131 nginx]# ls
deployment.yml service.yml
[root@host131 nginx]# kubectl create -f .
deployment.apps/nginx-deployment created
service/nginx-service created
[root@host131 nginx]#
[root@host131 nginx]# kubectl get service nginx-service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx-service NodePort 10.254.131.2 <none> 80:30000/TCP 22s app=nginx-app
[root@host131 nginx]# kubectl get pod |grep nginx
nginx-deployment-7c5b8fc568-9g4jb 1/1 Running 0 31s
[root@host131 nginx]#
进入到pod之后,修改nginx的index.html文件
[root@host131 nginx]# kubectl exec -it nginx-deployment-7c5b8fc568-9g4jb sh
/ # cd /usr/share/nginx/html/
/usr/share/nginx/html # ls
50x.html index.html
/usr/share/nginx/html # vi index.html
/usr/share/nginx/html # cat index.html
<!DOCTYPE html>
<html>
<body>
<title>Welcome to use nginx</title>>
<script>
document.write("location: " + window.location.href + "</br>");
document.write("hostname: " + window.location.hostname