Skip to content

Instantly share code, notes, and snippets.

@viglesiasce
Last active October 6, 2023 08:22
Show Gist options
  • Save viglesiasce/bcdacc95068803be565a45b215bf069f to your computer and use it in GitHub Desktop.
Save viglesiasce/bcdacc95068803be565a45b215bf069f to your computer and use it in GitHub Desktop.
GKE gRPC Ingress
"""The Python implementation of the GRPC helloworld.Greeter server."""
from concurrent import futures
import time
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
import SimpleHTTPServer
import SocketServer
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
def SayHelloAgain(self, request, context):
return helloworld_pb2.HelloReply(
message='Hello again, %s!' % request.name)
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051')
server.start()
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", 8080), Handler)
print "serving at port", 8080
httpd.serve_forever()
# gRPC starts a new thread to service requests. Just make the main thread
# sleep.
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(grace=0)
if __name__ == '__main__':
serve()
# Copyright 2017 Google Inc.
#
# Licensed 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.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: grpc-ssl-demo
annotations:
kubernetes.io/ingress.global-static-ip-name: grpc-ssl-demo
kubernetes.io/ingress.allow-http: "false"
ingress.gcp.kubernetes.io/pre-shared-cert: grpc-ssl-demo
spec:
tls:
- hosts:
- grpc-ssl.1.vic.dev
secretName: grpc-ssl-demo
rules:
- host: grpc-ssl.1.vic.dev
http:
paths:
- backend:
serviceName: grpc-hello
servicePort: grpc
---
apiVersion: v1
kind: Service
metadata:
name: grpc-hello
annotations:
cloud.google.com/app-protocols: '{"grpc":"HTTP2", "hc-web": "HTTP"}'
service.alpha.kubernetes.io/app-protocols: '{"grpc":"HTTP2", "hc-web": "HTTP"}'
spec:
ports:
- port: 50051
targetPort: 50051
protocol: TCP
name: grpc
- name: hc-web
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: grpc-hello
type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: grpc-hello
spec:
replicas: 1
template:
metadata:
labels:
app: grpc-hello
spec:
containers:
- name: python-grpc-hello
image: gcr.io/vic-goog/python-grpc-hello:1.1
imagePullPolicy: Always
ports:
- containerPort: 50051
- containerPort: 8080
readinessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment