kubectl 命令的使用

Kubectl 是 Kubernetes 集群的命令行工具,用于部署、扩展和管理容器化应用程序。它提供了资源管理、服务发现、扩缩容等丰富的命令,是 Kubernetes 用户日常操作集群的主要方式。

k8s命令自动补全:

1
2
3
4
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

应用部署

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[root@testcce-26771 ~]# kubectl create deployment web --image=nginx --replicas=1
deployment.apps/web created

[root@testcce-26771 ~]# kubectl expose deployment web --port=8080 --target-port=80
service/web exposed
[root@testcce-26771 ~]# kubectl get  svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.247.0.1       <none>        443/TCP    59m
web          ClusterIP   10.247.145.241   <none>        8080/TCP   6s

[root@testcce-26771 ~]# kubectl get  svc web -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2023-09-29T03:43:43Z"
  labels:
    app: web
  name: web
  namespace: default
  resourceVersion: "15468"
  uid: 7e3bd0d8-fc7d-48d6-8ddb-16959c1f791b
spec:
  clusterIP: 10.247.145.241
  clusterIPs:
  - 10.247.145.241
  internalTrafficPolicy: Cluster
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

[root@testcce-26771 ~]# kubectl exec -it web-8667899c97-r7nmq  -- /bin/bash

root@web-8667899c97-r7nmq:/# curl 10.247.145.241:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

捐赠本站(Donate)

weixin_pay
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))