官术网_书友最值得收藏!

Exposing Kubernetes services

When we run a pod using the kubectl run command, this pod is accessible only inside Kubernetes. In most of cases, we would want this pod to be accessible from the outside as well. This is where the kubectl expose command comes in handy. Let's create the httpd pod one more time and then expose it to the outside world:

$ kubectl run httpd --image=httpd

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
httpd-66c6df655-8h5f4 1/1 Running 0 27m

Now let's use the kubectl expose command and expose the httpd web server to the outside of Kubernetes:

$ kubectl expose pod httpd-66c6df655-8h5f4 --port=80 --name=httpd-exposed --type=NodePort

While using the kubectl expose command, we specify several options:

  • port: Pod (Docker container) port that we are going to expose to the outside of the Kubernetes cluster.
  • name: Kubernetes service name.
  • type: Kubernetes service type. NodePort uses Kubernetes Node IP.  

The command to get a list of exposed Kubernetes services is kubectl get services:

$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d
httpd-exposed NodePort 10.110.40.149<none> 80:31395/TCP 3s
Note that port 80 was mapped to dynamic port 31395 on the Minikube VM. The port is dynamically chosen in the range 30000–32767.

Also, there is a ClusterIP field with the IP address 10.110.40.149 allocated for the httpd-expose service. Do not pay attention to this at the moment; we are going to discuss this later in the book.

Finally, use curl to check if the httpd server is available from the outside of the Kubernetes cluster:

$ curl 192.168.99.101:31395
<html><body><h1>It works!</h1></body></html>

If you open this link in your web browser, you should see It works! on the web page.

主站蜘蛛池模板: 开化县| 新巴尔虎左旗| 黑水县| 林州市| 尼勒克县| 汉中市| 顺平县| 安仁县| 晋江市| 永寿县| 济源市| 海阳市| 灵川县| 炉霍县| 武城县| 云梦县| 凤阳县| 葫芦岛市| 保亭| 武邑县| 始兴县| 涟水县| 蓬安县| 化隆| 肥西县| 宜丰县| 会昌县| 绥阳县| 泰顺县| 台北市| 大足县| 霍林郭勒市| 江门市| 林芝县| 额济纳旗| 桐柏县| 温泉县| 大理市| 科技| 图木舒克市| 彭阳县|