blob: 8df1d5d1015a13208057059fcc318123adce1b3a [file] [log] [blame]
"use strict";(self.webpackChunkwebsite_next=self.webpackChunkwebsite_next||[]).push([[6295],{15680:(e,n,t)=>{t.d(n,{xA:()=>c,yg:()=>m});var a=t(96540);function r(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function o(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);n&&(a=a.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,a)}return t}function s(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?o(Object(t),!0).forEach((function(n){r(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):o(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function l(e,n){if(null==e)return{};var t,a,r=function(e,n){if(null==e)return{};var t,a,r={},o=Object.keys(e);for(a=0;a<o.length;a++)t=o[a],n.indexOf(t)>=0||(r[t]=e[t]);return r}(e,n);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(a=0;a<o.length;a++)t=o[a],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(r[t]=e[t])}return r}var i=a.createContext({}),u=function(e){var n=a.useContext(i),t=n;return e&&(t="function"==typeof e?e(n):s(s({},n),e)),t},c=function(e){var n=u(e.components);return a.createElement(i.Provider,{value:n},e.children)},p="mdxType",d={inlineCode:"code",wrapper:function(e){var n=e.children;return a.createElement(a.Fragment,{},n)}},g=a.forwardRef((function(e,n){var t=e.components,r=e.mdxType,o=e.originalType,i=e.parentName,c=l(e,["components","mdxType","originalType","parentName"]),p=u(t),g=r,m=p["".concat(i,".").concat(g)]||p[g]||d[g]||o;return t?a.createElement(m,s(s({ref:n},c),{},{components:t})):a.createElement(m,s({ref:n},c))}));function m(e,n){var t=arguments,r=n&&n.mdxType;if("string"==typeof e||r){var o=t.length,s=new Array(o);s[0]=g;var l={};for(var i in n)hasOwnProperty.call(n,i)&&(l[i]=n[i]);l.originalType=e,l[p]="string"==typeof e?e:r,s[1]=l;for(var u=2;u<o;u++)s[u]=t[u];return a.createElement.apply(null,s)}return a.createElement.apply(null,t)}g.displayName="MDXCreateElement"},63224:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>i,contentTitle:()=>s,default:()=>d,frontMatter:()=>o,metadata:()=>l,toc:()=>u});var a=t(58168),r=(t(96540),t(15680));const o={id:"getting-started-docker",title:"Run a standalone Pulsar cluster in Docker",sidebar_label:"Run Pulsar in Docker"},s=void 0,l={unversionedId:"getting-started-docker",id:"version-3.0.x/getting-started-docker",title:"Run a standalone Pulsar cluster in Docker",description:"For local development and testing, you can run Pulsar in standalone mode on your own machine within a Docker container.",source:"@site/versioned_docs/version-3.0.x/getting-started-docker.md",sourceDirName:".",slug:"/getting-started-docker",permalink:"/docs/3.0.x/getting-started-docker",draft:!1,editUrl:"https://github.com/apache/pulsar-site/edit/main/versioned_docs/version-3.0.x/getting-started-docker.md",tags:[],version:"3.0.x",frontMatter:{id:"getting-started-docker",title:"Run a standalone Pulsar cluster in Docker",sidebar_label:"Run Pulsar in Docker"},sidebar:"docsSidebar",previous:{title:"Run Pulsar locally",permalink:"/docs/3.0.x/getting-started-standalone"},next:{title:"Run Pulsar in Kubernetes",permalink:"/docs/3.0.x/getting-started-helm"}},i={},u=[{value:"Start Pulsar in Docker",id:"start-pulsar-in-docker",level:2},{value:"Use Pulsar in Docker",id:"use-pulsar-in-docker",level:2},{value:"Consume a message",id:"consume-a-message",level:3},{value:"Produce a message",id:"produce-a-message",level:3},{value:"Get the topic statistics",id:"get-the-topic-statistics",level:2}],c={toc:u},p="wrapper";function d(e){let{components:n,...t}=e;return(0,r.yg)(p,(0,a.A)({},c,t,{components:n,mdxType:"MDXLayout"}),(0,r.yg)("p",null,"For local development and testing, you can run Pulsar in standalone mode on your own machine within a Docker container."),(0,r.yg)("p",null,"If you have not installed Docker, download it following ",(0,r.yg)("a",{parentName:"p",href:"https://docs.docker.com/get-docker/"},"the instructions")," for your OS."),(0,r.yg)("h2",{id:"start-pulsar-in-docker"},"Start Pulsar in Docker"),(0,r.yg)("p",null,"For macOS, Linux, and Windows, run the following command to start Pulsar within a Docker container."),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-shell"},"docker run -it -p 6650:6650 -p 8080:8080 --mount source=pulsardata,target=/pulsar/data --mount source=pulsarconf,target=/pulsar/conf apachepulsar/pulsar:3.0.4 bin/pulsar standalone\n")),(0,r.yg)("admonition",{type:"tip"},(0,r.yg)("p",{parentName:"admonition"},"You may encounter issues with the default RocksDB metadata store."),(0,r.yg)("p",{parentName:"admonition"},"We recommend you consider using the following environment variable to use ZooKeeper as the metadata store:"),(0,r.yg)("pre",{parentName:"admonition"},(0,r.yg)("code",{parentName:"pre"},"...\n-e PULSAR_STANDALONE_USE_ZOOKEEPER=1 \\\n...\n")),(0,r.yg)("p",{parentName:"admonition"},"Don't apply this fix for existing Pulsar standalone instances if you don't want to loose your data.")),(0,r.yg)("p",null,"If you want to change Pulsar configurations and start Pulsar, run the following command by passing environment variables with the ",(0,r.yg)("inlineCode",{parentName:"p"},"PULSAR_PREFIX_")," prefix. See ",(0,r.yg)("a",{parentName:"p",href:"https://github.com/apache/pulsar/blob/e6b12c64b043903eb5ff2dc5186fe8030f157cfc/conf/standalone.conf"},"default configuration file")," for more details."),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-shell"},'docker run -it -e PULSAR_PREFIX_xxx=yyy -p 6650:6650 -p 8080:8080 --mount source=pulsardata,target=/pulsar/data --mount source=pulsarconf,target=/pulsar/conf apachepulsar/pulsar:3.0.4 sh -c "bin/apply-config-from-env.py conf/standalone.conf && bin/pulsar standalone"\n')),(0,r.yg)("admonition",{type:"tip"},(0,r.yg)("ul",{parentName:"admonition"},(0,r.yg)("li",{parentName:"ul"},"The docker container runs as UID 10000 and GID 0 by default. You need to ensure the mounted volumes give write permission to either UID 10000 or GID 0. Note that UID 10000 is arbitrary, so it is recommended to make these mounts writable for the root group (GID 0)."),(0,r.yg)("li",{parentName:"ul"},'The data, metadata, and configuration are persisted on Docker volumes to not start "fresh" every time the container is restarted. For details on the volumes, you can use ',(0,r.yg)("inlineCode",{parentName:"li"},"docker volume inspect <sourcename>"),"."),(0,r.yg)("li",{parentName:"ul"},"For Docker on Windows, make sure to configure it to use Linux containers."))),(0,r.yg)("p",null,"After starting Pulsar successfully, you can see ",(0,r.yg)("inlineCode",{parentName:"p"},"INFO"),"-level log messages like this:"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre"},"08:18:30.970 [main] INFO org.apache.pulsar.broker.web.WebService - HTTP Service started at http://0.0.0.0:8080\n...\n07:53:37.322 [main] INFO org.apache.pulsar.broker.PulsarService - messaging service is ready, bootstrap service port = 8080, broker url= pulsar://localhost:6650, cluster=standalone, configs=org.apache.pulsar.broker.ServiceConfiguration@98b63c1\n...\n")),(0,r.yg)("admonition",{type:"tip"},(0,r.yg)("ul",{parentName:"admonition"},(0,r.yg)("li",{parentName:"ul"},"To perform a health check, you can use the ",(0,r.yg)("inlineCode",{parentName:"li"},"bin/pulsar-admin brokers healthcheck")," command. For more information, see ",(0,r.yg)("a",{parentName:"li",href:"pathname:///reference/#/3.0.x/pulsar-admin/"},"Pulsar admin docs"),"."),(0,r.yg)("li",{parentName:"ul"},"When you start a local standalone cluster, a ",(0,r.yg)("inlineCode",{parentName:"li"},"public/default")," namespace is created automatically. The namespace is used for development purposes. All Pulsar topics are managed within namespaces. For more information, see ",(0,r.yg)("a",{parentName:"li",href:"/docs/3.0.x/concepts-messaging#topics"},"Topics"),"."))),(0,r.yg)("h2",{id:"use-pulsar-in-docker"},"Use Pulsar in Docker"),(0,r.yg)("p",null,"Pulsar offers a variety of ",(0,r.yg)("a",{parentName:"p",href:"/docs/3.0.x/client-libraries"},"client libraries"),", such as ",(0,r.yg)("a",{parentName:"p",href:"/docs/3.0.x/client-libraries-java"},"Java"),", ",(0,r.yg)("a",{parentName:"p",href:"/docs/3.0.x/client-libraries-go"},"Go"),", ",(0,r.yg)("a",{parentName:"p",href:"/docs/3.0.x/client-libraries-python"},"Python"),", ",(0,r.yg)("a",{parentName:"p",href:"/docs/3.0.x/client-libraries-cpp"},"C++"),"."),(0,r.yg)("p",null,"If you're running a local standalone cluster, you can use one of these root URLs to interact with your cluster:"),(0,r.yg)("ul",null,(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("inlineCode",{parentName:"li"},"pulsar://localhost:6650")),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("inlineCode",{parentName:"li"},"http://localhost:8080"))),(0,r.yg)("p",null,"The following example guides you to get started with Pulsar by using the ",(0,r.yg)("a",{parentName:"p",href:"https://pulsar.apache.org/api/python/3.5.x"},"Python client API"),"."),(0,r.yg)("p",null,"Install the Pulsar Python client library directly from ",(0,r.yg)("a",{parentName:"p",href:"https://pypi.org/project/pulsar-client/"},"PyPI"),":"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-shell"},"pip install pulsar-client\n")),(0,r.yg)("h3",{id:"consume-a-message"},"Consume a message"),(0,r.yg)("p",null,"Create a consumer and subscribe to the topic:"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-python"},"import pulsar\n\nclient = pulsar.Client('pulsar://localhost:6650')\nconsumer = client.subscribe('my-topic', subscription_name='my-sub')\n\nwhile True:\n msg = consumer.receive()\n print(\"Received message: '%s'\" % msg.data())\n consumer.acknowledge(msg)\n\nclient.close()\n")),(0,r.yg)("h3",{id:"produce-a-message"},"Produce a message"),(0,r.yg)("p",null,"Start a producer to send some test messages:"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-python"},"import pulsar\n\nclient = pulsar.Client('pulsar://localhost:6650')\nproducer = client.create_producer('my-topic')\n\nfor i in range(10):\n producer.send(('hello-pulsar-%d' % i).encode('utf-8'))\n\nclient.close()\n")),(0,r.yg)("h2",{id:"get-the-topic-statistics"},"Get the topic statistics"),(0,r.yg)("p",null,"In Pulsar, you can use REST API, Java, or command-line tools to control every aspect of the system. For details on APIs, refer to ",(0,r.yg)("a",{parentName:"p",href:"/docs/3.0.x/admin-api-overview"},"Admin API Overview"),"."),(0,r.yg)("p",null,"In the simplest example, you can use curl to probe the stats for a particular topic:"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-shell"},"curl http://localhost:8080/admin/v2/persistent/public/default/my-topic/stats | python -m json.tool\n")),(0,r.yg)("p",null,"The output is something like this:"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-json"},'{\n "msgRateIn": 0.0,\n "msgThroughputIn": 0.0,\n "msgRateOut": 1.8332950480217471,\n "msgThroughputOut": 91.33142602871978,\n "bytesInCounter": 7097,\n "msgInCounter": 143,\n "bytesOutCounter": 6607,\n "msgOutCounter": 133,\n "averageMsgSize": 0.0,\n "msgChunkPublished": false,\n "storageSize": 7097,\n "backlogSize": 0,\n "offloadedStorageSize": 0,\n "publishers": [\n {\n "accessMode": "Shared",\n "msgRateIn": 0.0,\n "msgThroughputIn": 0.0,\n "averageMsgSize": 0.0,\n "chunkedMessageRate": 0.0,\n "producerId": 0,\n "metadata": {},\n "address": "/127.0.0.1:35604",\n "connectedSince": "2021-07-04T09:05:43.04788Z",\n "clientVersion": "2.8.0",\n "producerName": "standalone-2-5"\n }\n ],\n "waitingPublishers": 0,\n "subscriptions": {\n "my-sub": {\n "msgRateOut": 1.8332950480217471,\n "msgThroughputOut": 91.33142602871978,\n "bytesOutCounter": 6607,\n "msgOutCounter": 133,\n "msgRateRedeliver": 0.0,\n "chunkedMessageRate": 0,\n "msgBacklog": 0,\n "backlogSize": 0,\n "msgBacklogNoDelayed": 0,\n "blockedSubscriptionOnUnackedMsgs": false,\n "msgDelayed": 0,\n "unackedMessages": 0,\n "type": "Exclusive",\n "activeConsumerName": "3c544f1daa",\n "msgRateExpired": 0.0,\n "totalMsgExpired": 0,\n "lastExpireTimestamp": 0,\n "lastConsumedFlowTimestamp": 1625389101290,\n "lastConsumedTimestamp": 1625389546070,\n "lastAckedTimestamp": 1625389546162,\n "lastMarkDeleteAdvancedTimestamp": 1625389546163,\n "consumers": [\n {\n "msgRateOut": 1.8332950480217471,\n "msgThroughputOut": 91.33142602871978,\n "bytesOutCounter": 6607,\n "msgOutCounter": 133,\n "msgRateRedeliver": 0.0,\n "chunkedMessageRate": 0.0,\n "consumerName": "3c544f1daa",\n "availablePermits": 867,\n "unackedMessages": 0,\n "avgMessagesPerEntry": 6,\n "blockedConsumerOnUnackedMsgs": false,\n "lastAckedTimestamp": 1625389546162,\n "lastConsumedTimestamp": 1625389546070,\n "metadata": {},\n "address": "/127.0.0.1:35472",\n "connectedSince": "2021-07-04T08:58:21.287682Z",\n "clientVersion": "2.8.0"\n }\n ],\n "isDurable": true,\n "isReplicated": false,\n "allowOutOfOrderDelivery": false,\n "consumersAfterMarkDeletePosition": {},\n "nonContiguousDeletedMessagesRanges": 0,\n "nonContiguousDeletedMessagesRangesSerializedSize": 0,\n "durable": true,\n "replicated": false\n }\n },\n "replication": {},\n "deduplicationStatus": "Disabled",\n "nonContiguousDeletedMessagesRanges": 0,\n "nonContiguousDeletedMessagesRangesSerializedSize": 0\n}\n')))}d.isMDXComponent=!0}}]);