Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Dev. env. container-per-worker implementation for Kubernetes - #11419

Merged
cihangir merged 8 commits into
koding:masterfrom
mertaytore:container-per-worker
Oct 23, 2017
Merged

cihangir merged 8 commits into
koding:masterfrom
mertaytore:container-per-worker

Conversation

@mertaytore

@mertaytore mertaytore commented Aug 29, 2017 •

Copy link
Copy Markdown
Contributor

All workers run on different containers in backend pod, landing & client builds are done in frontend pod and external services (rabbitmq, postgres, redis, countly & mongo) run on their respective replication controllers for development environment.

Description

Details on some modifications:
generateKonfig.coffee: Kubernetes' API runs on 8080. Therefore, webserver needs to run on another port, configured to be 8040 for this implementation.
nginx.coffee: Added port 8040 to webserver's upstream for nginx.conf generation. This addition will provide the port for both build types, docker-compose and k8s.
bootstrap-container: Added build_k8s. bootstrap-container build command was not removed because the command can potentially be used in GitLab integration
kontrol.go: Kontrol is mislead by the environment variables that are generated by the Kubernetes service resource exposing Kontrol. Kontrol's environment loader tries to load KONTROL_PORT=tcp://<clusterIP-of-kontrol>:3000 as kontrol's port value but it fails since the value is not an integer to be parsed. This change did not result in any errors when Koding was built with docker-compose.

The change regarding kontrol.go is open for other workarounds.

Motivation and Context

This implementation is worked on top of #11338 with no supervisor execution.

How Has This Been Tested?

No warnings or errors occurred on coffeelint executions on the .coffee files that were modified

Types of changes

  • New feature (non-breaking change which adds functionality)

@mertaytore
mertaytore requested review from cihangir and szkl August 29, 2017 11:42
Comment thread config/generateRunFile.coffee Outdated

sleep 60

envsubst < deployment/kubernetes/build-pod.yaml.template > deployment/kubernetes/build-pod.yaml

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this on previous PR. Could you use (or eliminate the necessity) something else other than envsubst MacOS does not come with it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make it a required dependency. It is a part of gettext, actually. So, it might not be installed by GNU+Linux distros. What do you think?

brew install gettext
brew link --force gettext

It is located in $BREW_PREFIX/opt/gettext/bin/envsubst.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might even not need this if we start working generating templates from JSON objects related

Comment thread config/main.dev.coffee Outdated
KONFIG.supervisorConf = (require '../deployment/supervisord.coffee').create KONFIG
KONFIG.kubernetesConf = (require '../deployment/kubernetes.coffee').create KONFIG
for name, options of KONFIG.workers when options.kubernetes?.ports?
fs.writeFileSync "./deployment/kubernetes/backend-pod/#{name}-svc.yaml", (require '../deployment/kubernetes.coffee').createWorkerServices name, options

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you separate this into multiple lines where first you create the data to be written and write it later?

@cihangir cihangir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to work on the configuration.

Comment thread deployment/kubernetes.coffee Outdated
return mountConf


generateVolumesSection = ->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you generate this section from the KONFIG.workers.volumes?

Comment thread config/workers.coffee Outdated
kubernetes :
image : 'nginx'
command : " [ \"nginx\", \"-c\", \"#{KONFIG.projectRoot}/nginx.conf\" ] "
mounts : [ 'koding-working-tree', 'assets' ]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you define this mounts in KONFIG.worker as separate properties? We will have greater visibility in what we can mount in k8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While generating the mounts for kubernetes templates, check if the mount is pre-defined in KONFIG, if not fail.

Comment thread config/workers.coffee Outdated
ports :
containerPort : "#{KONFIG.kontrol.port}"
hostPort : "#{KONFIG.kontrol.port}"
envVariables : [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont need to define every env vars as key-value pairs. Just define the required konfig params and read them from KONFIG.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also while reading from KONFIG, check if the required env var is in KONFIG, if not fail.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also consider re-configuring once service host is set.

Comment thread config/workers.coffee Outdated
versionURL : "http://localhost:#{KONFIG.kloud.port}/version"
kubernetes :
image : 'koding/base'
command : " [ \"./run\", \"exec\", \"#{GOBIN_K8S}/kloud\" ] "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ps: You can use """ to remove double quote escaping.

Comment thread config/workers.coffee Outdated
mounts : [ 'koding-working-tree', 'root-kite-volume', 'generated-volume' ]
ports:
containerPort : "#{KONFIG.kloud.port}"
hostPort : "#{KONFIG.kloud.port}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can eliminate the host port in next iteration, we wont need it when we have service descriptions in place.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until then you can get rid of redundant quotes.

Comment thread config/workers.coffee
healthCheckURLs : [ "http://localhost:#{KONFIG.kloud.port}/healthCheck" ]
versionURL : "http://localhost:#{KONFIG.kloud.port}/version"
kubernetes :
image : 'koding/base'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not need the command section here. This worker definition already has it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies for ports section too.

Comment thread config/workers.coffee Outdated
mounts : [ 'koding-working-tree', 'root-kite-volume' ]
ports :
containerPort : '4560'
hostPort : '4560'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much space.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My drunk coder detector is detecting a drunk coder :trollface:

Comment thread deployment/kubernetes.coffee Outdated

containerSection =
"""
\n - name: #{container.name}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont need to work with strings. First generate an object then use a yaml generator to convert that to yaml.

Comment thread deployment/nginx.coffee Outdated
servers += '\n' if servers isnt ''
port = parseInt(port, 10)

if name is 'webserver' and options.kubernetes.ports?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the place where we should handle port swapping. Please pass a new flag to configure and do this in first step.

Comment thread scripts/bootstrap-container Outdated
esac
}

function k8s_build() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please apply shfmt.

@szkl seems we need ci/check-lint from packer repo here as well...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted.

@szkl szkl left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many places repeating the same information.

Comment thread .gitignore Outdated

deployment/kubernetes/backend-pod/
deployment/kubernetes/frontend-pod/client-containers.yaml
deployment/kubernetes/build-pod.yaml

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think deployment/kubernetes would be enough.

Comment thread config/generateKonfig.coffee Outdated
nodejsRateLimiter : { enabled: no, guestRules: [{ interval: 3600, limit: 5000 }], userRules: [{ interval: 3600, limit: 10000 }] } # limit: request limit per rate limit window, interval: rate limit window duration in seconds
nodejsRateLimiterForApi : { enabled: yes, guestRules: [{ interval: 60, limit: 5 }], userRules: [{ interval: 60, limit: 60 }] } # limit: request limit per rate limit window, interval: rate limit window duration in seconds
webserver : { port: 8080 }
webserver : { port: 8080, k8sPort: 8040 }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No way, Jose.

Comment thread config/main.dev.coffee Outdated
sh: (require './generateShellEnv').create KONFIG, options
json: JSON.stringify KONFIG, null, 2

if not fs.existsSync './deployment/kubernetes/backend-pod' then fs.mkdirSync './deployment/kubernetes/backend-pod'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add that directory to repository with a file named .empty in it.

Comment thread config/workers.coffee Outdated
GOPATH = '%(ENV_KONFIG_PROJECTROOT)s/go'

GOBIN_K8S = "#{KONFIG.projectRoot}/go/bin"
GOPATH_K8S = "#{KONFIG.projectRoot}/go"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be avoided.

Comment thread config/workers.coffee Outdated
stopsignal : 'QUIT'
kubernetes :
image : 'nginx'
command : " [ \"nginx\", \"-c\", \"#{KONFIG.projectRoot}/nginx.conf\" ] "

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON in a string is a no-go.

Comment thread deployment/nginx.coffee Outdated

if name is 'webserver' and options.kubernetes.ports?
webserverPort = parseInt(options.kubernetes.ports.containerPort, 10)
servers += "\tserver 127.0.0.1:#{webserverPort + index} max_fails=3 fail_timeout=10s;\n"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically duplicating line 22.

Comment thread scripts/k8s-utilities.sh Outdated
# more info on guest user update: https://www.rabbitmq.com/blog/2014/04/02/breaking-things-with-rabbitmq-3-3/
k8s_health_check $RABBITMQ_POD_NAME koding 5 120 rabbitmq
sleep 10
kubectl exec -n koding $RABBITMQ_POD_NAME -c rabbitmq -- bash -c "rabbitmqctl add_user test test && rabbitmqctl set_user_tags test administrator && rabbitmqctl set_permissions -p / test '.*' '.*' '.*'"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break down into multiple lines.

Comment thread scripts/k8s-utilities.sh Outdated
else
echo "Unknown command: $1"
exit 1
fi No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a newline at the EOF.

Comment thread scripts/k8s-utilities.sh Outdated
kubectl delete -f $1
}

if [ "$1" == "k8s_connectivity_check" ]; then

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to add a conditional clause for each command. See if you can just find out if there is a function defined with the same name as "$1".

Comment thread config/workers.coffee
healthCheckURLs : [ "http://localhost:#{KONFIG.kloud.port}/healthCheck" ]
versionURL : "http://localhost:#{KONFIG.kloud.port}/version"
kubernetes :
image : 'koding/base'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies for ports section too.

@mertaytore
mertaytore force-pushed the container-per-worker branch 22 times, most recently from 6d836a1 to 14f9f1a Compare September 12, 2017 14:44

@cihangir cihangir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing blocker apart from -R and -r

Comment thread config/generateRunFile.coffee Outdated

export NAMESPACE_DIR="${KONFIG_PROJECTROOT}/deployment/kubernetes/namespace.yaml"
export BACKEND_DIR="${KONFIG_PROJECTROOT}/deployment/kubernetes/backend-pod/containers.yaml"
export EXT_SERVICES_DIR="${KONFIG_PROJECTROOT}/deployment/kubernetes/external-services/ -R"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this "-R" here?

Comment thread .env-kubernetes
@@ -1,15 +1,22 @@
export KONFIG_PROJECTROOT=/opt/koding

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may need to get rid of from this special config file sooner than it might seem ;)

Comment thread config/generateRunFile.coffee Outdated
envsubst < deployment/kubernetes/frontend-pod/client-containers.yaml.template > deployment/kubernetes/frontend-pod/client-containers.yaml

cp $BACKEND_DIR ${KONFIG_PROJECTROOT}/deployment/generated_files
cp $BACKEND_DIR ${KONFIG_PROJECTROOT}/deployment/generated_files -r

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this -r meant for recursive, please move it next to the command itself with its corresponding capital case.

kubectl exec -n $2 $1 -c $3 -- bash -c "./run is_ready" || exit 1
}
${KONFIG_PROJECTROOT}/scripts/k8s-utilities.sh create_k8s_resource ${KONFIG_PROJECTROOT}/deployment/kubernetes/external-services/mongo
export MONGO_POD_NAME=$(kubectl get pods --namespace koding -l "app=mongo-ext-service" -o jsonpath="{.items[0].metadata.name}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can create a new variable for general options that will be passed to each command ie: export KUBECTL_OPTS="--namespace koding" on your next iterations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted and will make the appropriate changes on the next iteration.

Comment thread scripts/k8s-utilities.sh Outdated
remaining_time=600

until eval "check_pod_response $1 $2 $3"; do
sleep 30

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

30 is a rather big number for an interval. 3-5 would be more suitable.

Comment thread config/generateRunFile.coffee Outdated
${KONFIG_PROJECTROOT}/scripts/k8s-utilities.sh create_k8s_resource ${KONFIG_PROJECTROOT}/deployment/kubernetes/external-services/countly
export COUNTLY_POD_NAME=$(kubectl get pods --namespace koding -l "app=countly-ext-service" -o jsonpath="{.items[0].metadata.name}")
${KONFIG_PROJECTROOT}/scripts/k8s-utilities.sh check_pod_state $COUNTLY_POD_NAME Pending
sleep 40

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we have sleeps around the code? check_pod_state already ensures that the state is in desired state.


KONFIG.kubernetesConf.spec.containers.push generateContainerSection name, workerOptions

KONFIG.kubernetesConf.spec.volumes = generateVolumesSection KONFIG, options

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should create the volumes spec from the KONFIG itself, not from your prior knowledge. This is the same reason that i commented earlier.

Anyways please create an issue to refactor these volume creation steps and we can leave this as is for now.

@mertaytore mertaytore Sep 13, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened an issue for it and will change it on the next iteration.

@@ -0,0 +1,25 @@
apiVersion: v1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ps: replication controller is shortened with "rc"

see your filename

@mertaytore
mertaytore force-pushed the container-per-worker branch 3 times, most recently from e949e6f to e01d3df Compare September 19, 2017 14:02
@mertaytore
mertaytore force-pushed the container-per-worker branch 6 times, most recently from d3aa997 to 6e7c59e Compare October 3, 2017 07:54
@mertaytore
mertaytore force-pushed the container-per-worker branch from 6e7c59e to 1f6c7ff Compare October 5, 2017 12:19
@tardyp

tardyp commented Oct 17, 2017

Copy link
Copy Markdown

Looks like this is useful. Can we merge it?

@cihangir
cihangir merged commit 0397e89 into koding:master Oct 23, 2017
@cihangir

Copy link
Copy Markdown
Contributor

@mertaytore you can send the fixes for this PR on your next PRs. ie: removing command section from kubernetes config in workers.coffee

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants