Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[k8s] Refactored k8s operator to use kopf for controller logic #15787

Merged
merged 44 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
8fb61df
Refactored k8s operator to use kopf for controller logic
tgaddair May 13, 2021
1956df6
Added role permissions
tgaddair May 13, 2021
6a2e28d
Fixed TODO
tgaddair May 13, 2021
b3ba4d9
Go back to global queue
tgaddair May 13, 2021
18108ff
Removed unused memo
tgaddair May 13, 2021
cbc24f4
Fix target
tgaddair May 13, 2021
82e117a
Merge branch 'master' into kopf
DmitriGekhtman May 20, 2021
e02046d
logistics
DmitriGekhtman May 20, 2021
31f9f2c
lint
DmitriGekhtman May 20, 2021
9cda7b3
Addressed comments
tgaddair May 21, 2021
3e78c5e
Typing
tgaddair May 21, 2021
d868fb7
Changed to use memo
tgaddair May 21, 2021
2cac877
queue to mp
tgaddair May 21, 2021
5997dea
lint
DmitriGekhtman May 21, 2021
a80e8be
Merge branch 'master' into kopf
DmitriGekhtman May 21, 2021
8c785c9
Merged master
tgaddair May 25, 2021
d1f8aec
Merge branch 'kopf' of https://github.com/tgaddair/ray into kopf
DmitriGekhtman May 25, 2021
fcf6a6b
update test requirments
DmitriGekhtman May 25, 2021
9011889
Skip operator type check
DmitriGekhtman May 25, 2021
fb1e4df
Type fiddling, imports
DmitriGekhtman May 25, 2021
ccfdc3a
No kopf in Ray 1.6
DmitriGekhtman May 26, 2021
93eca5f
Mock kopf in unit test
DmitriGekhtman May 26, 2021
b338026
format
DmitriGekhtman May 26, 2021
5a5e1cc
Remove kopf from requirements.txt
DmitriGekhtman May 26, 2021
5ac79c0
try dockerfile again
DmitriGekhtman May 26, 2021
2a4d53c
Fix requirements.txt
DmitriGekhtman May 26, 2021
d9a29f3
don't prune kopf status info
DmitriGekhtman May 27, 2021
94f0caf
wip
DmitriGekhtman May 27, 2021
c5723c1
wip
DmitriGekhtman May 27, 2021
39a83d9
Simplify code, fix status handling.
DmitriGekhtman May 27, 2021
53f5c1b
Space out retries in tests
DmitriGekhtman May 28, 2021
e256a43
CRD before operator in scale test
DmitriGekhtman May 28, 2021
f0fda95
newline
DmitriGekhtman May 28, 2021
1e0a33c
Update delete instructions. Kopf finalizer warning.
DmitriGekhtman May 28, 2021
a759b01
wip
DmitriGekhtman May 28, 2021
2c2e5fd
Test and doc update
DmitriGekhtman May 28, 2021
ae4310d
Merge branch 'master' into kopf
DmitriGekhtman May 28, 2021
936a269
Better subprocess clean-up
DmitriGekhtman May 29, 2021
2aace4c
Operator in main thread
DmitriGekhtman May 29, 2021
4ef291c
Teardown
DmitriGekhtman May 29, 2021
fb457b0
Wait for service deletion in test
DmitriGekhtman May 29, 2021
709b12c
Fix test, wait for teardown
DmitriGekhtman May 29, 2021
b92054e
Remove main
DmitriGekhtman May 29, 2021
19c7910
Sneak in doc fix + helm chart config consistency.
DmitriGekhtman May 30, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ci/travis/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ MYPY_FILES=(
'autoscaler/node_provider.py'
'autoscaler/sdk.py'
'autoscaler/_private/commands.py'
'ray_operator/operator.py'
# TODO(dmitri) Fails with meaningless error, maybe due to a bug in the mypy version
# in the CI. Type check once we get serious about type checking:
#'ray_operator/operator.py'
'ray_operator/operator_utils.py'
)

Expand Down
1 change: 1 addition & 0 deletions deploy/charts/ray/crds/cluster_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ spec:
properties:
status:
type: object
x-kubernetes-preserve-unknown-fields: true
properties:
phase:
description: Updating, Running, Error, or AutoscalingExceptionRecovery
Expand Down
3 changes: 3 additions & 0 deletions deploy/charts/ray/templates/operator_cluster_scoped.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ rules:
- apiGroups: ["", "cluster.ray.io"]
resources: ["rayclusters", "rayclusters/finalizers", "rayclusters/status", "pods", "pods/exec", "services"]
verbs: ["get", "watch", "list", "create", "delete", "patch", "update"]
- apiGroups: [""]
resources: [events]
verbs: [create]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
3 changes: 3 additions & 0 deletions deploy/charts/ray/templates/operator_namespaced.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ rules:
- apiGroups: ["", "cluster.ray.io"]
resources: ["rayclusters", "rayclusters/finalizers", "rayclusters/status", "pods", "pods/exec", "services"]
verbs: ["get", "watch", "list", "create", "delete", "patch", "update"]
- apiGroups: [""]
resources: [events]
verbs: [create]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
Expand Down
8 changes: 4 additions & 4 deletions deploy/charts/ray/templates/raycluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ spec:
name: dshm
resources:
requests:
cpu: {{ .numCPU }}
cpu: {{ .CPU }}
memory: {{ .memory }}
limits:
cpu: {{ .numCPU }}
cpu: {{ .CPU }}
# The maximum memory that this pod is allowed to use. The
# limit will be detected by ray and split to use 10% for
# redis, 30% for the shared memory object store, and the
Expand All @@ -73,8 +73,8 @@ spec:
# allocate a very large object store in each pod that may
# cause problems for other pods.
memory: {{ .memory }}
{{- if .numGPU }}
nvidia.com/gpu: {{ .numGPU }}
{{- if .GPU }}
nvidia.com/gpu: {{ .GPU }}
{{- end }}
{{- if .nodeSelector }}
nodeSelector:
Expand Down
22 changes: 12 additions & 10 deletions deploy/charts/ray/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ podTypes:
# No worker pods of this pod type (just the head). Thus, we set minWorkers and maxWorkers to 0.
minWorkers: 0
maxWorkers: 0
# numCPU is the number of CPUs used by this pod type.
# CPU is the number of CPUs used by this pod type.
# (Used for both requests and limits. Must be an integer, as Ray does not support fractional CPUs.)
numCPU: 1
CPU: 1
# memory is the memory used by this Pod type.
# (Used for both requests and limits.)
memory: 512Mi
# numGPU is the number of NVIDIA GPUs used by this pod type.
# GPU is the number of NVIDIA GPUs used by this pod type.
# (Optional, requires GPU nodes with appropriate setup. See https://docs.ray.io/en/master/cluster/kubernetes-gpu.html)
numGPU: 0
GPU: 0
# rayResources is an optional string-int mapping signalling additional resources to Ray.
# "CPU", "GPU", and "memory" are filled automatically based on the above settings.
# "CPU", "GPU", and "memory" are filled automatically based on the above settings, but can be overriden;
# For example, rayResources: {"CPU": 0} can be used in the head podType to prevent Ray from scheduling tasks on the head.
# See https://docs.ray.io/en/master/advanced.html#dynamic-remote-parameters for an example of usage of custom resources in a Ray task.
rayResources: {}
# Optionally, set a node selector for this podType: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#nodeselector
Expand All @@ -39,14 +40,15 @@ podTypes:
# memory is the memory used by this Pod type.
# (Used for both requests and limits.)
memory: 512Mi
# numCPU is the number of CPUs used by this pod type.
# CPU is the number of CPUs used by this pod type.
# (Used for both requests and limits. Must be an integer, as Ray does not support fractional CPUs.)
numCPU: 1
# numGPU is the number of NVIDIA GPUs used by this pod type.
CPU: 1
# GPU is the number of NVIDIA GPUs used by this pod type.
# (Optional, requires GPU nodes with appropriate setup. See https://docs.ray.io/en/master/cluster/kubernetes-gpu.html)
numGPU: 0
GPU: 0
# rayResources is an optional string-int mapping signalling additional resources to Ray.
# "CPU", "GPU", and "memory" are filled automatically based on the above settings.
# "CPU", "GPU", and "memory" are filled automatically based on the above settings, but can be overriden;
# For example, rayResources: {"CPU": 0} can be used in the head podType to prevent Ray from scheduling tasks on the head.
# See https://docs.ray.io/en/master/advanced.html#dynamic-remote-parameters for an example of usage of custom resources in a Ray task.
rayResources: {}
# Optionally, set a node selector for this Pod type. See https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#nodeselector
Expand Down
3 changes: 3 additions & 0 deletions deploy/components/operator_cluster_scoped.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ rules:
- apiGroups: ["", "cluster.ray.io"]
resources: ["rayclusters", "rayclusters/finalizers", "rayclusters/status", "pods", "pods/exec", "services"]
verbs: ["get", "watch", "list", "create", "delete", "patch", "update"]
- apiGroups: [""]
resources: [events]
verbs: [create]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
3 changes: 3 additions & 0 deletions deploy/components/operator_namespaced.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ rules:
- apiGroups: ["", "cluster.ray.io"]
resources: ["rayclusters", "rayclusters/finalizers", "rayclusters/status", "pods", "pods/exec", "services"]
verbs: ["get", "watch", "list", "create", "delete", "patch", "update"]
- apiGroups: [""]
resources: [events]
verbs: [create]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
Expand Down
30 changes: 27 additions & 3 deletions doc/source/cluster/kubernetes-advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The autoscaler will try to maintain at least ``minWorkers`` of the ``podType`` a
for the head ``podType``; this signals that the ``podType`` is to be used only for the head node.
You can use `helm upgrade`_ to adjust the fields ``minWorkers`` and ``maxWorkers`` without :ref:`restarting<k8s-restarts>` the Ray cluster.

The fields ``numCPU``, ``numGPU``, ``memory``, and ``nodeSelector`` configure the Kubernetes ``PodSpec`` to use for nodes
The fields ``CPU``, ``GPU``, ``memory``, and ``nodeSelector`` configure the Kubernetes ``PodSpec`` to use for nodes
of the ``podType``. The ``image`` field determines the Ray container image used by all nodes in the Ray cluster.

The ``rayResources`` field of each ``podType`` can be used to signal the presence of custom resources to Ray.
Expand All @@ -53,6 +53,10 @@ To schedule Ray tasks and actors that use custom hardware resources, ``rayResour
- Signal availability of the hardware for that ``podType`` with ``rayResources: {"custom_resource": 3}``.
- Schedule a Ray task or actor to use that resource with ``@ray.remote(resources={"custom_resource": 1})``.

By default, the fields ``CPU``, ``GPU``, and ``memory`` are used to configure cpu, gpu, and memory resources advertised to Ray.
However, ``rayResources`` can be used to override this behavior. For example, ``rayResources: {"CPU": 0}`` can be set for head podType,
to :ref:``avoid scheduling tasks on the Ray head``.

Refer to the documentation in `values.yaml`_ for more details.

.. note::
Expand Down Expand Up @@ -133,6 +137,25 @@ This string can be used to filter for a specific Ray cluster's logs:
$(kubectl get pod -l cluster.ray.io/component=operator -o custom-columns=:metadata.name) \
| grep example-cluster2,ray | tail -n 100

.. _k8s-cleanup:

Cleaning up resources
---------------------
When cleaning up,
**RayCluster resources must be deleted before the Operator deployment is deleted**.
This is because the Operator must remove a `finalizer`_ from the ``RayCluster`` resource to allow
deletion of the resource to complete.

If the Operator and ``RayCluster`` are created as part of the same Helm release,
the ``RayCluster`` must be deleted :ref:`before<k8s-cleanup-basic>` uninstalling the Helm release.
If the Operator and one or more ``RayClusters`` are created in multiple Helm releases,
the ``RayCluster`` releases must be uninstalled before the Operator release.

To remedy a situation where the Operator deployment was deleted first and ``RayCluster`` deletion is hanging, try one of the following:

- Manually delete the ``RayCluster``'s finalizers with ``kubectl edit`` or ``kubectl patch``.
- Restart the Operator so that it can remove ``RayCluster`` finalizers. Then remove the Operator.

Cluster-scoped vs. namespaced operators
---------------------------------------
By default, the Ray Helm chart installs a ``cluster-scoped`` operator.
Expand Down Expand Up @@ -171,11 +194,11 @@ Restart behavior
The Ray cluster will restart under the following circumstances:

- There is an error in the cluster's autoscaling process. This will happen if the Ray head node goes down.
- There has been a change to the Ray head pod configuration. In terms of the Ray Helm chart, this means either ``image`` or one of the following fields of the head's ``podType`` has been modified: ``numCPU``, ``numGPU``, ``memory``, ``nodeSelector``.
- There has been a change to the Ray head pod configuration. In terms of the Ray Helm chart, this means either ``image`` or one of the following fields of the head's ``podType`` has been modified: ``CPU``, ``GPU``, ``memory``, ``nodeSelector``.

Similarly, all workers of a given ``podType`` will be discarded if

- There has been a change to ``image`` or one of the following fields of the ``podType``: ``numCPU``, ``numGPU``, ``memory``, ``nodeSelector``.
- There has been a change to ``image`` or one of the following fields of the ``podType``: ``CPU``, ``GPU``, ``memory``, ``nodeSelector``.

Status information
~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -204,6 +227,7 @@ Questions or Issues?
.. include:: /_help.rst

.. _`RayCluster CRD`: https://github.com/ray-project/ray/tree/master/deploy/charts/ray/crds/cluster_crd.yaml
.. _`finalizer` : https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#finalizers
.. _`namespaced`: https://github.com/ray-project/ray/tree/master/deploy/components/operator_namespaced.yaml
.. _`cluster-scoped`: https://github.com/ray-project/ray/tree/master/deploy/components/operator_cluster_scoped.yaml
.. _`example`: https://github.com/ray-project/ray/tree/master/deploy/components/example_cluster.yaml
Expand Down
12 changes: 11 additions & 1 deletion doc/source/cluster/kubernetes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,20 @@ then fetch its logs:
$ kubectl -n ray delete job ray-test-job
job.batch "ray-test-job" deleted

.. _k8s-cleanup-basic:

Cleanup
-------

To remove a Ray Helm release and the associated API resources, use `helm uninstall`_.
To remove a Ray Helm release and the associated API resources, use `kubectl delete`_ and `helm uninstall`_.
Note the order of the commands below.

.. code-block:: shell

# First, delete the RayCluster custom resource.
$ kubectl -n ray delete raycluster example-cluster
raycluster.cluster.ray.io "example-cluster" deleted

# Delete the Ray release.
$ helm -n ray uninstall example-cluster
release "example-cluster" uninstalled
Expand All @@ -237,6 +244,8 @@ To remove a Ray Helm release and the associated API resources, use `helm uninsta
Note that ``helm uninstall`` `does not delete`_ the RayCluster CRD. If you wish to delete the CRD,
make sure all Ray Helm releases have been uninstalled, then run ``kubectl delete crd rayclusters.cluster.ray.io``.

- :ref:`More details on resource cleanup<k8s-cleanup>`

Next steps
----------
:ref:`Ray Operator Advanced Configuration<k8s-advanced>`
Expand All @@ -262,6 +271,7 @@ Questions or Issues?
.. _`kubectl`: https://kubernetes.io/docs/tasks/tools/
.. _`Helm 3`: https://helm.sh/
.. _`Helm`: https://helm.sh/
.. _`kubectl delete`: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#delete
.. _`helm uninstall`: https://helm.sh/docs/helm/helm_uninstall/
.. _`does not delete`: https://helm.sh/docs/chart_best_practices/custom_resource_definitions/
.. _`Pods`: https://kubernetes.io/docs/concepts/workloads/pods/
Expand Down
2 changes: 2 additions & 0 deletions docker/ray-deps/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ RUN $HOME/anaconda3/bin/pip --no-cache-dir install $(basename $WHEEL_PATH)[all]
"azure-mgmt-compute==14.0.0" \
"azure-mgmt-msi==1.0.0" \
"azure-mgmt-network==10.2.0"; fi) \
$(if [ $($HOME/anaconda3/bin/python -c "import sys; print(sys.version_info.minor)") != 6 ] \
&& [ "$AUTOSCALER" = "autoscaler" ]; then echo "kopf"; fi) \
&& $HOME/anaconda3/bin/pip uninstall ray -y && sudo rm $(basename $WHEEL_PATH)
Loading