Skip to content

Commit

Permalink
Add notebook on how to create a Service
Browse files Browse the repository at this point in the history
  • Loading branch information
djkonro committed Jun 9, 2017
1 parent 93f3f94 commit 5c46d43
Showing 1 changed file with 248 additions and 0 deletions.
248 changes: 248 additions & 0 deletions examples/notebooks/create_service.ipynb
@@ -0,0 +1,248 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How to create a Service\n",
"=============\n",
"\n",
"In this notebook, we show you how to create a [Service](https://kubernetes.io/docs/concepts/services-networking/service/). \n",
"A Service in Kubernetes is a REST object, similar to a Pod. It is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from kubernetes import client, config"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load config from default location"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"config.load_kube_config()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create an instance of the API class"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"api_instance = client.CoreV1Api()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create Service object"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"service = client.V1Service()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fill required Service fields (apiVersion, kind, and metadata)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"service.api_version = 'v1'\n",
"service.kind = 'Service'\n",
"service.metadata = client.V1ObjectMeta(name='my-service')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Provide Service .spec description\n",
"Set Service object named **my-service** to target TCP port **9376** on any Pod with the **app=MyApp** label."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"spec = client.V1ServiceSpec()\n",
"spec.selector = {'app': 'MyApp'}\n",
"spec.ports = [client.V1ServicePort(protocol='TCP', port=80, target_port=9376)]\n",
"service.spec = spec"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create Service"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'api_version': 'v1',\n",
" 'kind': 'Service',\n",
" 'metadata': {'annotations': None,\n",
" 'cluster_name': None,\n",
" 'creation_timestamp': datetime.datetime(2017, 6, 9, 12, 33, 25, tzinfo=tzlocal()),\n",
" 'deletion_grace_period_seconds': None,\n",
" 'deletion_timestamp': None,\n",
" 'finalizers': None,\n",
" 'generate_name': None,\n",
" 'generation': None,\n",
" 'labels': None,\n",
" 'name': 'my-service',\n",
" 'namespace': 'default',\n",
" 'owner_references': None,\n",
" 'resource_version': '251538',\n",
" 'self_link': '/api/v1/namespaces/default/services/my-service',\n",
" 'uid': 'd572699d-4d0f-11e7-a819-0800277d3a21'},\n",
" 'spec': {'cluster_ip': '10.0.0.133',\n",
" 'deprecated_public_i_ps': None,\n",
" 'external_i_ps': None,\n",
" 'external_name': None,\n",
" 'load_balancer_ip': None,\n",
" 'load_balancer_source_ranges': None,\n",
" 'ports': [{'name': None,\n",
" 'node_port': None,\n",
" 'port': 80,\n",
" 'protocol': 'TCP',\n",
" 'target_port': '9376'}],\n",
" 'selector': {u'app': 'MyApp'},\n",
" 'session_affinity': 'None',\n",
" 'type': 'ClusterIP'},\n",
" 'status': {'load_balancer': {'ingress': None}}}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"api_instance.create_namespaced_service(namespace='default', body=service)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Delete Service"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'api_version': 'v1',\n",
" 'code': 200,\n",
" 'details': None,\n",
" 'kind': 'Status',\n",
" 'message': None,\n",
" 'metadata': {'resource_version': None, 'self_link': None},\n",
" 'reason': None,\n",
" 'status': 'Success'}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"api_instance.delete_namespaced_service(name='my-service', namespace='default')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 5c46d43

Please sign in to comment.