容器開發(fā)運維人員的 Linux 操作機配置優(yōu)化建議

"工欲善其事必先利其器", 作為一個PAAS平臺架構(gòu)師, 容器相關(guān)技術(shù)(docker, k8s等)是必不可少的. 本文簡單介紹下我自己的Linux操作機配置. 提升工作效率, 提高使用體驗. :heart::heart::heart:

:exclamation: 注意:

本文以CentOS 7.6 為例, RHEL7.6 操作類似.

Ubuntu系統(tǒng)操作可以觸類旁通. 沒啥難度.

另外下文中會有一些"可選"項, 主要是針對一些特殊情況, 如: 需要通過代理連接互聯(lián)網(wǎng)...

更換OS 軟件安裝源

目的: 加快軟件下載速度.

可以換成: 阿里, 騰訊, 清華, 中科大...的源.

以清華Mirror為例, 操作步驟如下:

:bookmark: 參考文章:

清華大學(xué)開源軟件鏡像站 - CentOS 鏡像使用幫助https://mirrors.tuna.tsinghua.edu.cn/help/centos/

操作步驟

  1. 先備份 CentOS-Base.repo

    sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
    
  2. 用下面內(nèi)容覆蓋CentOS-Base.repo

    # CentOS-Base.repo
    #
    # The mirror system uses the connecting IP address of the client and the
    # update status of each mirror to pick mirrors that are updated to and
    # geographically close to the client.  You should use this for CentOS updates
    # unless you are manually picking other mirrors.
    #
    # If the mirrorlist= does not work for you, as a fall back you can try the
    # remarked out baseurl= line instead.
    #
    #
    
    
    [base]
    name=CentOS-$releasever - Base
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
    
    #released updates
    [updates]
    name=CentOS-$releasever - Updates
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
    
    
    
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
    
    
    
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7
    
  3. 更新軟件包緩存

    sudo yum makecache
    

配置代理(可選)

sudo vi /etc/profile.d/setproxy.sh

#!/bin/sh

# for terminal
export proxyserveraddr=127.0.0.1
export proxyserverport=8080
export HTTP_PROXY="http://$proxyserveraddr:$proxyserverport/"
export HTTPS_PROXY="http://$proxyserveraddr:$proxyserverport/"
# export FTP_PROXY="ftp://$proxyserveraddr:$proxyserverport/"
# export SOCKS_PROXY="socks://$proxyserveraddr:$proxyserverport/"
export NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy="http://$proxyserveraddr:$proxyserverport/"
export https_proxy="http://$proxyserveraddr:$proxyserverport/"
# export ftp_proxy="ftp://$proxyserveraddr:$proxyserverport/"
# export socks_proxy="socks://$proxyserveraddr:$proxyserverport/"
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

sudo source /etc/profile.d/setproxy.sh

YUM配置代理

echo "proxy=http://127.0.0.1:8080" >> /etc/yum.conf

安裝及配置Git

目的: 使用Git, 畢竟很多資料、代碼庫和軟件都需要通過git clone

步驟

  1. sudo yum install -y git

  2. 配置全局用戶: git config --global user.name "<username>"

  3. 配置全局email: git config --global user.email "<username@example.com>"

  4. (可選): 配置ssh認證

    1. :bookmark: 參考文檔: GitHub - 使用 SSH 連接到 GitHub https://docs.github.com/cn/github/authenticating-to-github/connecting-to-github-with-ssh
  5. (可選): 配置代理Proxy

    # 查看當前代理設(shè)置
    git config --global http.proxy
    
    # 設(shè)置當前代理為 http://127.0.0.1::8080 或 socket5://127.0.0.1::8080
    git config --global http.proxy 'http://127.0.0.1::8080'
    git config --global https.proxy 'http://127.0.0.1::8080'
    git config --global http.proxy 'socks5://127.0.0.1::8080'
    git config --global https.proxy 'socks5://127.0.0.1::8080' 
    
    # 刪除 proxy 
    git config --global --unset http.proxy
    git config --global --unset https.proxy
    
  6. (可選): 配置Proxy Bypass, 如對應(yīng)倉庫的origin需要Bypass: git config --add remote.origin.proxy ""

優(yōu)化配置Shell

目的: zsh + plugins, 提供豐富而友好的shell體驗. 如: 語法高亮, 自動補全, 自動建議, 容器相關(guān)插件...

安裝zsh

sudo yum install -y zsh
zsh --version
sudo chsh -s $(which zsh)
# 注銷

安裝 powerline

可以通過pip安裝:

pip install powerline-status

:bookmark: 參考文章:

powerline - Installation: https://powerline.readthedocs.io/en/latest/installation.html#pip-installation

安裝 oh-my-zsh

sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

:warning: 注意:

如果連不上: <raw.githubusercontent.com>, 就從github對應(yīng)的地址: https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh把腳本復(fù)制下來運行.

安裝zsh插件: zsh-autosuggestionszsh-syntax-highlighting

:bookmark: 參考文檔:

zsh-syntax-highlighting

  1. clone: git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  2. ~/.zshrc激活插件: plugins=( [plugins...] zsh-syntax-highlighting)
  3. 重啟zsh

zsh-autosuggestions

  1. clone: git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  2. ~/.zshrc激活插件: plugins=( [plugins...] zsh-autosuggestions)
  3. 重啟zsh

使用 oh-my-zsh

編輯zshrc文件: vi ~/.zshrc

# 修改主題
ZSH_THEME="agnoster"

# 啟用插件
plugins=(
  git
  ansible
  docker-compose
  docker
  helm
  kubectl
  minikube
  oc
  pip
  python
  ubuntu
  zsh-autosuggestions
  zsh-syntax-highlighting
)

:notebook: 備注:

  • helm: k8s上的鏡像包管理工具
  • minikube: 最小化K8S安裝工具
  • oc: K8S的RedHat商業(yè)發(fā)行版(OpenShift)的命令行工具

最終效果

按需安裝常用軟件

目的: 根據(jù)自己需要, 按需安裝常用軟件和工具

sudo yum -y install dnsmasq httpd haproxy nginx \
                    python3 \
                    genisoimage libguestfs-tools

按需配置服務(wù)和開機自啟動:

systemctl enable haproxy.service 
systemctl start haproxy.service 
...

安裝jq, jq安裝鏈接https://stedolan.github.io/jq/download/. JQ是個json格式化命令行工具, 在日常管理K8S中很有用.

安裝容器類組件

docker全家桶

建議直接安裝docker全家桶, 省心省力

:bookmark: 參考文檔:

Install Docker Engine on CentOS: https://docs.docker.com/engine/install/centos/

  1. 卸載老版本:

    $ sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
    
  2. 配置REPOSITORY

    $ sudo yum install -y yum-utils
    
    $ sudo yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
    
  3. 安裝:

    $ sudo yum install docker-ce docker-ce-cli containerd.io
    
  4. 啟動:

    $ sudo systemctl start docker
    
  5. 驗證:

    $ sudo docker run hello-world
    

其他開源組件

對于RedHat系, 可能要安裝多個組件替代:

sudo yum -y install buildah podman skopeo

:notebook: 備注:

  • buildah: 構(gòu)建容器鏡像的組件
  • podman: 運行容器鏡像的組件
  • skopeo: 傳輸移動容器鏡像的組件

安裝 kubectl

官方安裝文檔: https://kubernetes.io/zh/docs/tasks/tools/install-kubectl/

  1. 下載: curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
  2. 標記 kubectl文件為可執(zhí)行:chmod +x ./kubectl
  3. 將文件放到 PATH 路徑下:sudo mv ./kubectl /usr/local/bin/kubectl
  4. 測試你所安裝的版本是最新的:kubectl version --client

安裝 minikube 或 kind

這里以 minikube 為例:

官方安裝文檔: https://kubernetes.io/zh/docs/tasks/tools/install-minikube/

:warning: 需要強調(diào)的是:

  1. 看中文文檔
  2. :notebook: 說明: 由于國內(nèi)無法直接連接 k8s.gcr.io,推薦使用阿里云鏡像倉庫,在 minikube start 中添加--image-repository 參數(shù)。
  3. 示例: minikube start --vm-driver=<驅(qū)動名稱> --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

安裝 helm v3

二進制CLI下載地址https://github.com/helm/helm/releases/latest

安裝源文檔: https://helm.sh/docs/intro/install/

$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh

安裝OpenShift 命令行 oc

直接下載二進制CLI安裝: https://mirror.openshift.com/pub/openshift-v4/clients/oc/

安裝OpenShift for Developer命令行odo

直接下載二進制CLI安裝:https://mirror.openshift.com/pub/openshift-v4/clients/odo/latest/

安裝 Tekton - K8S原生CI/CD工具

CLI工具叫做tkn, 官方文檔: https://github.com/tektoncd/cli

安裝:

# Get the tar.xz
curl -LO https://github.com/tektoncd/cli/releases/download/v0.12.0/tkn_0.12.0_Darwin_x86_64.tar.gz
# Extract tkn to your PATH (e.g. /usr/local/bin)
sudo tar xvzf tkn_0.12.0_Darwin_x86_64.tar.gz -C /usr/local/bin tkn

當然, golang 環(huán)境也是必不可少的.

最后祝大家用的順手! :muscle::muscle::muscle:

本文由東風微鳴技術(shù)博客 EWhisper.cn 編寫!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容