kubectl get po -l type=app # 搜索标签 type=app
kubectl get po -A -l type=app # 搜索所有命名空间标签 type=app
kubectl get po -A -l type=app --show-labels # 搜索 type=app, 并显示详细标签信息
kubect get po -l "type in (app,app1,app2)" # 搜索标签 type 包含其中一个值标签
kubect get po -l version!=1.0.0,type=app # 搜索多标签 version!=1.0.0,type=app
# 扩容不是滚动更新
kubectl edit deploy nginx-deploy
replicas: 3 # 期望副本数
# 修改下镜像,保存执行滚动更新
kubectl edit deploy nginx-deploy # 或者命令方式 kubectl set image deployment/nginx-deploy nginx=nginx:1.8
- image: nginx:1.8 # 修改下镜像
# 观察自滚动更新状态
[root@k8s-master1 ~]# kubectl rollout status deploy nginx-deploy
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deploy" successfully rolled out
[root@k8s-master1 ~]# kubectl create -f statefulset.yaml
service/nginx created
statefulset.apps/web created
[root@k8s-master1 ~]# kubectl get sts,svc,pod
NAME READY AGE
statefulset.apps/web 2/2 9s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12h
service/nginx ClusterIP None <none> 80/TCP 9s
NAME READY STATUS RESTARTS AGE
pod/web-0 1/1 Running 0 9s
pod/web-1 1/1 Running 0 7s
[root@k8s-master1 ~]# kubectl scale sts web --replicas=4 # 命令方式
statefulset.apps/web scaled
[root@k8s-master1 ~]# kubectl patch sts web -p '{"spec":{"replicas": 32}}' # json 方式
statefulset.apps/web patched
[root@k8s-master1 ~]# kubectl get sts
NAME READY AGE
web 2/2 9m51s
# 2.执行命令 `kubectl autoscale deploy <deploy. name> --cpu-percent= 20 --min=2 --max=5`
[root@k8s-master1 ~]# kubectl autoscale deploy nginx-deploy --cpu-percent=20 --min=2 --max=5
horizontalpodautoscaler.autoscaling/nginx-deploy autoscaled
# 3.通过`kubectl get hpa`可以获取HPA信息
[root@k8s-master1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-deploy Deployment/nginx-deploy <unknown>/20% 2 5 2 111s
[root@k8s-master1 ~]# kubectl top pods
NAME CPU(cores) MEMORY(bytes)
nginx-deploy-bc96d8cd9-74ms5 0m 1Mi
nginx-deploy-bc96d8cd9-khc68 0m 1Mi
# 测试:找到对应服务的service, 编写循环测试脚本提升内存与cpu负载,可以通过多台机器执行上述命令,增加负载,当超过负载后可以查看pods的打容情况
while true; do wget -q -O- http://<ip:port> > /dev/nul; done
# 查看pods资源使用情况
kubectl get pods
kubectl top pods
kubectl get hpa -w
kubectl get deploy -w
# 创建service
kubectl create -f nginx-sVc.yaml
# 查看service信息,通过service的cluster ip进行访问
kubectl get svc
# 查看pod信息,通过pod的ip进行访问
kubectl get po -owide
# 创建其他 pod 通过 service name进行访问(推荐)
kubectl exec -it busybox -- sh # busybox 是自己创建的临时 pod
curl htp://nginx-gvc
# 默认在当前 namespace 中访问,如果需要跨namespace访问pod,则在service name后面加上.<namespace>即可
curl http://nginx-svc.default
# nginx-svc-endpoint.yaml 使用 service 代理外部 IP 访问
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc-external
labels:
app: nginx
spec:
ports:
- port: 80
targetPort: 80
name: web
type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
labels:
app: nginx # 与 service 标签一致
name: nginx-svc-external # 与 service 名字一致
namespace: default # 与 service 一致
subsets:
- addresses:
- ip: <远程地址> # 代理转发的目标 ip 地址
ports: # 与 service 一致
- name: web
port: 80
protocol: TCP
# nginx-svc-domain.yaml 使用 service 代理外部 域名 访问
[root@k8s-master1 ~]# cat nginx-domain.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc-domain
labels:
app: nginx-svc-domain
spec:
type: ExternalName
externalName: www.baidu.com
[root@k8s-master1 ~]# kubectl get svc -l app=nginx-svc-domain
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-svc-domain ExternalName <none> www.baidu.com <none> 41s
kubectl get svc, ep
cd /opt/ && mkdir helm && cd helm/
wget https://get.helm.sh/helm-v3.2.3-linux-amd64.tar.gz
tar fx helm-v3.2.3-linux-amd64.tar.gz
cp linux-amd64/helm /usr/bin/
[root@k8s-master1 helm]# which helm
/usr/bin/helm
[root@k8s-master1 helm]# helm version
version.BuildInfo{Version:"v3.2.3", GitCommit:"8f832046e258e2cb800894579b1b3b50c2d83492", GitTreeState:"clean", GoVersion:"go1.13.12"}
# 添加仓库
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
# 查看仓库列表
helm repo list
# 搜索ingress-nginx
[root@k8s-master1 helm]# helm search repo ingress-nginx
NAME CHART VERSION APP VERSION DESCRIPTION
ingress-nginx/ingress-nginx 4.7.1 1.8.1 Ingress controller for Kubernetes using NGINX a...
# 下载包
helm pull ingress-nginx/ingress-nginx
# 解压
tar fx ingress-nginx-4.7.1.tgz
cd ingress-nginx
# 1. 修改 values.yaml 镜像地址:为国内镜像, 国外无需修改镜像
controller:
name: controller
image:
## Keep false as default for now!
chroot: false
registry: registry.cn-hangzhou.aliyuncs.com
image: google_containers/nginx-ingress-controller
.... 并注释掉 Hash 校验值,因为修改了镜像地址,所以不需要校验
# digest: sha256:e5c4824e7375fcf2a393e1c03c293b69759af37a9ca6abdb91b13d78a93da8bd
# digestChroot: sha256:e0d4121e3c5e39de9122e55e331a32d5ebf8d4d257227cb93ab54a1b912a7627
... 搜索 kube-webhook 修改配置如下
patch:
enabled: true
image:
registry: registry.cn-hangzhou.aliyuncs.com
image: google_containers/kube-webhook-certgen
tag: v1.5.1
## for backwards compatibility consider setting the full image url via the repository value below
## use *either* current default registry/image or repository format or installing chart by providing the values.yaml will fail
## repository:
#tag: v20230407
#digest: sha256:543c40fd093964bc9ab509d3e791f9989963021f1e9e4c9c7b6700b02bfb227b
#pullPolicy: IfNotPresent
# 2. 修改部署配置的 kind 为 DaemonSet, nodeSelector.ingress 为 true
# -- Use a `DaemonSet` or `Deployment`
kind: DaemonSet
nodeSelector:
kubernetes.io/os: linux
ingress: "true"
# 3. 修改其他参数
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
type: ClusterIP # 如果是云平台使用 LoadBalancer
admissionWebhooks:
...
enabled: false # 设置为 false,true 为https证书方式
kubectl create ns ingress-nginx
# 为需要部署 ingress 的节点的加标签, 我们这里部署到 node1 商
kubectl label node k8s-node1 ingress=true
# 安装 ingress-nginx
helm install ingress-nginx -n ingress-nginx .
# 如果安装失败,先卸载,修改之后重新执行安装:helm uninstall ingress-nginx -n ingress-nginx
# 之后安装到 node1 节点,
[root@k8s-master1 ingress-nginx]# kubectl get po -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx-controller-94vcq 0/1 ContainerCreating 0 38s 192.168.8.129 k8s-node1 <none> <none>
[root@k8s-master1 ~]# kubectl get svc |grep nginx-svc # 目前已经有一个 svc
nginx-svc NodePort 10.103.189.179 <none> 80:32572/TCP 9h
[root@k8s-master1 ~]# cat ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress # 资源类型为 Ingress
metadata:
name: wolfcode-nginx-ingress
annotations:
kubernetes.io/ingress.class: "nignx"
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules: # Ingress 规则配置,可以配置多个
- host: kk8s.wolfcode.cn #域名配置,可以使用通配符 *
http:
paths: # 相当于 nginx 的location 配置,可以配置多个
- pathType: Prefix # 路径类型,安装路径类型进行匹配 ImplementationSpecific 需要指定 IngressClass, 具体匹配规则以 IngressClass 中的规则为准。 Exact: 精确匹配, URL 需要与path完全匹配上,且区分大小写。 Prefix: 以 / 作为分隔符来进行前缀匹配
backend:
service:
name: nginx-svc # 代理到哪个 service
port:
number: 80 # services 的端口
path: /api # 等价于 nginx 中的 location 的路径前缀匹配,可正则,和nginx相同
[root@k8s-master1 ~]# kubectl create -f ingress.yaml
ingress.networking.k8s.io/wolfcode-nginx-ingress created
[root@k8s-master1 ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
wolfcode-nginx-ingress <none> kk8s.wolfcode.cn 80 2m54s
[root@k8s-master1 ~]# kubectl get po -n ingress-nginx -o wide # 反向只有 node1 节点上创建了 pod
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx-controller-94vcq 1/1 Running 0 24m 192.168.8.129 k8s-node1 <none> <none>
[root@k8s-master1 ~]# helm ls -A
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
ingress-nginx ingress-nginx 1 2023-07-03 19:44:08.059637001 +0800 CST deployed ingress-nginx-4.7.1 1.8.1
# 帮助
kubectl create cm -h
Examples:
# Create a new config map named my-config based on folder bar
kubectl create configmap my-config --from-file=path/to/bar
# Create a new config map named my-config with specified keys instead of file basenames on disk
kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
# Create a new config map named my-config with key1=config1 and key2=config2
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
# Create a new config map named my-config from the key=value pairs in the file
kubectl create configmap my-config --from-file=path/to/bar
# Create a new config map named my-config from an env file
kubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env
# 文件内容, k: v形式、k=v形式、 yaml 格式
kubectl get sa # 查看认证账户
# 查看已有命名空间角色信息
kubectl get role -A
# 查看已有已有集群空间角色信息
kubectl get clusterrole
# 查看 rolebinding 信息
kubectl get rolebinding -A
# 查看指定 rolebinding 配置信息
kubect get rolebinding <role_binding_name> -A -o yaml