<p>How do I define a network policy YML file so it only allows traffic to and from a hostname like console.cloud.ibm.com?</p>

<p>I only can find how to block from certain IP addresses or namespaces.</p>

<p>You can use <a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/" rel="nofollow noreferrer">Network Policies</a> to block the IP address of what resolves to:</p>

<pre><code>$ dig +short console.cloud.ibm.com
23.204.34.209
</code></pre>

<p>The downside is that the IP might change. Network policies are a layer 3 or IP layer feature so you won't see hostnames there. Some background <a href="https://github.com/kubernetes/kubernetes/issues/50453" rel="nofollow noreferrer">here</a></p>

<p>If you want to block on hostname you might consider using a Service Mesh like <a href="https://istio.io/" rel="nofollow noreferrer">Istio</a> with <a href="https://www.envoyproxy.io/" rel="nofollow noreferrer">Envoy</a> which will allow you to control traffic to a <code>host</code> by using <a href="https://istio.io/docs/tasks/traffic-management/egress/" rel="nofollow noreferrer">Egress Control</a>.</p>
