解决制作CI流水线时的no host异常报错

方法介绍

使用 HostAliases 向 Pod /etc/hosts 文件添加条目

当dns配置以及其他选项不合理时,可以通过向pod的/etc/hosts添加条目,可以在pod级别覆盖对主机名的解析,可以通过pod spec的pod aliases来自定义添加条目。

默认的hosts文件内容

当你启动一个nginx pod时,他会默认给你分配一个ip地址

bash 复制代码
kubectl exec nginx -- cat /etc/hosts

# Kubernetes-managed hosts file.
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
fe00::0	ip6-mcastprefix
fe00::1	ip6-allnodes
fe00::2	ip6-allrouters
10.200.0.4	nginx

但是如果你想让他解析到别的ip和域名可以通过添加HostAliases增加额外条目

例如 :要将 foo.localbar.local 解析为 127.0.0.1, 将 foo.remotebar.remote 解析为 10.1.2.3,你可以在 .spec.hostAliases 下为 Pod 配置 HostAliases。

bash 复制代码
apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "127.0.0.1"
    hostnames:
    - "foo.local"
    - "bar.local"
  - ip: "10.1.2.3"
    hostnames:
    - "foo.remote"
    - "bar.remote"
  containers:
  - name: cat-hosts
    image: busybox:1.28
    command:
    - cat
    args:
    - "/etc/hosts"

通过kubectl apply -f 启动pod查看hosts

bash 复制代码
# Kubernetes-managed hosts file.
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
fe00::0	ip6-mcastprefix
fe00::1	ip6-allnodes
fe00::2	ip6-allrouters
10.200.0.5	hostaliases-pod

# Entries added by HostAliases.
127.0.0.1	foo.local	bar.local
10.1.2.3	foo.remote	bar.remote

这样就增加了额外的条目!

为什么 kubelet 管理 hosts 文件?

kubelet 管理每个Pod 容器的 hosts 文件,以防止容器运行时在容器已经启动后修改文件。 由于历史原因,Kubernetes 总是使用 Docker Engine 作为其容器运行时,而 Docker Engine 将在容器启动后修改 /etc/hosts 文件。

按照上面的方法可以每次运行容器时,都符合你的预期

在CI流水线中添加podtemplate

下面是task的添加模板

bash 复制代码
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: mytask
spec:
  steps:
    - name: writesomething
      image: ubuntu
      command: ["bash", "-c"]
      args: ["echo 'foo' > /my-cache/bar"]
      volumeMounts:
        - name: my-cache
          mountPath: /my-cache
---
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: mypipeline
spec:
  tasks:
    - name: task1
      taskRef:
        name: mytask
---
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: mypipelinerun
spec:
  pipelineRef:
    name: mypipeline
  taskRunTemplate:
    podTemplate:
      securityContext:
        runAsNonRoot: true
        runAsUser: 1001
      volumes:
        - name: my-cache
          persistentVolumeClaim:
            claimName: my-volume-claim
相关推荐
帅帅梓12 分钟前
Linux性能检测与调优
linux·运维·php
van叶~28 分钟前
Linux网络-------3.应⽤层协议HTTP
linux·网络·http
Wmenghu1 小时前
java获取电脑公网IP和内网IP
服务器·网络·tcp/ip
我不是程序猿儿1 小时前
【git】在 GitLab 上如何把 A 分支(如 feature/xxx)合并到 B 分支(如 trunk)
服务器·git·gitlab
极小狐1 小时前
GitLab 18.2 发布几十项与 DevSecOps 有关的功能,可升级体验【一】
ci/cd·gitlab·devsecops·devops·极狐gitlab
极小狐1 小时前
GitLab 18.2 发布几十项与 DevSecOps 有关的功能,可升级体验【三】
ci/cd·gitlab·devsecops·devops·极狐gitlab
慌糖1 小时前
Spring Boot音乐服务器项目-查询喜欢的音乐模块
服务器·spring boot·mybatis
花小璇学linux2 小时前
imx6ull-驱动开发篇5——新字符设备驱动实验
linux·驱动开发·imx6ull·嵌入式软件
小猪咪piggy2 小时前
【JavaEE】(7) 网络原理 TCP/IP 协议
运维·服务器·网络
饭碗的彼岸one2 小时前
重生之我在10天内卷赢C++ - DAY 1
linux·开发语言·c++·经验分享·笔记·学习方法