應用k8s入門

1,kubectl run
創建pods

[root@master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
[root@master ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-7689897d8d-7wr6d   1/1     Running   0          17s
[root@master ~]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
nginx-deploy-7689897d8d-7wr6d   1/1     Running   0          24s   10.244.1.12   node01   <none>           <none>
[root@master ~]# kubectl get deployment
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1/1     1            1           119s
訪問:
[root@node01 ~]# curl  10.244.1.12
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
[root@node01 ~]# 
創建svc:
[root@master ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
service/nginx exposed
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   5d3h
nginx        ClusterIP   10.101.119.241   <none>        80/TCP    8s
下載bind-utils:
[root@master ~]# yum install -y bind-utils
run一個client:
[root@master ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never
If you don't see a command prompt, try pressing enter.
/ # cat /etc/resolv.conf 
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
[root@master ~]# dig -t A nginx.default.svc.cluster.local svc.cluster.local @10.96.0.10
在client訪問:
/ # wget -O - -q http://nginx:80/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
/ # 
編輯服務nginx:
[root@master ~]# kubectl edit svc nginx
更新鏡像:
[root@master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
deployment.extensions/myapp image updated
宿主機訪問:
[root@master ~]# kubectl edit svc myapp
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2019-09-23T07:15:21Z"
  labels:
    run: myapp
  name: myapp
  namespace: default
  resourceVersion: "102661"
  selfLink: /api/v1/namespaces/default/services/myapp
  uid: 6c482a98-0065-4a04-89e1-e74847898327
spec:
  clusterIP: 10.106.65.163
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: myapp
  sessionAffinity: None
  type: NodePort
  [root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        5d4h
myapp        NodePort    10.106.65.163    <none>        80:30167/TCP   13m
nginx        ClusterIP   10.101.119.241   <none>        80/TCP         41m

2,kubernetes資源清單定義入門
apiVersion:group/cersion

[root@master ~]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
[root@master ~]# 
kind:資源類別
metadata:元數據
	name
	namespace
	annotations
spec:期望狀態
status:當前狀態

編寫一個資源清單:
[root@master manifests]# vi pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
  - name: busybox
    image: busybox:latest
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 3600"
創建pod:
[root@master manifests]# kubectl create -f pod-demo.yaml 
pod/pod-demo created

3,pod控制器應用
標籤:
key=value
key:字母、數字、_、-、.
value:可以爲空,只能字母或數字開頭及結尾

查看標籤:
[root@master manifests]# kubectl get pods --show-labels
NAME                            READY   STATUS      RESTARTS   AGE    LABELS
client                          0/1     Completed   0          117m   run=client
myapp-746644f8d6-2msxj          1/1     Running     0          92m    pod-template-hash=746644f8d6,run=myapp
myapp-746644f8d6-ch5n2          1/1     Running     0          92m    pod-template-hash=746644f8d6,run=myapp
nginx-deploy-7689897d8d-rvp28   1/1     Running     0          132m   pod-template-hash=7689897d8d,run=nginx-deploy
[root@master manifests]# 
[root@master manifests]# kubectl get pods -l app
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   0          19s
[root@master manifests]# 
添加標籤:
[root@master manifests]# kubectl label pods pod-demo release=canary
pod/pod-demo labeled
[root@master manifests]# kubectl get pods --show-labels
NAME                            READY   STATUS      RESTARTS   AGE     LABELS
client                          0/1     Completed   0          121m    run=client
myapp-746644f8d6-2msxj          1/1     Running     0          96m     pod-template-hash=746644f8d6,run=myapp
myapp-746644f8d6-ch5n2          1/1     Running     0          95m     pod-template-hash=746644f8d6,run=myapp
nginx-deploy-7689897d8d-rvp28   1/1     Running     0          136m    pod-template-hash=7689897d8d,run=nginx-deploy
pod-demo                        2/2     Running     0          2m26s   app=myapp,release=canary,tier=frontend
[root@master manifests]# 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章