容器开发运维人员的 Linux 操作机配置优化建议

"工欲善其事必先利其器", 作为一个PAAS平台架构师, 容器相关技术(docker, k8s等)是必不可少的. 本文简单介绍下我自己的Linux操作机配置. 提升工作效率, 提高使用体验. :heart::heart::heart:

:exclamation: 注意:

本文以CentOS 7.6 为例, RHEL7.6 操作类似.

Ubuntu系统操作可以触类旁通. 没啥难度.

另外下文中会有一些"可选"项, 主要是针对一些特殊情况, 如: 需要通过代理连接互联网...

更换OS 软件安装源

目的: 加快软件下载速度.

可以换成: 阿里, 腾讯, 清华, 中科大...的源.

以清华Mirror为例, 操作步骤如下:

:bookmark: 参考文章:

清华大学开源软件镜像站 - 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. 用下面内容覆盖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

    # 查看当前代理设置
    git config --global http.proxy
    
    # 设置当前代理为 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, 如对应仓库的origin需要Bypass: git config --add remote.origin.proxy ""

优化配置Shell

目的: zsh + plugins, 提供丰富而友好的shell体验. 如: 语法高亮, 自动补全, 自动建议, 容器相关插件...

安装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对应的地址: https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh把脚本复制下来运行.

安装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商业发行版(OpenShift)的命令行工具

最终效果

按需安装常用软件

目的: 根据自己需要, 按需安装常用软件和工具

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

按需配置服务和开机自启动:

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: 构建容器镜像的组件
  • 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文件为可执行: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: 需要强调的是:

  1. 看中文文档
  2. :notebook: 说明: 由于国内无法直接连接 k8s.gcr.io,推荐使用阿里云镜像仓库,在 minikube start 中添加--image-repository 参数。
  3. 示例: minikube start --vm-driver=<驱动名称> --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 环境也是必不可少的.

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

本文由东风微鸣技术博客 EWhisper.cn 编写!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 175,490评论 5 419
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 74,060评论 2 335
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 124,407评论 0 291
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 47,741评论 0 248
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 56,543评论 3 329
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 43,040评论 1 246
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 34,107评论 3 358
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 32,646评论 0 229
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 36,694评论 1 271
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 32,398评论 2 279
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 33,987评论 1 288
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 30,097评论 3 285
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 35,298评论 3 282
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 27,278评论 0 14
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 28,413评论 1 232
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 38,397评论 2 309
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 38,099评论 2 314

推荐阅读更多精彩内容