Skip to content

Commit

Permalink
deploy to kubernetes and use kubernetes service discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
felix committed Dec 29, 2021
1 parent 3c4520c commit 773dc0c
Show file tree
Hide file tree
Showing 12 changed files with 507 additions and 24 deletions.
2 changes: 1 addition & 1 deletion date-api/dataApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {

service := micro.NewService(
micro.Server(srv),
micro.Registry(mesh.GetConsulRegistry()),
micro.Registry(mesh.GetRegistry()),
)
service.Init()
service.Run()
Expand Down
19 changes: 16 additions & 3 deletions date-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/FelixAnna/web-service-dlw/date-api
go 1.17

require (
github.com/FelixAnna/web-service-dlw/common v0.0.0-20211222162226-5a4d1c2d50bc
github.com/FelixAnna/web-service-dlw/common v0.0.0-20211228152933-29d52f28e6b8
github.com/gin-gonic/gin v1.7.7
github.com/golang-module/carbon/v2 v2.0.1
go-micro.dev/v4 v4.4.0
Expand All @@ -15,14 +15,19 @@ require (
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/asim/go-micro/plugins/registry/consul/v4 v4.0.0-20211210113221-37de747d195c // indirect
github.com/asim/go-micro/plugins/registry/etcd/v4 v4.0.0-20211221131829-7af0eb4b7ab7 // indirect
github.com/asim/go-micro/plugins/registry/kubernetes/v4 v4.0.0-20211221131829-7af0eb4b7ab7 // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/hashicorp/consul/api v1.9.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
Expand All @@ -48,9 +53,17 @@ require (
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/urfave/cli/v2 v2.3.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.0 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.0 // indirect
go.etcd.io/etcd/client/v3 v3.5.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand All @@ -66,14 +79,14 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 // indirect
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

Expand Down
105 changes: 101 additions & 4 deletions date-api/go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion date-api/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ docker image ls

### Tag

docker image tag date-api:latest date-api:v1.0
docker image tag date-api:latest date-api:1.0.0

### Run

Expand Down
184 changes: 184 additions & 0 deletions deployment/kubernetes/deployment_dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# 1. create namespace
apiVersion: v1
kind: Namespace
metadata:
name: dlw-dev
labels:
environment: development
resourceVersion : "2"

---
# 2. create ResourceQuota for the namespace
apiVersion: v1
kind: ResourceQuota
metadata:
name: dlw-dev-quota
namespace: dlw-dev
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "2"
limits.memory: 4Gi

---
# 3.1 create roles for access pods
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: dlw-service-role
namespace: dlw-dev
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "patch"] # You can also use ["*"]

---
# 3.2 bind role to the default service account in the namespace
apiVersion: rbac.authorization.k8s.io/v1
# This role binding allows "jane" to read pods in the "default" namespace.
# You need to already have a Role named "pod-reader" in that namespace.
kind: RoleBinding
metadata:
name: dlw-service-role-binding
namespace: dlw-dev
subjects:
# You can specify more than one "subject"
- kind: User
name: system:serviceaccount:dlw-dev:default # "name" is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
# "roleRef" specifies the binding to a Role / ClusterRole
kind: Role #this must be Role or ClusterRole
name: dlw-service-role # this must match the name of the Role or ClusterRole you wish to bind to
apiGroup: rbac.authorization.k8s.io

---
# 4.1 create deployment with pod spec for date-api service
apiVersion: apps/v1
kind: Deployment
metadata:
name: dlw-deployment-dev-date
namespace: dlw-dev
spec:
replicas: 2
selector:
matchLabels:
app: date-api
template:
metadata:
labels:
app: date-api
spec:
containers:
- name: date-api
image: date-api:1.0.0
ports:
- containerPort: 8383
env:
- name: AWS_ACCESS_KEY_ID
value: "xx"
- name: AWS_SECRET_ACCESS_KEY
value: "yy"
- name: AWS_REGION
value: "ap-southeast-1"
resources:
requests:
memory: 256Mi
cpu: "128m"
limits:
memory: 512Mi
cpu: "256m"

---
# 4.2 create deployment with pod spec for memo-api service
apiVersion: apps/v1
kind: Deployment
metadata:
name: dlw-deployment-dev-memo
namespace: dlw-dev
spec:
replicas: 2
selector:
matchLabels:
app: memo-api
template:
metadata:
labels:
app: memo-api
spec:
containers:
- name: memo-api
image: memo-api:1.0.0
ports:
- containerPort: 8282
env:
- name: AWS_ACCESS_KEY_ID
value: "xx"
- name: AWS_SECRET_ACCESS_KEY
value: "yy"
- name: AWS_REGION
value: "ap-southeast-1"
resources:
requests:
memory: 256Mi
cpu: "128m"
limits:
memory: 512Mi
cpu: "256m"

---
# 4.3 create deployment with pod spec for user-api service
apiVersion: apps/v1
kind: Deployment
metadata:
name: dlw-deployment-dev-user
namespace: dlw-dev
spec:
replicas: 2
selector:
matchLabels:
app: user-api
template:
metadata:
labels:
app: user-api
spec:
containers:
- name: user-api
image: user-api:1.0.0
ports:
- containerPort: 8181
env:
- name: AWS_ACCESS_KEY_ID
value: "xx"
- name: AWS_SECRET_ACCESS_KEY
value: "yy"
- name: AWS_REGION
value: "ap-southeast-1"
resources:
requests:
memory: 256Mi
cpu: "128m"
limits:
memory: 512Mi
cpu: "256m"

---
# 5. create service for with NodePort exposed
apiVersion: v1
kind: Service
metadata:
name: dlw-service
namespace: dlw-dev
spec:
type: NodePort #default is ClusterIP which is only accessible inside kubernetes
selector:
app: date-api
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8383 # can use port name reference or port number, container name is more flexible as we can change port number
nodePort: 30080 #default range from: 30000 - 32767

20 changes: 16 additions & 4 deletions memo-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
//github.com/FelixAnna/web-service-dlw/common v0.0.0-00010101000000-000000000000
github.com/FelixAnna/web-service-dlw/common v0.0.0-20211228152933-29d52f28e6b8
github.com/asim/go-micro/plugins/client/http/v4 v4.0.0-20211210113221-37de747d195c
github.com/asim/go-micro/plugins/registry/consul/v4 v4.0.0-20211210113221-37de747d195c
github.com/asim/go-micro/plugins/server/http/v4 v4.0.0-20211210113221-37de747d195c
Expand All @@ -12,14 +13,16 @@ require (
go-micro.dev/v4 v4.4.0
)

require github.com/FelixAnna/web-service-dlw/common v0.0.0-20211222162226-5a4d1c2d50bc

require (
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/asim/go-micro/plugins/registry/etcd/v4 v4.0.0-20211221131829-7af0eb4b7ab7 // indirect
github.com/asim/go-micro/plugins/registry/kubernetes/v4 v4.0.0-20211221131829-7af0eb4b7ab7 // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fatih/color v1.9.0 // indirect
Expand All @@ -31,6 +34,7 @@ require (
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.2.0 // indirect
Expand All @@ -44,7 +48,7 @@ require (
github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
Expand All @@ -65,11 +69,19 @@ require (
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/urfave/cli/v2 v2.3.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.0 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.0 // indirect
go.etcd.io/etcd/client/v3 v3.5.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 // indirect
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
Loading

0 comments on commit 773dc0c

Please sign in to comment.