跳到主要内容

4、kubectl插件管理器Krew安装与使用

实战:kubectl 插件管理器 Krew 安装与使用-2023.3.3(测试成功)

image-20230303140335996

目录

[toc]

实验环境

实验环境:
1、win10,vmwrokstation虚机;
2、k8s集群:3台centos7.6 1810虚机,1个master节点,2个node节点
k8s version:v1.22.2
containerd: v1.5.5

实验软件

链接:https://pan.baidu.com/s/1aWwuXFX7PMzY_NUTz8u0lw?pwd=4h7f 提取码:4h7f 2023.3.3-实战:kubectl 插件管理器 Krew 安装与使用-2023.3.3(测试成功)

image-20230303123449779

前言

Krew 是一个由 Kubernetes SIG CLI 社区维护的 kubectl 命令行工具的插件管理器。类似 红帽的YUM, 开发角度理解,类似 Nodejs 的 npm。

Krew 可以用于管理 kubelet 插件,发现 kubectl 插件,并在机器上安装它们。保持安装的插件是最新的。Krew 适用于所有主要平台,例如 macOS、Linux 和 Windows。

需要说明的是,Krew 插件索引 所维护的 kubectl 插件并 未经过安全性审查,你要了解安装和运行第三方插件的安全风险。

国内因为墙的问题,无法正常使用,所以需要科学上网。所以如果是内网,或者是没办法科学上网,装了姐也没啥用。

1、安装krew工具

krew软件地址

https://github.com/kubernetes-sigs/krew

image-20230302222742891

What does Krew do?

Krew is a tool that makes it easy to use kubectl plugins. Krew helps you discover plugins, install and manage them on your machine. It is similar to tools like apt, dnf or brew. Today, over 200 kubectl plugins are available on Krew.

  • For kubectl users: Krew helps you find, install and manage kubectl plugins in a consistent way.
  • For plugin developers: Krew helps you package and distribute your plugins on multiple platforms and makes them discoverable.

方法1:安装krew软件(常规二进制安装)

  • 下载软件

https://github.com/kubernetes-sigs/krew/releases/tag/v0.4.3

https://github.com/kubernetes-sigs/krew/releases/download/v0.4.3/krew-linux_amd64.tar.gz

image-20230302223105589

  • 执行命令
[root@master1 ~]#ll -h krew-linux_amd64.tar.gz 
-rw-r--r-- 1 root root 4.0M Mar 2 22:31 krew-linux_amd64.tar.gz

[root@master1 ~]#tar tf krew-linux_amd64.tar.gz
./LICENSE
./krew-linux_amd64
#步骤1:解压
[root@master1 ~]#tar xf krew-linux_amd64.tar.gz
[root@master1 ~]#ll krew-linux_amd64
-rwxr-xr-x 1 1001 121 11836580 Jan 1 2000 krew-linux_amd64

#步骤2:移动
[root@master1 ~]#mv krew-linux_amd64 /usr/bin/krew
[root@master1 ~]#ll /usr/bin/krew
-rwxr-xr-x 1 1001 121 11836580 Jan 1 2000 /usr/bin/krew

#步骤3:添加环境变量
[root@master1 ~]#vim .bashrc
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" #追加这一行
[root@master1 ~]#source .bashrc
  • 验证
#验证
[root@master1 ~]#krew --help
krew is the kubectl plugin manager.
You can invoke krew through kubectl: "kubectl krew [command]..."

Usage:
kubectl krew [command]

Available Commands:
completion generate the autocompletion script for the specified shell
help Help about any command
index Manage custom plugin indexes
info Show information about an available plugin
install Install kubectl plugins
list List installed kubectl plugins
search Discover kubectl plugins
uninstall Uninstall plugins
update Update the local copy of the plugin index
upgrade Upgrade installed plugins to newer versions
version Show krew version and diagnostics

Flags:
-h, --help help for krew
-v, --v Level number for the log level verbosity

Use "kubectl krew [command] --help" for more information about a command.

[root@master1 ~]#krew version
OPTION VALUE
GitTag v0.4.3
GitCommit dbfefa5
IndexURI https://github.com/kubernetes-sigs/krew-index.git
BasePath /root/.krew
IndexPath /root/.krew/index/default
InstallPath /root/.krew/store
BinPath /root/.krew/bin
DetectedPlatform linux/amd64
[root@master1 ~]#

方法2:安装krew软件(官方脚本)

  • 安装软件(官方安装命令)

https://krew.sigs.k8s.io/docs/user-guide/setup/install/

image-20230302224953644

(
set -x; cd "$(mktemp -d)" &&
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
KREW="krew-${OS}_${ARCH}" &&
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
tar zxvf "${KREW}.tar.gz" &&
./"${KREW}" install krew
)

(1)步骤1:安装git

注意:这里是需要安装git的。

yum install -y git

(2)执行脚本命令

(
set -x; cd "$(mktemp -d)" &&
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
KREW="krew-${OS}_${ARCH}" &&
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
tar zxvf "${KREW}.tar.gz" &&
./"${KREW}" install krew
)

(3)添加环境变量

[root@master1 ~]#vim .bashrc
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" #追加这一行
[root@master1 ~]#source .bashrc

(4)验证

#验证
[root@master1 ~]#krew --help
krew is the kubectl plugin manager.
You can invoke krew through kubectl: "kubectl krew [command]..."

Usage:
kubectl krew [command]

Available Commands:
completion generate the autocompletion script for the specified shell
help Help about any command
index Manage custom plugin indexes
info Show information about an available plugin
install Install kubectl plugins
list List installed kubectl plugins
search Discover kubectl plugins
uninstall Uninstall plugins
update Update the local copy of the plugin index
upgrade Upgrade installed plugins to newer versions
version Show krew version and diagnostics

Flags:
-h, --help help for krew
-v, --v Level number for the log level verbosity

Use "kubectl krew [command] --help" for more information about a command.

[root@master1 ~]#krew version
OPTION VALUE
GitTag v0.4.3
GitCommit dbfefa5
IndexURI https://github.com/kubernetes-sigs/krew-index.git
BasePath /root/.krew
IndexPath /root/.krew/index/default
InstallPath /root/.krew/store
BinPath /root/.krew/bin
DetectedPlatform linux/amd64
[root@master1 ~]#

2、使用kubectl-plugin

1.krew插件管理器使用案例

Quickstart

Krew helps you discover and install kubectl plugins on your machine.

You can install and use a wide variety of kubectl plugins to enhance your Kubernetes experience.

Let’s get started:

  1. Install and set up Krew on your machine.

  2. Download the plugin list:

    $ kubectl krew update

    这里有时会无法拉取的:(一会儿可以,一会儿不行,还是因为墙的问题……,可能会拉取github超时)

    image-20230303103046275

  3. Discover plugins available on Krew:

    $ kubectl krew search
    NAME DESCRIPTION INSTALLED
    access-matrix Show an RBAC access matrix for server resources no
    advise-psp Suggests PodSecurityPolicies for cluster. no
    auth-proxy Authentication proxy to a pod or service no
    [...]
  4. Choose a plugin from the list and install it:

    $ kubectl krew install access-matrix

    这里也会有时拉取不下来的:

    image-20230303103506454

  5. Use the installed plugin:

    $ kubectl access-matrix
  6. Keep your plugins up-to-date:

    $ kubectl krew upgrade
  7. Uninstall a plugin you no longer use:

    $ kubectl krew uninstall access-matrix

This is practically all you need to know to start using Krew.

注意:自己的pc是有连接vpn科学上网的,但是里面的虚机好像并不能正常使用这个vpn,因此这些无法被正常拉取下来的;

2.安装kubectl插件ingress-nginx

 [root@master1 ~]#krew install ingress-nginx
Updated the local copy of plugin index.
Installing plugin: ingress-nginx
Installed plugin: ingress-nginx
\
| Use this plugin:
| kubectl ingress-nginx
| Documentation:
| https://kubernetes.github.io/ingress-nginx/kubectl-plugin/
/
WARNING: You installed plugin "ingress-nginx" from the krew-index plugin repository.
These plugins are not audited for security by the Krew maintainers.
Run them at your own risk.
[root@master1 ~]#

注意:

[root@master1 ~]#kubectl exec -it ingress-nginx-controller-c66bc7c5c-pj2h8 -n ingress-nginx -- cat /etc/nginx/nginx.conf
……

upstream upstream_balancer {
### Attention!!!
#
# We no longer create "upstream" section for every backend.
# Backends are handled dynamically using Lua. If you would like to debug
# and see what backends ingress-nginx has in its memory you can
# install our kubectl plugin https://kubernetes.github.io/ingress-nginx/kubectl-plugin.
# Once you have the plugin you can use "kubectl ingress-nginx backends" command to
# inspect current backends.
#
###

server 0.0.0.1; # placeholder

balancer_by_lua_block {
balancer.balance()
}

keepalive 320;
keepalive_time 1h;
keepalive_timeout 60s;
keepalive_requests 10000;

}

image-20230302214942162

  • 插件使用过程
[root@master1 ~]#kubectl ingress-nginx backends -n ingress-nginx
[
{
"name": "default-my-nginx-80",
"service": {
"metadata": {
"creationTimestamp": null
},
"spec": {
"ports": [
{
"name": "http",
"protocol": "TCP",
"port": 80,
"targetPort": 80
}
],
"selector": {
"app": "my-nginx"
},
"clusterIP": "10.99.186.115",
"clusterIPs": [
"10.99.186.115"
],
"type": "ClusterIP",
"sessionAffinity": "None",
"ipFamilies": [
"IPv4"
],
"ipFamilyPolicy": "SingleStack",
"internalTrafficPolicy": "Cluster"
},
"status": {
"loadBalancer": {}
}
},
"port": 80,
"sslPassthrough": false,
"endpoints": [
{
"address": "10.244.2.7",
"port": "80"
}
],
"sessionAffinityConfig": {
"name": "",
"mode": "",
"cookieSessionAffinity": {
"name": ""
}
},
"upstreamHashByConfig": {
"upstream-hash-by-subset-size": 3
},
"noServer": false,
"trafficShapingPolicy": {
"weight": 0,
"weightTotal": 0,
"header": "",
"headerValue": "",
"headerPattern": "",
"cookie": ""
}
},
{
"name": "upstream-default-backend",
"port": 0,
"sslPassthrough": false,
"endpoints": [
{
"address": "127.0.0.1",
"port": "8181"
}
],
"sessionAffinityConfig": {
"name": "",
"mode": "",
"cookieSessionAffinity": {
"name": ""
}
},
"upstreamHashByConfig": {},
"noServer": false,
"trafficShapingPolicy": {
"weight": 0,
"weightTotal": 0,
"header": "",
"headerValue": "",
"headerPattern": "",
"cookie": ""
}
}
]

引用

CSDN博主「山河已无恙」文章:https://blog.csdn.net/sanhewuyang/article/details/128752814

https://krew.sigs.k8s.io/

https://krew.sigs.k8s.io/docs/user-guide/quickstart/

https://github.com/kubernetes-sigs/krew

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 微信二维码 x2675263825 (舍得), qq:2675263825。

image-20230107215114763

🍀 微信公众号 《云原生架构师实战》

image-20230107215126971

🍀 csdn https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

image-20230107215149885

🍀 知乎 https://www.zhihu.com/people/foryouone

image-20230107215203185

最后

好了,关于本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!

image-20230303095531877

1