Kubernetes基础:指定容器执行时使用的参数

Source

前面的文章中介绍了使用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 + "</br>");
	document.write("host:     " + window.location.host + "</br>");
	document.write("pathname: " + window.location.pathname + "</br>");
	document.write("protocol: " + window.location.protocol + "</br>");
	document.write("port:     " + window.location.port + "</br>");
</script>

</body>
</html>
/usr/share/nginx/html # 

结果确认
在这里插入图片描述
因为JS是运行线沙箱中的,所以获取操作系统的hostname之类的信息,使用直接的方式几乎是不太可能。所以这里直接使用之前做过的一个tornado的镜像。

Docker方式的参数传递

比如使用如下镜像,可以在启动的时候传递参数:

liumiaocn:~ liumiao$ docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "in host1"
814d29e3391227349e7cde5c61f55e90a0cecdc40779ec0ee5834a1dab28daa2
liumiaocn:~ liumiao$ docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "in host2"
d4b0115d7dce5a4cf73e0213a6096cee46240e9d2ba6c2b7752d5ec63879f3ab
liumiaocn:~ liumiao$ 

执行确认如下所示

liumiaocn:~ liumiao$ curl http://localhost:7001
Hello, Service :in host1
liumiaocn:~ liumiao$ 
liumiaocn:~ liumiao$ curl http://localhost:7002
Hello, Service :in host2
liumiaocn:~ liumiao$ 

这是使用Docker方式进行参数的传递方式,而如果是用Deployment应该怎样做呢

Deployment中指定容器执行时使用的参数

使用同样的方式使用如下Deployment和Service

[root@host131 nginx]# ls
deployment.yml  service.yml
[root@host131 nginx]# cat deployment.yml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tornado-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tornado-app
  template:
    metadata:
      labels:
        app: tornado-app
    spec:
      containers:
      - name: tornado-host
        image: liumiaocn/tornado:latest
        command: ["python"]
        args: ["/usr/local/bin/daemon.py", "in host1"]
        ports:
        - containerPort: 80
...
[root@host131 nginx]# cat service.yml 
---
apiVersion: v1
kind: Service
metadata:
  name: tornado-service
  labels:
    app: tornado-service-app
spec:
  type: NodePort
  ports:
  - port: 8080
    nodePort: 30000
  selector:
    app: tornado-app
...
[root@host131 nginx]# 

创建服务

[root@host131 nginx]# kubectl create -f .
deployment.apps/tornado-deployment created
service/tornado-service created
[root@host131 nginx]# 
[root@host131 nginx]# kubectl get pods -o wide |grep tornado
tornado-deployment-69c57569c6-d28q8   1/1     Running   0          9s    10.254.152.6   192.168.163.131   <none>           <none>
[root@host131 nginx]# kubectl get service -o wide |grep tornado
tornado-service   NodePort    10.254.203.121   <none>        8080:30000/TCP   25s     app=tornado-app
[root@host131 nginx]#
  • 结果确认
[root@host131 nginx]# curl http://10.254.203.121:8080
Hello, Service :in host1
[root@host131 nginx]# 
[root@host131 nginx]# curl http://192.168.163.131:30000
Hello, Service :in host1
[root@host131 nginx]# 
  • 浏览器结果确认
    在这里插入图片描述

其他格式

Deployment传递参数,还可以写成如下信息

[root@host131 nginx]# cat deployment.yml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tornado-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tornado-app
  template:
    metadata:
      labels:
        app: tornado-app
    spec:
      containers:
      - name: tornado-host
        image: liumiaocn/tornado:latest
        command:
          - python
        args: 
          - /usr/local/bin/daemon.py
          - in host1
        ports:
        - containerPort: 80
...
[root@host131 nginx]#
发布了1140 篇原创文章 · 获赞 1362 · 访问量 414万+