@@ -11,180 +11,169 @@ 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
14
-
15
-
16
-
17
14
## {{% heading "prerequisites" %}}
18
15
19
-
20
16
* {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
21
-
22
17
* Install [ MongoDB Shell] ( https://www.mongodb.com/try/download/shell ) .
23
18
24
-
25
-
26
-
27
19
<!-- steps -->
28
20
29
21
## Creating MongoDB deployment and service
30
22
31
23
1 . Create a Deployment that runs MongoDB:
32
24
33
- ``` shell
34
- kubectl apply -f https://k8s.io/examples/application/mongodb/mongo-deployment.yaml
35
- ```
36
-
37
- The output of a successful command verifies that the deployment was created:
25
+ ``` shell
26
+ kubectl apply -f https://k8s.io/examples/application/mongodb/mongo-deployment.yaml
27
+ ```
38
28
39
- ```
40
- deployment.apps/mongo created
41
- ```
29
+ The output of a successful command verifies that the deployment was created:
42
30
43
- View the pod status to check that it is ready:
31
+ ```
32
+ deployment.apps/mongo created
33
+ ```
44
34
45
- ` ` ` shell
46
- kubectl get pods
47
- ` ` `
35
+ View the pod status to check that it is ready:
48
36
49
- The output displays the pod created:
37
+ ``` shell
38
+ kubectl get pods
39
+ ```
50
40
51
- ```
52
- NAME READY STATUS RESTARTS AGE
53
- mongo-75f59d57f4-4nd6q 1/1 Running 0 2m4s
54
- ```
41
+ The output displays the pod created:
55
42
56
- View the Deployment' s status:
43
+ ```
44
+ NAME READY STATUS RESTARTS AGE
45
+ mongo-75f59d57f4-4nd6q 1/1 Running 0 2m4s
46
+ ```
57
47
58
- ```shell
59
- kubectl get deployment
60
- ```
48
+ View the Deployment's status:
61
49
62
- The output displays that the Deployment was created:
50
+ ``` shell
51
+ kubectl get deployment
52
+ ```
63
53
64
- ```
65
- NAME READY UP-TO-DATE AVAILABLE AGE
66
- mongo 1/1 1 1 2m21s
67
- ```
54
+ The output displays that the Deployment was created:
68
55
69
- The Deployment automatically manages a ReplicaSet.
70
- View the ReplicaSet status using:
56
+ ```
57
+ NAME READY UP-TO-DATE AVAILABLE AGE
58
+ mongo 1/1 1 1 2m21s
59
+ ```
71
60
72
- ```shell
73
- kubectl get replicaset
74
- ```
61
+ The Deployment automatically manages a ReplicaSet.
62
+ View the ReplicaSet status using:
75
63
76
- The output displays that the ReplicaSet was created:
64
+ ``` shell
65
+ kubectl get replicaset
66
+ ```
77
67
78
- ```
79
- NAME DESIRED CURRENT READY AGE
80
- mongo-75f59d57f4 1 1 1 3m12s
81
- ```
68
+ The output displays that the ReplicaSet was created:
82
69
70
+ ```
71
+ NAME DESIRED CURRENT READY AGE
72
+ mongo-75f59d57f4 1 1 1 3m12s
73
+ ```
83
74
84
75
2 . Create a Service to expose MongoDB on the network:
85
76
86
- ```shell
87
- kubectl apply -f https://k8s.io/examples/application/mongodb/mongo-service.yaml
88
- ```
77
+ ``` shell
78
+ kubectl apply -f https://k8s.io/examples/application/mongodb/mongo-service.yaml
79
+ ```
89
80
90
- The output of a successful command verifies that the Service was created:
81
+ The output of a successful command verifies that the Service was created:
91
82
92
- ```
93
- service/mongo created
94
- ```
83
+ ```
84
+ service/mongo created
85
+ ```
95
86
96
- Check the Service created:
87
+ Check the Service created:
97
88
98
- ```shell
99
- kubectl get service mongo
100
- ```
89
+ ``` shell
90
+ kubectl get service mongo
91
+ ```
101
92
102
- The output displays the service created:
93
+ The output displays the service created:
103
94
104
- ```
105
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
106
- mongo ClusterIP 10.96.41.183 <none> 27017/TCP 11s
107
- ```
95
+ ```
96
+ NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
97
+ mongo ClusterIP 10.96.41.183 <none> 27017/TCP 11s
98
+ ```
108
99
109
100
3 . Verify that the MongoDB server is running in the Pod, and listening on port 27017:
110
101
111
- ```shell
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
- ```
102
+ ``` shell
103
+ # Change mongo-75f59d57f4-4nd6q to the name of the Pod
104
+ kubectl get pod mongo-75f59d57f4-4nd6q --template=' {{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
105
+ ```
115
106
116
- The output displays the port for MongoDB in that Pod:
107
+ The output displays the port for MongoDB in that Pod:
117
108
118
- ```
119
- 27017
120
- ```
109
+ ```
110
+ 27017
111
+ ```
121
112
122
- (this is the TCP port allocated to MongoDB on the internet) .
113
+ 27017 is the TCP port allocated to MongoDB on the internet.
123
114
124
115
## Forward a local port to a port on the Pod
125
116
126
- 1. `kubectl port-forward` allows using resource name, such as a pod name, to select a matching pod to port forward to.
117
+ 1 . ` kubectl port-forward ` allows using resource name, such as a pod name, to select a matching pod to port forward to.
127
118
128
119
129
- ```shell
130
- # Change mongo-75f59d57f4-4nd6q to the name of the Pod
131
- kubectl port-forward mongo-75f59d57f4-4nd6q 28015:27017
132
- ```
120
+ ``` shell
121
+ # Change mongo-75f59d57f4-4nd6q to the name of the Pod
122
+ kubectl port-forward mongo-75f59d57f4-4nd6q 28015:27017
123
+ ```
133
124
134
- which is the same as
125
+ which is the same as
135
126
136
- ```shell
137
- kubectl port-forward pods/mongo-75f59d57f4-4nd6q 28015:27017
138
- ```
127
+ ``` shell
128
+ kubectl port-forward pods/mongo-75f59d57f4-4nd6q 28015:27017
129
+ ```
139
130
140
- or
131
+ or
141
132
142
- ```shell
143
- kubectl port-forward deployment/mongo 28015:27017
144
- ```
133
+ ``` shell
134
+ kubectl port-forward deployment/mongo 28015:27017
135
+ ```
145
136
146
- or
137
+ or
147
138
148
- ```shell
149
- kubectl port-forward replicaset/mongo-75f59d57f4 28015:27017
150
- ```
139
+ ``` shell
140
+ kubectl port-forward replicaset/mongo-75f59d57f4 28015:27017
141
+ ```
151
142
152
- or
143
+ or
153
144
154
- ```shell
155
- kubectl port-forward service/mongo 28015:27017
156
- ```
145
+ ``` shell
146
+ kubectl port-forward service/mongo 28015:27017
147
+ ```
157
148
158
- Any of the above commands works. The output is similar to this:
149
+ Any of the above commands works. The output is similar to this:
159
150
160
- ```
161
- Forwarding from 127.0.0.1:28015 -> 27017
162
- Forwarding from [::1]:28015 -> 27017
163
- ```
151
+ ```
152
+ Forwarding from 127.0.0.1:28015 -> 27017
153
+ Forwarding from [::1]:28015 -> 27017
154
+ ```
164
155
165
- {{< note >}}
166
-
167
- `kubectl port-forward` does not return. To continue with the exercises, you will need to open another terminal.
156
+ {{< note >}}
157
+ ` kubectl port-forward ` does not return. To continue with the exercises, you will need to open another terminal.
158
+ {{< /note >}}
168
159
169
- {{< /note >}}
160
+ 2 . Start the MongoDB command line interface:
170
161
171
- 2. Start the MongoDB command line interface:
162
+ ``` shell
163
+ mongosh --port 28015
164
+ ```
172
165
173
- ```shell
174
- mongosh --port 28015
175
- ```
166
+ 3 . At the MongoDB command line prompt, enter the ` ping ` command:
176
167
177
- 3. At the MongoDB command line prompt, enter the `ping` command:
168
+ ```
169
+ db.runCommand( { ping: 1 } )
170
+ ```
178
171
179
- ```
180
- db.runCommand( { ping: 1 } )
181
- ```
172
+ A successful ping request returns:
182
173
183
- A successful ping request returns:
184
-
185
- ```
186
- { ok: 1 }
187
- ```
174
+ ```
175
+ { ok: 1 }
176
+ ```
188
177
189
178
### Optionally let _ kubectl_ choose the local port {#let-kubectl-choose-local-port}
190
179
@@ -204,7 +193,6 @@ Forwarding from 127.0.0.1:63753 -> 27017
204
193
Forwarding from [::1]:63753 -> 27017
205
194
```
206
195
207
-
208
196
<!-- discussion -->
209
197
210
198
## Discussion
@@ -219,9 +207,7 @@ The support for UDP protocol is tracked in
219
207
[ issue 47862] ( https://github.com/kubernetes/kubernetes/issues/47862 ) .
220
208
{{< /note >}}
221
209
222
-
223
-
224
-
225
210
## {{% heading "whatsnext" %}}
226
211
227
212
Learn more about [ kubectl port-forward] ( /docs/reference/generated/kubectl/kubectl-commands/#port-forward ) .
213
+
0 commit comments