K8s/Swiss Knife: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
(Created page with "{| |valign="top"| <syntaxhighlight lang="bash"> export KUBECONFIG="${HOME}/.kube/lke-dev-kubeconfig.yaml" export KUBECONFIG="${HOME}/.kube/gke-uat-kubeconfig.yaml" export KUBECONFIG="${HOME}/.kube/eks-pro-kubeconfig.yaml" </syntaxhighlight> |valign="top"| <syntaxhighlight lang="bash"> export KUBECONFIG="${HOME}/.kube/config" kubectl config get-contexts kubectl cluster-info </syntaxhighlight> |valign="top"| <syntaxhighlight lang="bash"> kubectl get ns|grep swiss-knife k...")
 
 
(15 intermediate revisions by the same user not shown)
Line 159: Line 159:
kubectl -n swiss-knife port-forward svc/swiss-knife-${USER} 1980:80
kubectl -n swiss-knife port-forward svc/swiss-knife-${USER} 1980:80
kubectl -n swiss-knife port-forward svc/swiss-knife-${USER}    :80
kubectl -n swiss-knife port-forward svc/swiss-knife-${USER}    :80
</syntaxhighlight>
|-
| colspan="3" |
----
<syntaxhighlight lang="bash">
# copy dump into pod
SWISS_KNIFE_POD=$(kubectl -n swiss-knife get pods -l app=swiss-knife-${USER} -ojson|jq -r '.items[0].metadata.name')
kubectl -n swiss-knife cp ./gitlab_sdlc-20240820-T1622-ZP0800.sql.gz ${SWISS_KNIFE_POD}:/opt/swiss-knife/${USER}
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- ls -lah /opt/swiss-knife/${USER}
# install apk packages
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- sh
apk --update add postgresql-client inetutils-telnet
# import psql dump
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- sh
echo -n password: ;read -s PGPASSWORD;export PGPASSWORD;echo
gunzip -c /opt/swiss-knife/${USER}/./gitlab_sdlc-20240820-T1622-ZP0800.sql.gz|psql -Ugitlab gitlab
psql -d gitlab -U gitlab
</syntaxhighlight>
</syntaxhighlight>


Line 349: Line 329:


|}
|}
==Workaround==
<syntaxhighlight lang="bash">
# copy dump into pod
SWISS_KNIFE_POD=$(kubectl -n swiss-knife get pods -l app=swiss-knife-${USER} -ojson|jq -r '.items[0].metadata.name')
kubectl -n swiss-knife cp ./gitlab_sdlc-20240820-T1622-ZP0800.sql.gz ${SWISS_KNIFE_POD}:/opt/swiss-knife/${USER}
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- ls -lah /opt/swiss-knife/${USER}
# install apk packages
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- sh
apk --update add postgresql-client inetutils-telnet
# import psql dump
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- sh
echo -n password: ;read -s PGPASSWORD;export PGPASSWORD;echo
gunzip -c /opt/swiss-knife/${USER}/./gitlab_sdlc-20240820-T1622-ZP0800.sql.gz|psql -Ugitlab gitlab
psql -d gitlab -U gitlab
</syntaxhighlight>


==Playground==
==Playground==
Line 354: Line 352:
| valign="top" |
| valign="top" |
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
kubectl -n swiss-knife delete pods minio-cli
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- sh
kubectl -n swiss-knife delete pods redis-cli
apk --update add --no-cache aws-cli inetutils-telnet postgresql-client rsync
 
aws configure --profile academia
export AWS_PROFILE=academia
aws sts get-caller-identity
aws configure list
</syntaxhighlight>
</syntaxhighlight>


| valign="top" |
| valign="top" colspan="2" |
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
kubectl -n swiss-knife delete pods infinity
cd /opt/swiss-knife/${USER}/
kubectl -n swiss-knife delete pods psql
 
</syntaxhighlight>
aws s3 cp s3://academia-backups/gitlab/gitlab-sdlc-20240820-T1622-ZP0800.sql.gz \
gitlab-sdlc-20240820-T1622-ZP0800.sql.gz


| valign="top" |
aws s3 cp s3://academia-backups/gitlab/gitlab-sdlc-20240822-T1910-ZP0800.tar.gz \
<syntaxhighlight lang="bash">
gitlab-sdlc-20240822-T1910-ZP0800.tar.gz
kubectl -n swiss-knife run infinity --image=alpine -- sleep infinity
kubectl -n swiss-knife run ubuntu  --image=ubuntu -- sleep infinity
</syntaxhighlight>
</syntaxhighlight>
| valign="top" |


|-
|-
Line 377: Line 377:
|-
|-
| valign="top" |
| valign="top" |
<syntaxhighlight lang="bash">
cd /opt/swiss-knife/${USER}/
tar -xzf gitlab-sdlc-20240822-T1910-ZP0800.tar.gz
gunzip -c gitlab-sdlc-20240820-T1622-ZP0800.sql.gz|\
psql -hpostgresql.postgresql -p5432 -Ugitlab gitlab
</syntaxhighlight>


| valign="top" |
| valign="top" |
Line 431: Line 437:
* [[K8s/CSI Hostpath Driver|K8s » CSI Hostpath Driver]]
* [[K8s/CSI Hostpath Driver|K8s » CSI Hostpath Driver]]
* [[K8s/Ingress|K8s » Ingress]]
* [[K8s/Ingress|K8s » Ingress]]
* [[K8s/Service|K8s » Service]]
* [[K8s/Run|K8s » Run]]
* [[K8s/Run|K8s » Run]]
* [[MinIO]]
* [[MinIO]]
Line 437: Line 444:


| valign="top" |
| valign="top" |
* [https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/ K8s » Stateful » Single Instance]
* [https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/ K8s » StatefulSet » Basics]
* [https://stackoverflow.com/questions/61918972/ Alpine » AWS CLI » Install]
* [https://kubernetes.io/docs/concepts/services-networking/service/ K8s » Network » Service]
* [[EKSctl|AWS » EKS » CLI]]
* [[AWS CLI|AWS » CLI]]
* [[AWS]]


|}
|}

Latest revision as of 09:21, 7 September 2024

export KUBECONFIG="${HOME}/.kube/lke-dev-kubeconfig.yaml"
export KUBECONFIG="${HOME}/.kube/gke-uat-kubeconfig.yaml"
export KUBECONFIG="${HOME}/.kube/eks-pro-kubeconfig.yaml"
export KUBECONFIG="${HOME}/.kube/config"
kubectl config get-contexts
kubectl cluster-info
kubectl get ns|grep swiss-knife
kubectl delete ns   swiss-knife
kubectl create ns   swiss-knife

Deploy » Minikube

PV » Manual


PV » Dynamic


Service & Deployment


cat << YML | kubectl -n swiss-knife apply -f -
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: swiss-knife-${USER}-pv
spec:
  storageClassName: standard
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteOnce
  hostPath:
    path: /tmp/hostpath-provisioner/swiss-knife/swiss-knife-${USER}-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app.kubernetes.io/name: swiss-knife-${USER}
  name: swiss-knife-${USER}-pvc
spec:
  storageClassName: standard
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  volumeName: swiss-knife-${USER}-pv
YML
cat << YML | kubectl -n swiss-knife apply -f -
---












---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app.kubernetes.io/name: swiss-knife-${USER}
  name: swiss-knife-${USER}-pvc
spec:
  storageClassName: standard
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

YML
cat << YML | kubectl -n swiss-knife apply -f -
---
apiVersion: v1
kind: Service
metadata:
  name: swiss-knife-${USER}
spec:
  ports:
  - port: 80
  selector:
    app: swiss-knife-${USER}
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: swiss-knife-${USER}
spec:
  selector: 
    matchLabels:
      app: swiss-knife-${USER}
  replicas: 1
  template:
    metadata:
      labels:
        app: swiss-knife-${USER}
    spec:
      containers:
      - name: nginx
        image: nginx:1.27-alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /opt/swiss-knife/${USER}
          name: ${USER}-data
      volumes:
      - name: ${USER}-data
        persistentVolumeClaim:
          claimName: swiss-knife-${USER}-pvc
      restartPolicy: Always
YML

kubectl -n swiss-knife exec -it     svc/swiss-knife-${USER} -c nginx -- sh
kubectl -n swiss-knife exec -it     svc/swiss-knife-${USER} -- sh

kubectl -n swiss-knife port-forward svc/swiss-knife-${USER} 1980:80
kubectl -n swiss-knife port-forward svc/swiss-knife-${USER}     :80

kubectl -n swiss-knife delete deploy    swiss-knife-${USER}
kubectl -n swiss-knife delete svc       swiss-knife-${USER}
kubectl -n swiss-knife delete pvc       swiss-knife-${USER}-pvc
kubectl                delete pv        swiss-knife-${USER}-pv

Deploy » EKS

PV » Manual


PV » Dynamic


Service & Deployment


cat << YML | kubectl -n swiss-knife apply -f -
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: swiss-knife-${USER}-pv
spec:
  storageClassName: "gp2"
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteOnce
  awsElasticBlockStore:
    fsType: ext4
    volumeID: aws://ap-southeast-1/${EBS_VOLUME_ID}
  persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app.kubernetes.io/name: swiss-knife-${USER}
  name: swiss-knife-${USER}-pvc
spec:
  storageClassName: "gp2"
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  volumeName: swiss-knife-${USER}-pv
YML
cat << YML | kubectl -n swiss-knife apply -f -
---














---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app.kubernetes.io/name: swiss-knife-${USER}
  name: swiss-knife-${USER}-pvc
spec:
  storageClassName: "gp2"
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  volumeName: swiss-knife-${USER}-pv
YML
cat << YML | kubectl -n swiss-knife apply -f -
---
apiVersion: v1
kind: Service
metadata:
  name: swiss-knife-${USER}
spec:
  ports:
  - port: 80
  selector:
    app: swiss-knife-${USER}
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: swiss-knife-${USER}
spec:
  selector: 
    matchLabels:
      app: swiss-knife-${USER}
  replicas: 1
  template:
    metadata:
      labels:
        app: swiss-knife-${USER}
    spec:
      nodeSelector:
        topology.kubernetes.io/zone: ap-southeast-1a
      containers:
      - name: nginx
        image: nginx:1.27-alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /opt/swiss-knife/${USER}
          name: ${USER}-data
      volumes:
      - name: ${USER}-data
        persistentVolumeClaim:
          claimName: swiss-knife-${USER}-pvc
      restartPolicy: Always
YML

kubectl -n swiss-knife exec -it     svc/swiss-knife-${USER} -c nginx -- sh
kubectl -n swiss-knife exec -it     svc/swiss-knife-${USER} -- sh

kubectl -n swiss-knife port-forward svc/swiss-knife-${USER} 1980:80
kubectl -n swiss-knife port-forward svc/swiss-knife-${USER}     :80

# aws » ec2 » ebs » volume » name: sdlc-eks-swiss-knife-${USER}-data
kubectl -n swiss-knife delete deploy    swiss-knife-${USER}
kubectl -n swiss-knife delete svc       swiss-knife-${USER}
kubectl -n swiss-knife delete pvc       swiss-knife-${USER}-pvc
kubectl                delete pv        swiss-knife-${USER}-pv

Workaround

# copy dump into pod
SWISS_KNIFE_POD=$(kubectl -n swiss-knife get pods -l app=swiss-knife-${USER} -ojson|jq -r '.items[0].metadata.name')
kubectl -n swiss-knife cp ./gitlab_sdlc-20240820-T1622-ZP0800.sql.gz ${SWISS_KNIFE_POD}:/opt/swiss-knife/${USER}
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- ls -lah /opt/swiss-knife/${USER}

# install apk packages
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- sh
apk --update add postgresql-client inetutils-telnet

# import psql dump
kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- sh
echo -n password: ;read -s PGPASSWORD;export PGPASSWORD;echo
gunzip -c /opt/swiss-knife/${USER}/./gitlab_sdlc-20240820-T1622-ZP0800.sql.gz|psql -Ugitlab gitlab
psql -d gitlab -U gitlab

Playground

kubectl -n swiss-knife exec -it svc/swiss-knife-${USER} -- sh
apk --update add --no-cache aws-cli inetutils-telnet postgresql-client rsync

aws configure --profile academia
export AWS_PROFILE=academia
aws sts get-caller-identity
aws configure list
cd /opt/swiss-knife/${USER}/

aws s3 cp s3://academia-backups/gitlab/gitlab-sdlc-20240820-T1622-ZP0800.sql.gz \
 gitlab-sdlc-20240820-T1622-ZP0800.sql.gz

aws s3 cp s3://academia-backups/gitlab/gitlab-sdlc-20240822-T1910-ZP0800.tar.gz \
 gitlab-sdlc-20240822-T1910-ZP0800.tar.gz

cd /opt/swiss-knife/${USER}/
 tar -xzf gitlab-sdlc-20240822-T1910-ZP0800.tar.gz
gunzip -c gitlab-sdlc-20240820-T1622-ZP0800.sql.gz|\
 psql -hpostgresql.postgresql -p5432 -Ugitlab gitlab

References