[TIL] 超好用的 Kubernetes Console tool - c9s/Vikube.vim

Kubernetes 相當好用,但是要維運的時候最痛苦的事情,就是要打 kubectl

雖然我超愛 Golang ,而且其實我基本的 IDE 都是使用 VSCode ,但是我一定要跟各位好好推薦這個好工具. Vikube.vim

不論你透過 alias 設定成 kc 甚至是 k 還是得要記憶一堆指令 ex: kubectl get pod

其實就算你不是 vim 的愛用者 (畢竟學習曲線太高了) 我還是很推薦你使用這個工具.

  • :VikubeContextList 可以開啟你所有連接過的 K8S 集群,透過 s 來切換你的集群.
  • :VikubeNodeList 可以開啟所有的節點清單,l 可以看到 結點上面的 logs, 可以幫助你除錯(如果有問題)
  • :VikubePodList 可以開啟 pod 清單,當然也可以透過 l 來看 POD 是否有出現問題.
  • :VikubeTop 可以開啟 top 來看各個 POD 的使用量

真的很好用… 再也不用擔心打 kubectl 或是忘記相關指令了 :p

[TIL] Change fish shell theme with nerd font

起源

Fish Shell 強大的親和力,並且讓我很簡單就上手.讓我使用上相當方便. 但是隨著許多軟體的安裝都還是需要 bash 並且在 vim 的更新上變得相當困難. 我其實有開始想要把 fish 換到大家比較常使用的 zsh .

原本看了這篇文章 超簡單!十分鐘打造漂亮又好用的 zsh command line 環境 要打算把自己的 fish shell 換到 zsh.

不過考量許多原因,做了一些小修改:

Fish shell failure update Vundle in Vim:

Refer: I don’t use a POSIX Shell (i.e. Bash/Sh)](https://github.com/VundleVim/Vundle.vim/wiki#i-dont-use-a-posix-shell-ie-bashsh)

解法: add ~/.config/fish/functions/updatevim.fish

function updatevim
    set -lx SHELL (which sh)
    vim +BundleInstall! +BundleClean +qall
end

Zsh PowerLevel9k 看起來實在很威

PowerLevel9K 實在很方便,所以找了一個 Fish Shell 可以使用的 bobthefish

不過 bobthefish 資訊太多了,需要一些調整.

#OMF setting for bobthefish
#
set -g theme_display_date no
set -g theme_nerd_fonts yes
set -g theme_display_k8s_context no

換漂亮的字型 nerd-font

後來有換這個字型,看起來還挺順眼的.

還有..

VSCode Integrated terminal 還是會有亂碼,目前還在解決中. 可以參考這個VSCode integrated terminal versus nerdfont-complete #672

Reference

[TIL][Google Kubernetes Engine] Tensorflow/k8s and GPU on GKE

Easy way for :Distributed Tensorflow

tensorflow/k8s : Tools for ML/Tensorflow on Kubernetes.

Architecture:

  • Run a job operator by Helm
  • Post your job just like a simple yaml file
  • Job Operator will help you to distributed to each node for computing result
    • PS
    • Worker

Sample Job Kubernetes YAML file

apiVersion: "tensorflow.org/v1alpha1"
kind: "TfJob"
metadata:
  name: "example-job"
spec:
  replicaSpecs:
    - replicas: 1
      tfReplicaType: MASTER
      template:
        spec:
          containers:
            - image: gcr.io/tf-on-k8s-dogfood/tf_sample:dc944ff
              name: tensorflow
          restartPolicy: OnFailure
    - replicas: 1
      tfReplicaType: WORKER
      template:
        spec:
          containers:
            - image: gcr.io/tf-on-k8s-dogfood/tf_sample:dc944ff
              name: tensorflow
          restartPolicy: OnFailure
    - replicas: 2
      tfReplicaType: PS

Sample Distributed Tensorflow Code

Refer here.

Troubleshooting: Could not enable default namespace on Helm.

kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'      
helm init --service-account tiller --upgrade

GKE: Enable GPU on GKE

gcloud alpha container clusters create gpu-test \
    --project $PROJECT_ID \
    --zone $ZONE \
    --enable-kubernetes-alpha \
    --enable-cloud-logging \
    --enable-cloud-monitoring \
    --accelerator type=nvidia-tesla-k80,count=1 \
    --machine-type n1-standard-1 \
    --cluster-version=1.8.4-gke.1 \
    --image-type $IMAGE_TYPE \
    --num-nodes 1 \
    --quiet

Trobleshooting:

  1. K8S alpha don’t support master version upgrade, so you need define k8s 1.8 when you create it. (default: 1.7.8)
  2. How to get GKE current support versions?
    1. ` gcloud container get-server-config`

[TIL] Netflix: Heterogeneous Cluster Allocation

原文: Netflix: Distributing Content to Open Connect

最近都在忙著趕公司專案,這篇文章讀了好久.

Netflix 最近寫了這篇技術文章來解釋他們如何面對大量影片下載需求的 Load Balance 的方式 : Heterogeneous Cluster Allocation (HCA)

一般來說我們都會使用 Consistent Hashing 的方式來處理伺服器分配流量的方式. 這也是最為人熟知的 Load Balancing 的方式. 但是 Netflix 提出了有趣的想法是,作為 Load Balancing 的時候,我們都是把伺服器當作是同質的 (註: 同樣速度,同樣硬碟大小)

但是如果其實伺服器可能是異質的(Heterogeneous) ,舉例來說: 有的一般硬碟 100TB 但是 throughput 比較低 ,另外則有速度快的 SSD 50 TB ,但是 throughput 相對來說就比較高.這樣的分配法則下,原來的 Consistent Hashing 就無法滿足這樣的方式.

於是 Netflix 他們使用 HCA 的方式來達到更適量的分配.針對這兩種異質的方式,Netflix: Heterogeneous Cluster Allocation (HCA) 作法如下:

  • 在兩種異質的機器上,各自給予 content 相對的權重 (weight).權重的評比來自於 content 受歡迎程度

  • 兩種機器各自有不同的 cutoff (也就是 threashold) ,來決定該 content 是否受歡迎. (ex: 40Gbps, cutoff: 0.3, 100Gbps 0.28..)

透過這樣的調整,可以確定接下來 content 會被放在哪裡.也能夠更有效率的分配流量.並且也驗證了在負載高的時候,更能讓不同的需求讓不同的伺服器來服務.

[TIL][markdown] 好用的編輯器 - typora

https://typora.io/img/new/inline.png

Typora 網站

今日內部教育訓練的時候,首席(筆頭?) 唯一問的問題,竟然是請問展示使用的 markdown 編輯器是什麼? 一問之下,才知道原來有另外一個好用的 markdown 編輯器 typora

  • 動態檢視 (類似 dropbox paper 體驗) (特愛
  • 支援 Latex preview, 支援 Latex preview, 支援 Latex preview (媽呀,真棒
  • 有 Tab 的支援
  • outline 顯示模式
  • 聽說支援 Windows, Ubuntu, OSX

於是我馬上把 MacDown 換掉了 XDDD ~~大推…

[TIL][Kubernetes] How to move your GKE into minikube

Preface

As a cloud company, we usually build our container cluster on cloud platforms such as GCP or AWS. but sometimes we need to think about how to take to go solution to our customer. So, here comes a challenge - Scale-in our cloud platform and put into pocket (mmm I mean “minikube”)

An example GKE service

Here is a simple example which you (might) build in GKE.

  • DB: MongoDB with stateful set binding with google persistent disk. Use stateful set from (“Running MongoDB on Kubernetes with StatefulSets”) as an example
  • Web service(fooBar): a golang application which accesses mongo DB with the load balancer. Because fooBar is proprietary application which fooBar image store in GCR (Google Cloud Registry) not in docker hub.

How to migrate your service from GKE to minikube:

We will just list some note to let you know any tip or note you migrate your service to minikube.

1. Migrate Persistent Volume to minikube.

Here” is a good article to understand StatefulSet. But however it use some resource you could not use in minikube, such as:

Let me put all yaml setting and give more detail.

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: "mongo"
  replicas: 3
  template:
    metadata:
      labels:
        role: mongo
        environment: test
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo
          command:
            - mongod
            - "--replSet"
            - rs0
            - "--smallfiles"
            - "--noprealloc"
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
        - name: mongo-sidecar
          image: cvallance/mongo-k8s-sidecar
          env:
            - name: MONGO_SIDECAR_POD_LABELS
              value: "role=mongo,environment=test"
  volumeClaimTemplates:
  - metadata:
      name: mongo-persistent-storage
      annotations:
        volume.beta.kubernetes.io/storage-class: "fast"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 100Gi

The volumeClaimTemplates provide stable storage using PersistentVolumes provisioned by a PersistentVolume Provisioner. (Use “Google Persistent Disk” on GCP)

The “fast” will specific to use google persistent disk with “SSD” performance. So, in this case our Kubernetes volume claim will specific to google cloud persistent disk.

How we migrate to Google Persistent Disk volume into minikube?

There are two solutions for this:

  • Use hostPath directly.
  • Still using volume claim but remove google persistent disk annotation.

In the second case, we could still use volumeClaimTemplates and just remove volume provider persistent disk annotation. It will select the best for our system. (for now, it is hostPath.

  volumeClaimTemplates:
    - metadata:
        name: minikube-clain
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 10Gi

2. Using GCR in minikube

When you deploy GKE, you usually use GCR (Google Cloud Registry) to storage your docker container. If you want to move your project from GKE to minikube, you have solutions as follows:

  1. Build your own docker registry. But you will need handle follow things:
    • Use --insecure-registry if you use the self-signed key.
    • Use public DNS to request CA key (It will against we want to move into minikube)
  2. Use minikube to connect to GCR directly.
    • Minikube has add-on minikube addons configure registry-creds
    • Use temproary token solution (around ~30 mins), here is the reference.
kubectl delete secret gcr

kubectl create secret docker-registry gcr \
    --docker-server=https://asia.gcr.io \
    --docker-username=oauth2accesstoken \
    --docker-password="$(gcloud auth print-access-token)" \
    [email protected]

Here is the document from minikube.

Launch your service

Ok, that’s done. If you are wondering how to connect to your web service fooBar. You can just call minikube service fooBar.

Troubleshooting

using busybox to debug

It is very useful when you don’t know what’s happening on your pod, especially for networks storage or network.

cat <<EOF | kubectl create -f -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: busybox
spec:
  selector:
    matchLabels:
      app: busybox
  replicas: 1
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox
        image: busybox
        command:
          - sleep
          - "86400"
EOF

kubectl exec -it <BUSYBOX_POD_NAME> -- nslookup redis.default

example:

kubectl exec -it busybox-7fc4f6df6-fhxnp -- nslookup redis.default

Destroy & Cleanup the minikube

Remember to remove the setting file when you uninstall minikube.

minikube delete
rm -rf ~/.minikube

Cannot start minikube

It might happen as follow reasons:

  1. minikube version change.
  2. docker version change.
  3. VM has already been destroyed by VirtualBox.
Starting local Kubernetes v1.8.0 cluster...
Starting VM...
E1124 22:16:44.080926   87887 start.go:150] Error starting host: Temporary Error: Error configuring auth on host: Temporary Error: ssh command error:

Solution: rm -rf ~/.minikube