K8s/Run: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 275: Line 275:


| valign="top" |
| valign="top" |
<syntaxhighlight lang="yaml" highlight="13-16,25,31">
<syntaxhighlight lang="yaml" highlight="8,13-16,25,31">
cat << YML | kubectl -n swiss-knife apply -f -
cat << YML | kubectl -n swiss-knife apply -f -
---
---
Line 310: Line 310:
</syntaxhighlight>
</syntaxhighlight>


| valign="top" rowspan="2" |
| valign="top" rowspan="2"|
<syntaxhighlight lang="yaml" line>
<syntaxhighlight lang="yaml" highlight="28-29" line>
cat << YML | kubectl -n swiss-knife apply -f -
cat << YML | kubectl -n swiss-knife apply -f -
---
---
Line 339: Line 339:
         app: swiss-knife-${USER}
         app: swiss-knife-${USER}
     spec:
     spec:
      nodeSelector:
        topology.kubernetes.io/zone: ap-southeast-1a
       containers:
       containers:
       - name: nginx
       - name: nginx
Line 352: Line 354:
             claimName: swiss-knife-${USER}-pvc
             claimName: swiss-knife-${USER}-pvc
       restartPolicy: Always
       restartPolicy: Always
---
YML
YML
</syntaxhighlight>
</syntaxhighlight>

Revision as of 22:56, 27 August 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

Run

Run » PSQL

kubectl -n swiss-knife run -i --tty --rm psql --image=alpine --restart=Never -- sh
echo -n password: ;read -s PGPASSWORD;export PGPASSWORD;echo
apk --update add postgresql-client inetutils-telnet

psql -d postgres -U postgres -h postgresql.postgresql

Run » Infinity

kubectl -n swiss-knife run infinity --image=alpine -- sleep infinity
kubectl -n swiss-knife exec -it infinity -- sh
apk --update add inetutils-telnet

Run » Redis CLI

kubectl -n swiss-knife run -i --tty --rm redis-cli --image=alpine --restart=Never -- sh
echo -n password: ;read -s REDISCLI_AUTH;export REDISCLI_AUTH;echo
apk --update add redis inetutils-telnet

echo 'ping'|redis-cli -h redis-headless.redis

Run » MinIO CLI

kubectl -n swiss-knife run -i --tty --rm minio-cli --image=alpine --restart=Never -- sh
apk --update add minio-client inetutils-telnet

echo -n password: ;\
read -s MINIO_SERVER_ROOT_PASSWORD;\
export  MINIO_SERVER_ROOT_PASSWORD;\
export  MINIO_SERVER_ROOT_USER=admin;\
export  MINIO_SERVER_HOST=minio.k8s.local;echo

mcli ping --error-count 5 minio.minio
mcli ping --count 5 minio.minio

telnet minio.minio 9000
telnet minio.minio 9001

Apply

Apply » Minikube » USER


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

Apply » AWS » USER


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

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

Playground

kubectl -n swiss-knife delete pods minio-cli
kubectl -n swiss-knife delete pods redis-cli
kubectl -n swiss-knife delete pods infinity
kubectl -n swiss-knife delete pods psql
kubectl -n swiss-knife run infinity --image=alpine -- sleep infinity
kubectl -n swiss-knife run ubuntu   --image=ubuntu -- sleep infinity

References