Getting started with Yandex Cloud — Kubernetes cluster
Before we start, you should create own Yandex Cloud account and setup Managed Kubernetes cluster. How to do it you can read in the documentation.
Install Yandex cloud command line interface:
curl -sSL https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash
add credentials to kubectl
configuration file:
yc managed-kubernetes cluster get-credentials test-k8s-cluster --external
This command will add cluster information to .kube/config
and set it as a current context. Now we can use kubectl
to interact with our test-k8s-cluster
. For example, we can list current namespaces
:
kubectl get namespaces
Now let’s create a simple node echo server and deploy it to our cluster.
mkdir /tmp/echo-app && \
pushd /tmp/echo-app && \
mkdir src && \
npm init -y && \
npm install express
cat > src/index.js <<EOF
function main() {
const express = require('express');
const app = express();
app.use((req, res) => {
res.json({
method: req.method,
url: req.url,
headers: req.headers
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
}
main();
EOF
cat > Dockerfile <<EOF
FROM node:18-slim
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
COPY src ./src
RUN npm ci --only=production
EXPOSE 3000
CMD ["node", "src/index.js"]
EOF
Create docker registry test-registry
in default
folder on Yandex Cloud:
yc container registry create --name test-registry --folder-name=default
yc container registry configure-docker
Build docker image with our application and push to test-registry
. The image name should be cr.yandex/<registry ID>/<Docker image name>
. To get <registry ID>
you can use command yc container registry list
REGISTRY_ID="$(yc container registry list --format=yaml | awk '{gsub(/-/,""); print}' | awk '{print $2}' | head -n 1)"
docker build --platform=linux/amd64 -t cr.yandex/$REGISTRY_ID/echo-app .
Push to the registry
docker push cr.yandex/$REGISTRY_ID/echo-app
Create namespace and deploy our application to the cloud:
kubectl create namespace test
kubectl config set-context --current --namespace=test
Now add deployment configuration:
cat > deployment.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: echo-app
name: echo-app
spec:
replicas: 1
selector:
matchLabels:
app: echo-app
template:
metadata:
labels:
app: echo-app
spec:
containers:
- image: <registry ID>/echo-app
name: echo-app
resources: {
limits: {
cpu: 500m,
memory: 512Mi
}
}
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: echo-service
spec:
type: NodePort
selector:
app: echo-app
ports:
- port: 3000
targetPort: 3000
nodePort: 30000
EOF
Create node group in control panel UI.
Now deploy app to the cloud:
kubectl apply -f deployment.yaml
Now we can test that all works fine. Forward service’s port to local machine:
kubectl port-forward services/echo-service 3000:3000
and send request by curl
curl 127.0.0.1:3000/
Done!