Skip to content

Commit 5e2de3c

Browse files
committed
Fix #15 - Show how expose a service out of the Kubernetes Cluster
1 parent 14b236d commit 5e2de3c

6 files changed

Lines changed: 192 additions & 24 deletions

File tree

attendees/kubernetes/app-wildfly-rc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ spec:
1111
metadata:
1212
labels:
1313
name: wildfly
14+
context: docker-k8s-lab
1415
spec:
1516
containers:
1617
- name: wildfly-rc-pod
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: wildfly-service
5+
labels:
6+
name: wildfly
7+
context: docker-k8s-lab
8+
spec:
9+
type: NodePort
10+
ports:
11+
# the port that this service should serve on
12+
- port: 8080
13+
# label keys and values that must match in order to receive traffic for this service
14+
selector:
15+
name: wildfly
16+
context: docker-k8s-lab

attendees/kubernetes/create-script.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

attendees/kubernetes/delete-script.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

chapters/docker-kubernetes.adoc

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ The order of Service and the targeted Pods does not matter. However Service need
232232
pods/mysql-pod
233233
----
234234

235+
235236
It uses the following configuration file:
236237

237238
[source, yaml]
@@ -280,6 +281,7 @@ mysql-pod 1/1 Running 0 44s
280281

281282
`-w` watches for changes to the requested object. Wait for the MySQL pod to be in Running status.
282283

284+
[[Mysql_service_file]]
283285
#### Start MySQL service
284286

285287
. Start MySQL Service:
@@ -343,7 +345,7 @@ Kubernetes also allows services to be resolved using DNS configuration. Send a P
343345
#### Start WildFly Replication Controller
344346

345347
. Start WildFly replication controller:
346-
+
348+
347349
[source, text]
348350
----
349351
./cluster/kubectl.sh create -f ../../attendees/kubernetes/app-wildfly-rc.yaml
@@ -394,7 +396,7 @@ wildfly-rc-w2kk5 1/1 Running 0 6m
394396
----
395397

396398
[[Access_Kubernetes_Application_Node]]
397-
#### Access the application (using node)
399+
#### Access the application (from inside a node)
398400

399401
. Log in to node:
400402

@@ -412,10 +414,71 @@ Last login: Thu Jul 16 00:24:36 2015 from 10.0.2.2
412414
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection><employee><id>1</id><name>Penny</name></employee><employee><id>2</id><name>Sheldon</name></employee><employee><id>3</id><name>Amy</name></employee><employee><id>4</id><name>Leonard</name></employee><employee><id>5</id><name>Bernadette</name></employee><employee><id>6</id><name>Raj</name></employee><employee><id>7</id><name>Howard</name></employee><employee><id>8</id><name>Priya</name></employee></collection>
413415
----
414416

415-
[[Access_Kubernetes_Application_Proxy]]
416-
#### Access the application (using proxy)
417+
[[Access_Kubernetes_Application_Service]]
418+
#### Access the application (using Services)
419+
420+
For some parts of your application (e.g. frontends) you may want to expose a Service onto an external (outside of your cluster, maybe public internet) IP address, other services should be visible only from inside of the cluster.
421+
422+
Kubernetes ServiceTypes allow you to specify what kind of service you want. The default and base type is ClusterIP, which exposes a service to connection from inside the cluster. NodePort and LoadBalancer are two types that expose services to external traffic.
423+
424+
Valid values for the ServiceType field are:
425+
426+
- *ClusterIP*: use a cluster-internal IP only - this is the default and is discussed above. Choosing this value means that you want this service to be reachable only from inside of the cluster.
427+
- *NodePort*: on top of having a cluster-internal IP, expose the service on a port on each node of the cluster (the same port on each node). You'll be able to contact the service on any <NodeIP>:NodePort address.
428+
- *LoadBalancer*: on top of having a cluster-internal IP and exposing service on a NodePort also, ask the cloud provider for a load balancer which forwards to the Service exposed as a <NodeIP>:NodePort for each Node.
429+
430+
As you could see on <<Mysql_service_file>> section, the MySql service file doesn't contain any ServiceType field. For that reason, the default value is *ClusterIP* which means that the MySql service can be accessed inside the Cluster.
431+
432+
In the other hand, not all Kubernetes Provider supports the *LoadBalancer* type. On cloud providers which support external load balancers, setting the type field to "LoadBalancer" will provision a load balancer for your Service.
433+
434+
Do show how to access WildFly outside Kubernetes, we will need to use *NodePort* type which exposes your service on an external port on all nodes in your cluster.
435+
436+
Let's see how it works.
437+
438+
439+
. Create a WildFly service of NodePort type
440+
441+
[source, text]
442+
----
443+
./cluster/kubectl.sh create -f ../../attendees/kubernetes/app-wildfly-service.yaml
444+
You have exposed your service on an external port on all nodes in your
445+
cluster. If you want to expose this service to the external internet, you may
446+
need to set up firewall rules for the service port(s) (tcp:30140) to serve traffic.
447+
448+
See http://releases.k8s.io/HEAD/docs/user-guide/services-firewalls.md for more details.
449+
services/wildfly-service
450+
----
451+
452+
Note that it will reply with the port that was opened on each node of the cluster. Now you can use the combination between the node ip and the service port to access the service.
453+
454+
. Get the IP of the node
455+
456+
[source, text]
457+
----
458+
cluster/kubectl.sh get nodes
459+
NAME LABELS STATUS
460+
10.245.1.3 kubernetes.io/hostname=10.245.1.3 Ready
461+
----
462+
463+
. Get the PORT of the service
464+
465+
[source, text]
466+
----
467+
cluster/kubectl.sh describe se wildfly-service
468+
Name: wildfly-service
469+
Namespace: default
470+
Labels: context=docker-k8s-lab,name=wildfly
471+
Selector: context=docker-k8s-lab,name=wildfly
472+
Type: NodePort
473+
IP: 10.247.39.239
474+
Port: <unnamed> 8080/TCP
475+
NodePort: <unnamed> 30140/TCP
476+
Endpoints: 10.246.88.7:8080
477+
Session Affinity: None
478+
No events.
479+
----
417480

418-
Send a PR for https://github.com/javaee-samples/docker-java/issues/80
481+
Note the _NodePort_ value and now you can access the URL: http://10.245.1.3:30140/employees/
419482

420483
### Deploy Java EE Application (one configuration file)
421484

@@ -485,6 +548,7 @@ spec:
485548
metadata:
486549
labels:
487550
name: wildfly
551+
context: docker-k8s-lab
488552
spec:
489553
containers:
490554
- name: wildfly-rc-pod
@@ -505,7 +569,7 @@ services/mysql-service
505569
replicationcontrollers/wildfly-rc
506570
----
507571

508-
. Access the application using <<Access_Kubernetes_Application_Node>> or <<Access_Kubernetes_Application_Proxy>>.
572+
. Access the application using <<Access_Kubernetes_Application_Node>> or <<Access_Kubernetes_Application_Service>>.
509573

510574
### Rescheduling Pods
511575

instructor/dockerfiles/lab-httpd-server/index.html

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,10 @@ <h1>Docker for Java Developers</h1>
615615
<li><a href="#Deploy_JavaEE_Kubernetes_Multiple_Config">10.4. Deploy Java EE Application (multiple configuration files)</a>
616616
<ul class="sectlevel3">
617617
<li><a href="#_start_mysql_pod">10.4.1. Start MySQL Pod</a></li>
618-
<li><a href="#_start_mysql_service">10.4.2. Start MySQL service</a></li>
618+
<li><a href="#Mysql_service_file">10.4.2. Start MySQL service</a></li>
619619
<li><a href="#_start_wildfly_replication_controller">10.4.3. Start WildFly Replication Controller</a></li>
620-
<li><a href="#Access_Kubernetes_Application_Node">10.4.4. Access the application (using node)</a></li>
621-
<li><a href="#Access_Kubernetes_Application_Proxy">10.4.5. Access the application (using proxy)</a></li>
620+
<li><a href="#Access_Kubernetes_Application_Node">10.4.4. Access the application (from inside a node)</a></li>
621+
<li><a href="#Access_Kubernetes_Application_Service">10.4.5. Access the application (using Services)</a></li>
622622
</ul>
623623
</li>
624624
<li><a href="#_deploy_java_ee_application_one_configuration_file">10.5. Deploy Java EE Application (one configuration file)</a></li>
@@ -2444,7 +2444,7 @@ <h3 id="_start_application_server">8.2. Start Application Server</h3>
24442444
<div class="listingblock">
24452445
<div class="content">
24462446
<pre class="CodeRay highlight"><code data-lang="text">mkdir deployments
2447-
chmod aw deployments</code></pre>
2447+
chmod a+w deployments</code></pre>
24482448
</div>
24492449
</div>
24502450
<div class="paragraph">
@@ -3760,7 +3760,7 @@ <h4 id="_start_mysql_pod">10.4.1. Start MySQL Pod</h4>
37603760
</div>
37613761
</div>
37623762
<div class="sect3">
3763-
<h4 id="_start_mysql_service">10.4.2. Start MySQL service</h4>
3763+
<h4 id="Mysql_service_file">10.4.2. Start MySQL service</h4>
37643764
<div class="olist arabic">
37653765
<ol class="arabic">
37663766
<li>
@@ -3843,15 +3843,15 @@ <h4 id="_start_wildfly_replication_controller">10.4.3. Start WildFly Replication
38433843
<ol class="arabic">
38443844
<li>
38453845
<p>Start WildFly replication controller:</p>
3846+
</li>
3847+
</ol>
3848+
</div>
38463849
<div class="listingblock">
38473850
<div class="content">
38483851
<pre class="CodeRay highlight"><code data-lang="text">./cluster/kubectl.sh create -f ../../attendees/kubernetes/app-wildfly-rc.yaml
38493852
replicationcontrollers/wildfly-rc</code></pre>
38503853
</div>
38513854
</div>
3852-
</li>
3853-
</ol>
3854-
</div>
38553855
<div class="paragraph">
38563856
<p>It uses the following configuration file:</p>
38573857
</div>
@@ -3908,7 +3908,7 @@ <h4 id="_start_wildfly_replication_controller">10.4.3. Start WildFly Replication
39083908
</div>
39093909
</div>
39103910
<div class="sect3">
3911-
<h4 id="Access_Kubernetes_Application_Node">10.4.4. Access the application (using node)</h4>
3911+
<h4 id="Access_Kubernetes_Application_Node">10.4.4. Access the application (from inside a node)</h4>
39123912
<div class="olist arabic">
39133913
<ol class="arabic">
39143914
<li>
@@ -3937,9 +3937,101 @@ <h4 id="Access_Kubernetes_Application_Node">10.4.4. Access the application (usin
39373937
</div>
39383938
</div>
39393939
<div class="sect3">
3940-
<h4 id="Access_Kubernetes_Application_Proxy">10.4.5. Access the application (using proxy)</h4>
3940+
<h4 id="Access_Kubernetes_Application_Service">10.4.5. Access the application (using Services)</h4>
3941+
<div class="paragraph">
3942+
<p>For some parts of your application (e.g. frontends) you may want to expose a Service onto an external (outside of your cluster, maybe public internet) IP address, other services should be visible only from inside of the cluster.</p>
3943+
</div>
3944+
<div class="paragraph">
3945+
<p>Kubernetes ServiceTypes allow you to specify what kind of service you want. The default and base type is ClusterIP, which exposes a service to connection from inside the cluster. NodePort and LoadBalancer are two types that expose services to external traffic.</p>
3946+
</div>
3947+
<div class="paragraph">
3948+
<p>Valid values for the ServiceType field are:</p>
3949+
</div>
3950+
<div class="ulist">
3951+
<ul>
3952+
<li>
3953+
<p><strong>ClusterIP</strong>: use a cluster-internal IP only - this is the default and is discussed above. Choosing this value means that you want this service to be reachable only from inside of the cluster.</p>
3954+
</li>
3955+
<li>
3956+
<p><strong>NodePort</strong>: on top of having a cluster-internal IP, expose the service on a port on each node of the cluster (the same port on each node). You&#8217;ll be able to contact the service on any &lt;NodeIP&gt;:NodePort address.</p>
3957+
</li>
3958+
<li>
3959+
<p><strong>LoadBalancer</strong>: on top of having a cluster-internal IP and exposing service on a NodePort also, ask the cloud provider for a load balancer which forwards to the Service exposed as a &lt;NodeIP&gt;:NodePort for each Node.</p>
3960+
</li>
3961+
</ul>
3962+
</div>
3963+
<div class="paragraph">
3964+
<p>As you could see on <a href="#Mysql_service_file">Start MySQL service</a> section, the MySql service file doesn&#8217;t contain any ServiceType field. For that reason, the default value is <strong>ClusterIP</strong> which means that the MySql service can be accessed inside the Cluster.</p>
3965+
</div>
3966+
<div class="paragraph">
3967+
<p>In the other hand, not all Kubernetes Provider supports the <strong>LoadBalancer</strong> type. On cloud providers which support external load balancers, setting the type field to "LoadBalancer" will provision a load balancer for your Service.</p>
3968+
</div>
3969+
<div class="paragraph">
3970+
<p>Do show how to access WildFly outside Kubernetes, we will need to use <strong>NodePort</strong> type which exposes your service on an external port on all nodes in your cluster.</p>
3971+
</div>
3972+
<div class="paragraph">
3973+
<p>Let&#8217;s see how it works.</p>
3974+
</div>
3975+
<div class="olist arabic">
3976+
<ol class="arabic">
3977+
<li>
3978+
<p>Create a WildFly service of NodePort type</p>
3979+
</li>
3980+
</ol>
3981+
</div>
3982+
<div class="listingblock">
3983+
<div class="content">
3984+
<pre class="CodeRay highlight"><code data-lang="text">./cluster/kubectl.sh create -f ../../attendees/kubernetes/app-wildfly-service.yaml
3985+
You have exposed your service on an external port on all nodes in your
3986+
cluster. If you want to expose this service to the external internet, you may
3987+
need to set up firewall rules for the service port(s) (tcp:30140) to serve traffic.
3988+
3989+
See http://releases.k8s.io/HEAD/docs/user-guide/services-firewalls.md for more details.
3990+
services/wildfly-service</code></pre>
3991+
</div>
3992+
</div>
3993+
<div class="paragraph">
3994+
<p>Note that it will reply with the port that was opened on each node of the cluster. Now you can use the combination between the node ip and the service port to access the service.</p>
3995+
</div>
3996+
<div class="olist arabic">
3997+
<ol class="arabic">
3998+
<li>
3999+
<p>Get the IP of the node</p>
4000+
</li>
4001+
</ol>
4002+
</div>
4003+
<div class="listingblock">
4004+
<div class="content">
4005+
<pre class="CodeRay highlight"><code data-lang="text">cluster/kubectl.sh get nodes
4006+
NAME LABELS STATUS
4007+
10.245.1.3 kubernetes.io/hostname=10.245.1.3 Ready</code></pre>
4008+
</div>
4009+
</div>
4010+
<div class="olist arabic">
4011+
<ol class="arabic">
4012+
<li>
4013+
<p>Get the PORT of the service</p>
4014+
</li>
4015+
</ol>
4016+
</div>
4017+
<div class="listingblock">
4018+
<div class="content">
4019+
<pre class="CodeRay highlight"><code data-lang="text">cluster/kubectl.sh describe se wildfly-service
4020+
Name: wildfly-service
4021+
Namespace: default
4022+
Labels: context=docker-k8s-lab,name=wildfly
4023+
Selector: context=docker-k8s-lab,name=wildfly
4024+
Type: NodePort
4025+
IP: 10.247.39.239
4026+
Port: &lt;unnamed&gt; 8080/TCP
4027+
NodePort: &lt;unnamed&gt; 30140/TCP
4028+
Endpoints: 10.246.88.7:8080
4029+
Session Affinity: None
4030+
No events.</code></pre>
4031+
</div>
4032+
</div>
39414033
<div class="paragraph">
3942-
<p>Send a PR for <a href="https://github.com/javaee-samples/docker-java/issues/80" class="bare">https://github.com/javaee-samples/docker-java/issues/80</a></p>
4034+
<p>Note the <em>NodePort</em> value and now you can access the URL: <a href="http://10.245.1.3:30140/employees/" class="bare">http://10.245.1.3:30140/employees/</a></p>
39434035
</div>
39444036
</div>
39454037
</div>
@@ -4018,6 +4110,7 @@ <h3 id="_deploy_java_ee_application_one_configuration_file">10.5. Deploy Java EE
40184110
<span class="key">metadata</span>:
40194111
<span class="key">labels</span>:
40204112
<span class="key">name</span>: <span class="string"><span class="content">wildfly</span></span>
4113+
<span class="key">context</span>: <span class="string"><span class="content">docker-k8s-lab</span></span>
40214114
<span class="key">spec</span>:
40224115
<span class="key">containers</span>:
40234116
- <span class="string"><span class="content">name: wildfly-rc-pod</span></span>
@@ -4047,7 +4140,7 @@ <h3 id="_deploy_java_ee_application_one_configuration_file">10.5. Deploy Java EE
40474140
<div class="olist arabic">
40484141
<ol class="arabic">
40494142
<li>
4050-
<p>Access the application using <a href="#Access_Kubernetes_Application_Node">Access the application (using node)</a> or <a href="#Access_Kubernetes_Application_Proxy">Access the application (using proxy)</a>.</p>
4143+
<p>Access the application using <a href="#Access_Kubernetes_Application_Node">Access the application (from inside a node)</a> or <a href="#Access_Kubernetes_Application_Service">Access the application (using Services)</a>.</p>
40514144
</li>
40524145
</ol>
40534146
</div>

0 commit comments

Comments
 (0)