|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "How to create a Service\n", |
| 8 | + "=============\n", |
| 9 | + "\n", |
| 10 | + "In this notebook, we show you how to create a [Service](https://kubernetes.io/docs/concepts/services-networking/service/). \n", |
| 11 | + "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." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 1, |
| 17 | + "metadata": { |
| 18 | + "collapsed": true |
| 19 | + }, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "from kubernetes import client, config" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "metadata": {}, |
| 28 | + "source": [ |
| 29 | + "### Load config from default location" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": 2, |
| 35 | + "metadata": { |
| 36 | + "collapsed": true |
| 37 | + }, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "config.load_kube_config()" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "markdown", |
| 45 | + "metadata": {}, |
| 46 | + "source": [ |
| 47 | + "### Create an instance of the API class" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": 3, |
| 53 | + "metadata": { |
| 54 | + "collapsed": true |
| 55 | + }, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "api_instance = client.CoreV1Api()" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "metadata": {}, |
| 64 | + "source": [ |
| 65 | + "### Create Service object" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 4, |
| 71 | + "metadata": { |
| 72 | + "collapsed": true |
| 73 | + }, |
| 74 | + "outputs": [], |
| 75 | + "source": [ |
| 76 | + "service = client.V1Service()" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "### Fill required Service fields (apiVersion, kind, and metadata)" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 5, |
| 89 | + "metadata": { |
| 90 | + "collapsed": true |
| 91 | + }, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "service.api_version = 'v1'\n", |
| 95 | + "service.kind = 'Service'\n", |
| 96 | + "service.metadata = client.V1ObjectMeta(name='my-service')" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "metadata": {}, |
| 102 | + "source": [ |
| 103 | + "### Provide Service .spec description\n", |
| 104 | + "Set Service object named **my-service** to target TCP port **9376** on any Pod with the **app=MyApp** label." |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 6, |
| 110 | + "metadata": { |
| 111 | + "collapsed": true |
| 112 | + }, |
| 113 | + "outputs": [], |
| 114 | + "source": [ |
| 115 | + "spec = client.V1ServiceSpec()\n", |
| 116 | + "spec.selector = {'app': 'MyApp'}\n", |
| 117 | + "spec.ports = [client.V1ServicePort(protocol='TCP', port=80, target_port=9376)]\n", |
| 118 | + "service.spec = spec" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "markdown", |
| 123 | + "metadata": {}, |
| 124 | + "source": [ |
| 125 | + "### Create Service" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": 7, |
| 131 | + "metadata": { |
| 132 | + "collapsed": false |
| 133 | + }, |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "data": { |
| 137 | + "text/plain": [ |
| 138 | + "{'api_version': 'v1',\n", |
| 139 | + " 'kind': 'Service',\n", |
| 140 | + " 'metadata': {'annotations': None,\n", |
| 141 | + " 'cluster_name': None,\n", |
| 142 | + " 'creation_timestamp': datetime.datetime(2017, 6, 9, 12, 33, 25, tzinfo=tzlocal()),\n", |
| 143 | + " 'deletion_grace_period_seconds': None,\n", |
| 144 | + " 'deletion_timestamp': None,\n", |
| 145 | + " 'finalizers': None,\n", |
| 146 | + " 'generate_name': None,\n", |
| 147 | + " 'generation': None,\n", |
| 148 | + " 'labels': None,\n", |
| 149 | + " 'name': 'my-service',\n", |
| 150 | + " 'namespace': 'default',\n", |
| 151 | + " 'owner_references': None,\n", |
| 152 | + " 'resource_version': '251538',\n", |
| 153 | + " 'self_link': '/api/v1/namespaces/default/services/my-service',\n", |
| 154 | + " 'uid': 'd572699d-4d0f-11e7-a819-0800277d3a21'},\n", |
| 155 | + " 'spec': {'cluster_ip': '10.0.0.133',\n", |
| 156 | + " 'deprecated_public_i_ps': None,\n", |
| 157 | + " 'external_i_ps': None,\n", |
| 158 | + " 'external_name': None,\n", |
| 159 | + " 'load_balancer_ip': None,\n", |
| 160 | + " 'load_balancer_source_ranges': None,\n", |
| 161 | + " 'ports': [{'name': None,\n", |
| 162 | + " 'node_port': None,\n", |
| 163 | + " 'port': 80,\n", |
| 164 | + " 'protocol': 'TCP',\n", |
| 165 | + " 'target_port': '9376'}],\n", |
| 166 | + " 'selector': {u'app': 'MyApp'},\n", |
| 167 | + " 'session_affinity': 'None',\n", |
| 168 | + " 'type': 'ClusterIP'},\n", |
| 169 | + " 'status': {'load_balancer': {'ingress': None}}}" |
| 170 | + ] |
| 171 | + }, |
| 172 | + "execution_count": 7, |
| 173 | + "metadata": {}, |
| 174 | + "output_type": "execute_result" |
| 175 | + } |
| 176 | + ], |
| 177 | + "source": [ |
| 178 | + "api_instance.create_namespaced_service(namespace='default', body=service)" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "markdown", |
| 183 | + "metadata": {}, |
| 184 | + "source": [ |
| 185 | + "### Delete Service" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "execution_count": 8, |
| 191 | + "metadata": { |
| 192 | + "collapsed": false |
| 193 | + }, |
| 194 | + "outputs": [ |
| 195 | + { |
| 196 | + "data": { |
| 197 | + "text/plain": [ |
| 198 | + "{'api_version': 'v1',\n", |
| 199 | + " 'code': 200,\n", |
| 200 | + " 'details': None,\n", |
| 201 | + " 'kind': 'Status',\n", |
| 202 | + " 'message': None,\n", |
| 203 | + " 'metadata': {'resource_version': None, 'self_link': None},\n", |
| 204 | + " 'reason': None,\n", |
| 205 | + " 'status': 'Success'}" |
| 206 | + ] |
| 207 | + }, |
| 208 | + "execution_count": 8, |
| 209 | + "metadata": {}, |
| 210 | + "output_type": "execute_result" |
| 211 | + } |
| 212 | + ], |
| 213 | + "source": [ |
| 214 | + "api_instance.delete_namespaced_service(name='my-service', namespace='default')" |
| 215 | + ] |
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "code", |
| 219 | + "execution_count": null, |
| 220 | + "metadata": { |
| 221 | + "collapsed": true |
| 222 | + }, |
| 223 | + "outputs": [], |
| 224 | + "source": [] |
| 225 | + } |
| 226 | + ], |
| 227 | + "metadata": { |
| 228 | + "kernelspec": { |
| 229 | + "display_name": "Python 2", |
| 230 | + "language": "python", |
| 231 | + "name": "python2" |
| 232 | + }, |
| 233 | + "language_info": { |
| 234 | + "codemirror_mode": { |
| 235 | + "name": "ipython", |
| 236 | + "version": 2 |
| 237 | + }, |
| 238 | + "file_extension": ".py", |
| 239 | + "mimetype": "text/x-python", |
| 240 | + "name": "python", |
| 241 | + "nbconvert_exporter": "python", |
| 242 | + "pygments_lexer": "ipython2", |
| 243 | + "version": "2.7.13" |
| 244 | + } |
| 245 | + }, |
| 246 | + "nbformat": 4, |
| 247 | + "nbformat_minor": 2 |
| 248 | +} |
0 commit comments