Skip to content

Commit

Permalink
Add injector example and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinX committed Dec 17, 2019
1 parent 4a189ad commit 1c6d417
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 0 deletions.
70 changes: 70 additions & 0 deletions docs/injection/injection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Masher injection

Mesher can be used with any application language on any infrastructure. On Kubernetes, we inject Mesher into Pods, and applications can take advantage of all its features. Below we introduce the use of Mesher injector by example.

## Before
1. Install Servcicecomb [service-center](http://servicecomb.apache.org/docs/service-center/install/#deployment-with-kubernetes)


2. Download the example of quick_start
```bash
$ git clone https://github.com/apache/servicecomb-mesher.git
$ cd ./servicecomb-mesher/examples/quick_start
```

3. Build docker images
```bash
$ cd ./mesher_injection
$ bash build_images.sh
```

## Automatic Mesher injection
Meshers can be automatically added to applicable Kubernetes pods using a mutating webhook admission controller provided by Mesher Injector.

1. Create namespace "svccomb-system"
```bash
cp ../../../deployments/kubernetes/injector/*.yaml .
$ kubectl apply -f svccomb-system.yaml
namespace/svccomb-system created
```
2. Generate Injector's certificatesigningrequest and secret
```bash
$ wget https://raw.githubusercontent.com/morvencao/kube-mutating-webhook-tutorial/master/deployment/webhook-create-signed-cert.sh
$ bash webhook-create-signed-cert.sh --service svccomb-mesher-injector --namespace svccomb-system --secret svccomb-mesher-injector-service-account
```
3. Query caBundle and fill it into "mesher-injector.yaml"
```bash
$ CA_BUNDLE=$(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}')
$ sed -i "s|\${CA_BUNDLE}|${CA_BUNDLE}|g" mesher-injector.yaml
```
4. Deploy mesher injecter
```bash
$ kubectl apply -f mesher-injector.yaml
mutatingwebhookconfiguration.admissionregistration.k8s.io/svccomb-mesher-injector configured
service/svccomb-mesher-injector created
deployment.extensions/svccomb-mesher-injector created
```
5. Deploy examples to Kubernetes
```bash
$ kubectl apply -f svccomb-test.yaml
namespace/svccomb-test created
$ kubectl -n svccomb-test apply -f calculator.yaml
service/calculator created
deployment.extensions/calculator-python created
$ kubectl -n svccomb-test apply -f webapp.yaml
service/webapp created
deployment.extensions/webapp-node created
```
6. Validated results
```bash
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
calculator ClusterIP 10.104.2.143 <none> 5000/TCP 3m43s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42d
webapp NodePort 10.104.134.148 <none> 5001:30062/TCP 3m35s
```
Open the page "http://127.0.0.1:30062" in your browser, enter your height and weight in the input boxes, and click the submit button, you can see the BMI results about you.
22 changes: 22 additions & 0 deletions examples/quick_start/mesher_injection/Dockerfile_calculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM python:2

WORKDIR /usr/src/app

COPY httpserver_calculator/ ./

CMD [ "python", "httpserver_calculator.py" ]
24 changes: 24 additions & 0 deletions examples/quick_start/mesher_injection/Dockerfile_webapp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM node:10.16.2

WORKDIR /home

COPY httpserver_webapp/ ./

RUN npm install

CMD [ "node", "httpserver_webapp.js" ]
29 changes: 29 additions & 0 deletions examples/quick_start/mesher_injection/build_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

echo "copy resource"
cp -r ../httpserver_* .

echo "build calculator image"
cp -f Dockerfile_calculator Dockerfile
docker build -t servicecomb/calculator-python:latest .

echo "build webapp image"
cp -f Dockerfile_webapp Dockerfile
docker build -t servicecomb/webapp-node:latest .

echo "clean"
rm -rf httpserver_* Dockerfile
96 changes: 96 additions & 0 deletions examples/quick_start/mesher_injection/mesher-bmi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# svccomb-test namespace
apiVersion: v1
kind: Namespace
metadata:
name: svccomb-test
labels:
svccomb-injection: enabled
---
# calculator service
apiVersion: v1
kind: Service
metadata:
name: calculator
namespace: svccomb-test
labels:
app: calculator
spec:
ports:
- port: 5000
name: http
targetPort: 4540
selector:
app: calculator
---
# calculator deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: calculator-python
namespace: svccomb-test
spec:
replicas: 1
template:
metadata:
labels:
app: calculator
spec:
containers:
- name: calculator
image: servicecomb/calculator-python:latest
imagePullPolicy: IfNotPresent #Always
ports:
- containerPort: 4540
---
# webapp service
apiVersion: v1
kind: Service
metadata:
name: webapp
namespace: svccomb-test
labels:
app: webapp
spec:
type: NodePort
ports:
- port: 5001
name: http
targetPort: 4597
nodePort: 30062
selector:
app: webapp
---
# webapp deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: webapp-node
namespace: svccomb-test
spec:
replicas: 1
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: servicecomb/webapp-node:latest
imagePullPolicy: IfNotPresent #Always
ports:
- containerPort: 4597

0 comments on commit 1c6d417

Please sign in to comment.