@@ -7,7 +7,7 @@ min-kubernetes-server-version: v1.10
7
7
8
8
<!-- overview -->
9
9
10
- This page shows how to use ` kubectl port-forward ` to connect to a Redis
10
+ This page shows how to use ` kubectl port-forward ` to connect to a MongoDB
11
11
server running in a Kubernetes cluster. This type of connection can be useful
12
12
for database debugging.
13
13
@@ -19,25 +19,25 @@ for database debugging.
19
19
20
20
* {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
21
21
22
- * Install [ redis-cli ] ( http ://redis.io/topics/rediscli ) .
22
+ * Install [ MongoDB Shell ] ( https ://www.mongodb.com/try/download/shell ) .
23
23
24
24
25
25
26
26
27
27
<!-- steps -->
28
28
29
- ## Creating Redis deployment and service
29
+ ## Creating MongoDB deployment and service
30
30
31
- 1 . Create a Deployment that runs Redis :
31
+ 1 . Create a Deployment that runs MongoDB :
32
32
33
33
``` shell
34
- kubectl apply -f https://k8s.io/examples/application/guestbook/redis-master -deployment.yaml
34
+ kubectl apply -f https://k8s.io/examples/application/guestbook/mongo -deployment.yaml
35
35
```
36
36
37
37
The output of a successful command verifies that the deployment was created:
38
38
39
39
```
40
- deployment.apps/redis-master created
40
+ deployment.apps/mongo created
41
41
```
42
42
43
43
View the pod status to check that it is ready:
@@ -49,8 +49,8 @@ for database debugging.
49
49
The output displays the pod created:
50
50
51
51
```
52
- NAME READY STATUS RESTARTS AGE
53
- redis-master-765d459796-258hz 1/1 Running 0 50s
52
+ NAME READY STATUS RESTARTS AGE
53
+ mongo-75f59d57f4-4nd6q 1/1 Running 0 2m4s
54
54
```
55
55
56
56
View the Deployment' s status:
@@ -62,8 +62,8 @@ for database debugging.
62
62
The output displays that the Deployment was created:
63
63
64
64
```
65
- NAME READY UP-TO-DATE AVAILABLE AGE
66
- redis-master 1/1 1 1 55s
65
+ NAME READY UP-TO-DATE AVAILABLE AGE
66
+ mongo 1/1 1 1 2m21s
67
67
```
68
68
69
69
The Deployment automatically manages a ReplicaSet.
@@ -76,90 +76,90 @@ for database debugging.
76
76
The output displays that the ReplicaSet was created:
77
77
78
78
```
79
- NAME DESIRED CURRENT READY AGE
80
- redis-master-765d459796 1 1 1 1m
79
+ NAME DESIRED CURRENT READY AGE
80
+ mongo-75f59d57f4 1 1 1 3m12s
81
81
```
82
82
83
83
84
- 2. Create a Service to expose Redis on the network:
84
+ 2. Create a Service to expose MongoDB on the network:
85
85
86
86
```shell
87
- kubectl apply -f https://k8s.io/examples/application/guestbook/redis-master -service.yaml
87
+ kubectl apply -f https://k8s.io/examples/application/guestbook/mongo -service.yaml
88
88
```
89
89
90
90
The output of a successful command verifies that the Service was created:
91
91
92
92
```
93
- service/redis-master created
93
+ service/mongo created
94
94
```
95
95
96
96
Check the Service created:
97
97
98
98
```shell
99
- kubectl get service redis-master
99
+ kubectl get service mongo
100
100
```
101
101
102
102
The output displays the service created:
103
103
104
104
```
105
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
106
- redis-master ClusterIP 10.0.0.213 <none> 6379 /TCP 27s
105
+ NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
106
+ mongo ClusterIP 10.96.41.183 <none> 27017 /TCP 11s
107
107
```
108
108
109
- 3. Verify that the Redis server is running in the Pod, and listening on port 6379 :
109
+ 3. Verify that the MongoDB server is running in the Pod, and listening on port 27017 :
110
110
111
111
```shell
112
- # Change redis-master-765d459796-258hz to the name of the Pod
113
- kubectl get pod redis-master-765d459796-258hz --template=' {{(index (index .spec.containers 0).ports 0).containerPort}}{{" \n" }}'
112
+ # Change mongo-75f59d57f4-4nd6q to the name of the Pod
113
+ kubectl get pod mongo-75f59d57f4-4nd6q --template=' {{(index (index .spec.containers 0).ports 0).containerPort}}{{" \n" }}'
114
114
```
115
115
116
- The output displays the port for Redis in that Pod:
116
+ The output displays the port for MongoDB in that Pod:
117
117
118
118
```
119
- 6379
119
+ 27017
120
120
```
121
121
122
- (this is the TCP port allocated to Redis on the internet).
122
+ (this is the TCP port allocated to MongoDB on the internet).
123
123
124
124
## Forward a local port to a port on the Pod
125
125
126
126
1. `kubectl port-forward` allows using resource name, such as a pod name, to select a matching pod to port forward to.
127
127
128
128
129
129
```shell
130
- # Change redis-master-765d459796-258hz to the name of the Pod
131
- kubectl port-forward redis-master-765d459796-258hz 7000:6379
130
+ # Change mongo-75f59d57f4-4nd6q to the name of the Pod
131
+ kubectl port-forward mongo-75f59d57f4-4nd6q 28015:27017
132
132
```
133
133
134
134
which is the same as
135
135
136
136
```shell
137
- kubectl port-forward pods/redis-master-765d459796-258hz 7000:6379
137
+ kubectl port-forward pods/mongo-75f59d57f4-4nd6q 28015:27017
138
138
```
139
139
140
140
or
141
141
142
142
```shell
143
- kubectl port-forward deployment/redis-master 7000:6379
143
+ kubectl port-forward deployment/mongo 28015:27017
144
144
```
145
145
146
146
or
147
147
148
148
```shell
149
- kubectl port-forward replicaset/redis-master 7000:6379
149
+ kubectl port-forward replicaset/mongo-75f59d57f4 28015:27017
150
150
```
151
151
152
152
or
153
153
154
154
```shell
155
- kubectl port-forward service/redis-master 7000:redis
155
+ kubectl port-forward service/mongo 28015:27017
156
156
```
157
157
158
158
Any of the above commands works. The output is similar to this:
159
159
160
160
```
161
- Forwarding from 127.0.0.1:7000 -> 6379
162
- Forwarding from [::1]:7000 -> 6379
161
+ Forwarding from 127.0.0.1:28015 -> 27017
162
+ Forwarding from [::1]:28015 -> 27017
163
163
```
164
164
165
165
{{< note >}}
@@ -168,22 +168,22 @@ for database debugging.
168
168
169
169
{{< /note >}}
170
170
171
- 2. Start the Redis command line interface:
171
+ 2. Start the MongoDB command line interface:
172
172
173
173
```shell
174
- redis-cli -p 7000
174
+ mongosh --port 28015
175
175
```
176
176
177
- 3. At the Redis command line prompt, enter the `ping` command:
177
+ 3. At the MongoDB command line prompt, enter the `ping` command:
178
178
179
179
```
180
- ping
180
+ db.runCommand( { ping: 1 } )
181
181
```
182
182
183
183
A successful ping request returns:
184
184
185
185
```
186
- PONG
186
+ { ok: 1 }
187
187
```
188
188
189
189
### Optionally let _kubectl_ choose the local port {#let-kubectl-choose-local-port}
@@ -193,24 +193,31 @@ the local port and thus relieve you from having to manage local port conflicts,
193
193
the slightly simpler syntax:
194
194
195
195
` ` ` shell
196
- kubectl port-forward deployment/redis-master :6379
196
+ kubectl port-forward deployment/mongo :27017
197
+ ` ` `
198
+
199
+ The output is similar to this:
200
+
201
+ ```
202
+ Forwarding from 127.0.0.1:63753 -> 27017
203
+ Forwarding from [ ::1] :63753 -> 27017
197
204
```
198
205
199
206
The `kubectl` tool finds a local port number that is not in use (avoiding low ports numbers,
200
207
because these might be used by other applications). The output is similar to:
201
208
202
209
```
203
- Forwarding from 127.0.0.1:62162 -> 6379
204
- Forwarding from [ ::1] :62162 -> 6379
210
+ Forwarding from 127.0.0.1:63753 -> 27017
211
+ Forwarding from [ ::1] :63753 -> 27017
205
212
```
206
213
207
214
208
215
<!-- discussion -->
209
216
210
217
## Discussion
211
218
212
- Connections made to local port 7000 are forwarded to port 6379 of the Pod that
213
- is running the Redis server. With this connection in place, you can use your
219
+ Connections made to local port 28015 are forwarded to port 27017 of the Pod that
220
+ is running the MongoDB server. With this connection in place, you can use your
214
221
local workstation to debug the database that is running in the Pod.
215
222
216
223
{{< note >}}
0 commit comments