centos 7 筆記

安裝chrome, refer

sudo bash -c 'cat <<EOF > /etc/yum.repos.d/google-chrome.repo
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
EOF

'
sudo yum -y install google-chrome-stable

安裝qt5, refer

sudo yum -y install gcc gcc-c++ mesa-libGL-devel mesa-libGLU-devel freeglut-devel
wget http://download.qt.io/archive/qt/5.4/5.4.2/qt-opensource-linux-x64-5.4.2.run
chmod +x qt-opensource-linux-x64-5.4.2.run
sudo ./qt-opensource-linux-x64-5.4.2.run

安裝proj4, refer

sudo yum install epel-release -y
sudo yum install -y proj proj-devel proj-epsg

install & test cuda 9.0 on ubuntu 16.04

安裝

wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
sudo service lightdm stop
sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
sudo apt-get update && sudo apt-get install cuda -y
sudo reboot
sudo ln -s /usr/local/cuda/bin/nvcc /usr/bin/nvcc

設定環境變數

vim ~/.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/usr/lib/nvidia-367
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:/usr/local/cuda/bin

測試編譯
方法1:

cd /usr/local/cuda/samples/0_Simple/vectorAdd
make
./vectorAdd

方法2:
建立測試檔案vectorAdd.cu

#include <stdio.h>
#include <cuda_runtime.h>

__global__ void vectorAdd(const float *A, const float *B, float *C, int numElements){
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < numElements){
        C[i] = A[i] + B[i];
    }
}

int main(void){
  int numElements = 50000;

  //初始化測試資料
  float *h_A=new float[numElements];
  float *h_B=new float[numElements];
  float *h_C=new float[numElements];
  for (int i = 0; i < numElements; ++i) {
    h_A[i] = rand()/(float)RAND_MAX;
    h_B[i] = rand()/(float)RAND_MAX;
  }

  //配置GPU記憶體空間,並從記憶體中複製資料至GPU中
  size_t size = numElements * sizeof(float);
  float *d_A = NULL;  cudaMalloc((void **)&d_A, size);
  float *d_B = NULL;  cudaMalloc((void **)&d_B, size);
  float *d_C = NULL;  cudaMalloc((void **)&d_C, size);
  cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);
  cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice);

  //運算
  int threadsPerBlock = 256;
  int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock; 
  vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, numElements);

  //取回運算結果
  cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost);
  
  //清除GPU記憶體空間
  cudaFree(d_A);
  cudaFree(d_B);
  cudaFree(d_C);

  //驗證資料
  for (int i = 0; i < numElements; ++i) {
    if (fabs(h_A[i] + h_B[i] - h_C[i]) > 1e-5) {
      fprintf(stderr, "Result verification failed at element %d!\n", i);
      exit(EXIT_FAILURE);
    }
  }

  //清除記憶體
  delete d_A;
  delete d_B;
  delete d_C;

  printf("Test PASSED\n");
  return 0;
}

接著手動編譯~

/usr/local/cuda-9.0/bin/nvcc \
  -ccbin g++  \
  -m64 \
  -gencode arch=compute_30,code=sm_30 \
  -gencode arch=compute_35,code=sm_35 \
  -gencode arch=compute_37,code=sm_37 \
  -gencode arch=compute_50,code=sm_50 \
  -gencode arch=compute_52,code=sm_52 \
  -gencode arch=compute_60,code=sm_60 \
  -gencode arch=compute_70,code=sm_70 \
  -gencode arch=compute_70,code=compute_70 \
  -c vectorAdd.cu -o vectorAdd.o

/usr/local/cuda-9.0/bin/nvcc \
  -ccbin g++ \
  -m64 \
  -gencode arch=compute_30,code=sm_30 \
  -gencode arch=compute_35,code=sm_35 \
  -gencode arch=compute_37,code=sm_37 \
  -gencode arch=compute_50,code=sm_50 \
  -gencode arch=compute_52,code=sm_52 \
  -gencode arch=compute_60,code=sm_60 \
  -gencode arch=compute_70,code=sm_70 \
  -gencode arch=compute_70,code=compute_70 \
  vectorAdd.o -o vectorAdd

ubuntu 16.04 安裝kubernetes 1.8.1

前置工作
安裝kubectl

sudo rm ./kubectl /usr/local/bin/kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

設定kubeadm來源

sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo bash -c 'cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF'

安裝kubeadm

sudo apt-get update && sudo apt-get install -y kubelet kubeadm

關閉swap

sudo swapoff -a 
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo mount -a

安裝步驟:

##############################################################################################
##
## init
##
sudo kubeadm init \
  --kubernetes-version=v1.8.1 \
  --pod-network-cidr=10.244.0.0/16 \
  --skip-preflight-checks

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl get pods --all-namespaces -o wide

############################################################################################
##
## 安裝網路
##
kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')

############################################################################################
##
## 加入節點
##
sudo kubeadm join --token xxxxxxxxxxxxxxxxxxxx \
  10.1.1.1:6443 \
  --discovery-token-ca-cert-hash sha256:yyyyyyyyyyyyyyyyyyyy
mkdir -p $HOME/.kube
scp 10.1.1.1:~/.kube/config $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes


############################################################################################
##
## 加入dashboard, 參考來源: https://github.com/kubernetes/dashboard/wiki/Access-control 
##
cat <<EOF > dashboard-admin.yaml
{
  "apiVersion": "rbac.authorization.k8s.io/v1beta1", 
  "kind": "ClusterRoleBinding", 
  "metadata": {
    "name": "kubernetes-dashboard",
    "labels": {
      "k8s-app": "kubernetes-dashboard"
    }
  },
  "roleRef": {
    "apiGroup": "rbac.authorization.k8s.io", 
    "kind": "ClusterRole", 
    "name": "cluster-admin"
  }, 
  "subjects": [
    {
      "kind": "ServiceAccount", 
      "name": "kubernetes-dashboard",
      "namespace": "kube-system"
    }
  ]
}
EOF
kubectl apply -f dashboard-admin.yaml 
rm dashboard-admin.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

############################################################################################
##
## 加入proxy
##
nohup kubectl proxy --address 0.0.0.0 --accept-hosts '.*' >/dev/null 2>&1 &

############################################################################################
##
## 取得kubernetes-dashboard-admin登入dashboard的token (暫時用不到)
##
## kubectl describe -n kube-system secret/$(kubectl -n kube-system get secret | grep kubernetes-dashboard-admin | awk {'print $1'}) | grep token: | awk {'print $2'}

解決Ubuntu出現[W:mdadm: /etc/mdadm/mdadm.conf defines no arrays]的問題

Ubuntu 16.04 LTS每當出現Kernel update時,
就會出現W:mdadm: /etc/mdadm/mdadm.conf defines no arrays

解決方法:
刪除mdadm.conf文件

sudo rm /etc/mdadm/mdadm.conf

接著用update-initramfs命令,重新產生mdadm.conf

sudo update-initramfs -u

總結:用下列指令解決~

sudo rm /etc/mdadm/mdadm.conf && sudo update-initramfs -u

安裝kubernetes 1.7.5於ubuntu16.04

環境介紹:

hostname os ip note
node-k00 ubuntu16.04 10.1.200.100 kubernetes master
node-k01 ubuntu16.04 10.1.200.101 kubernetes node
node-k02 ubuntu16.04 10.1.200.102 kubernetes node
node-k03 ubuntu16.04 10.1.200.103 kubernetes node

1.首先在master以及node上安裝kubectl

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.7.5/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

2.安裝kubeadm (在master)

sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo bash -c 'cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF'
sudo apt-get update && sudo apt-get install -y kubelet kubeadm

3.使用kubeadm初始化kubernetes (在master)

sudo kubeadm init --kubernetes-version=v1.8.0 --pod-network-cidr 10.244.0.0/16

4.複製設定檔 (在master)

mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

5.等待一段時間後,使用下列指令確認一下pod部署狀態 (在master).

kubectl get pods --all-namespaces -o wide

6.建立網路 (在master)

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

並等待一段時間後,使用下列指令確認一下pod部署狀態 (在master).

kubectl get pods --all-namespaces -o wide

7.加入 dashboard-admin, 參考來源 (在master)

cat <<EOF > dashboard-admin.yaml
{
  "apiVersion": "rbac.authorization.k8s.io/v1beta1", 
  "kind": "ClusterRoleBinding", 
  "metadata": {
    "name": "kubernetes-dashboard",
    "labels": {
      "k8s-app": "kubernetes-dashboard"
    }
  },
  "roleRef": {
    "apiGroup": "rbac.authorization.k8s.io", 
    "kind": "ClusterRole", 
    "name": "cluster-admin"
  }, 
  "subjects": [
    {
      "kind": "ServiceAccount", 
      "name": "kubernetes-dashboard",
      "namespace": "kube-system"
    }
  ]
}
EOF
kubectl create -f dashboard-admin.yaml
rm dashboard-admin.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

並等待一段時間後,使用下列指令確認一下pod部署狀態 (在master).

kubectl get pods --all-namespaces -o wide

8.建立proxy,讓瀏覽器可以開始dashboard (在master)

nohup kubectl proxy --address 0.0.0.0 --accept-hosts '.*' >/dev/null 2>&1 &

9.開啟kubenetes dashboard (在10.1.0.0/16的網域內)

http://10.1.200.100:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

10.加入Nginx Backend (在master)

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress/master/examples/deployment/nginx/default-backend.yaml

11.加入 Ingress RBAC 認證 (在master)

curl -O https://raw.githubusercontent.com/kubernetes/ingress/master/examples/rbac/nginx/nginx-ingress-controller-rbac.yml
## 修改 namespace
sed -i 's/namespace: nginx-ingress/namespace: kube-system/g' nginx-ingress-controller-rbac.yml
## 匯入yaml檔案
kubectl apply -f nginx-ingress-controller-rbac.yml
rm -r nginx-ingress-controller-rbac.yml

12.加入 Ingress Controller (在master)

curl -O https://raw.githubusercontent.com/kubernetes/ingress/master/examples/daemonset/nginx/nginx-ingress-daemonset.yaml
## 修改 namespace
sed -i 's/terminationGracePeriodSeconds/hostNetwork: true\n      serviceAccountName: nginx-ingress-serviceaccount\n      terminationGracePeriodSeconds/g' nginx-ingress-daemonset.yaml
## 匯入yaml檔案
kubectl apply -f nginx-ingress-daemonset.yaml
rm nginx-ingress-daemonset.yaml
kubectl get daemonset -n kube-system nginx-ingress-lb

13.加入其他的node (在master)

token=$(sudo kubeadm token list | grep authentication,signing | awk '{print $1}')
ssh 10.1.200.101 "sudo kubeadm join --token $token 10.1.200.100:6443"
ssh 10.1.200.102 "sudo kubeadm join --token $token 10.1.200.100:6443"
ssh 10.1.200.103 "sudo kubeadm join --token $token 10.1.200.100:6443"

ubuntu安裝docker ce

首先先安裝一些必要元件

sudo apt-get install \
  apt-transport-https \
  ca-certificates \
  curl \
  software-properties-common

接著把docker官方的GPG key加入

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

並加入安裝來源(適用amd64機器)

sudo add-apt-repository \
  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"

然後用下列指令進行安裝Docker ce

sudo apt-get update && sudo apt-get install docker-ce -y

最後把目前使用者加入docker群組

sudo gpasswd -a $USER docker

以cat指令建立新檔案

過去很習慣使用vim建立新檔案
但是最近大量使用docker container的時候,面臨到沒有安裝vim的窘境.
因此找出了使用cat建立新檔案的方法,也順便建立使用script來產生檔案的方法.

使用方式如下:

cat <<EOF > example.txt
Hello World.
This is a test.
EOF

cat <<EOF為開頭,緊接著檔名,換行後就是要建立的內容,可允許多行輸入.
最後以EOF\n作為結束.

查看log

查看log最後30行的方法

sudo cat /var/log/syslog | tail -n 30

每5秒,查看現在正在執行php的程序

watch -n 5 'sudo ps aux | grep php | tail -n 5'

顯示log中,最新有關於named的資訊

tail -f /var/log/syslog | grep named

使用nohup在背景中執行程式

通常使用putty進行ssh連線時,常常遇到要長時間的執行一個指令
而半徒若將putty關閉的話,這個程式也將會被終止.
因此必須將程式放置到背景中執行

方法1: 執行結果輸出至nohup.out

nohup command &

方法2: 指定輸出logfile

nohup command > logfile &

方法3: 不要輸出任何資訊

nohup command >/dev/null 2>&1 &

若要搭配sudo的話,其用法如下

sudo sh -c 'nohup command &'