【k8s-0】--命令与配置文件

   kubectl get pods
   kubectl get pods -o wide
   kubectl get deployments
#加副本
   kubectl scale deploy   nginx-deployment  --replicas=3
#镜像更新失败回滚之前操作
kubectl  rollout  undo  deploy  nginx-deployment

[root@k8s-master-lzy k8s_test]# kubectl get pods  -l app=nginx
NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-569477d6d8-6xc85   1/1       Running   0          11m
nginx-deployment-569477d6d8-78pln   1/1       Running   0          11m

[root@k8s-master-lzy ~]# kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1            443/TCP   8d
[root@k8s-master-lzy ~]# kubectl describe  service kubernetes
Name:              kubernetes
Namespace:         default
Labels:            component=apiserver
                   provider=kubernetes
Annotations:       
Selector:          
Type:              ClusterIP
IP:                10.96.0.1
Port:              https  443/TCP
TargetPort:        6443/TCP
Endpoints:         172.16.31.151:6443
Session Affinity:  ClientIP
Events:            


#yml启动
kubectl create -f nginx-deployment2.yaml 
#YML

3.kube-proxy kube-dns

#proxy将容器中的服务通过proxy对外映射除去
#找到相应deploy
[root@k8s-master-lzy k8s_test]# kubectl get deploy
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
curl               1         1         1            1           8d
nginx-deployment   2         2         2            2           8d
#暴露deploy
[root@k8s-master-lzy k8s_test]# kubectl expose  deploy  nginx-deployment  --type="NodePort"  --target-port=80  --port=80
service "nginx-deployment" exposed
[root@k8s-master-lzy k8s_test]# kubectl get services
NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes         ClusterIP   10.96.0.1              443/TCP        8d
nginx-deployment   NodePort    10.99.252.93           80:32162/TCP   10s
[root@k8s-master-lzy k8s_test]# netstat -luntp | grep 32162
tcp6       0      0 :::32162                :::*                    LISTEN      12109/kube-proxy    
[root@k8s-master-lzy k8s_test]# 

[图片上传中...(image.png-263681-1543571709033-0)]

4:proxy:每台工作节点上都应该运行一个kube-proxy服务,它监听API server中service和endpoint的变化情况,并通过iptables等来为服务配置负载均衡,是让我们的服务在集群外可以被访问到的重要方式
如何创建service:

vim nginx-service.yaml 


apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  port:
  - port: 80
    targetPort: 80
    nodePort: 20000
  selector:
    app: nginx
  type: NodePort

@ok
[root@k8s-master-lzy k8s_test]# kubectl create -f nginx-service.yaml 
service "nginx-service" created

图中service已经启动并构建

5.kube-dns:kube-dns为Kubernetes集群提供命名服务,主要用来解析集群服务名和Pod的hostname。目的是让pod可以通过名字访问到集群内服务。它通过添加A记录的方式实现名字和service的解析。普通的service会解析到service-ip。headless service会解析到pod列表。

如何通过dns 访问服务:
如何查看系统svc是否启动:

[root@k8s-master-lzy k8s_test]# kubectl  -n kube-system  get svc
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
kube-dns               ClusterIP   10.96.0.10              53/UDP,53/TCP   8d
kubernetes-dashboard   NodePort    10.101.151.51           443:30000/TCP   8d

5.练习:

如何查看pods 日志:
[root@k8s-master-lzy ~]# kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
curl-6896d87888-7vltw               1/1       Running   1          8d
nginx-deployment-569477d6d8-6xc85   1/1       Running   0          2h
nginx-deployment-569477d6d8-78pln   1/1       Running   0          2h

[root@k8s-master-lzy ~]# kubectl logs  nginx-deployment-569477d6d8-78pln   -f

如何进入pod内部:
[root@k8s-master-lzy ~]# kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
curl-6896d87888-7vltw               1/1       Running   1          8d
nginx-deployment-569477d6d8-6xc85   1/1       Running   0          2h
nginx-deployment-569477d6d8-78pln   1/1       Running   0          2h
[root@k8s-master-lzy ~]# kubectl exec -it nginx-deployment-569477d6d8-6xc85  bash
root@nginx-deployment-569477d6d8-6xc85:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  selinux  srv  sys  tmp  usr  var

查看sa 并以yaml格式输出:
[root@k8s-master-lzy ~]# kubectl get sa  -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: ServiceAccount
  metadata:
    creationTimestamp: 2018-11-21T11:32:00Z
    name: default
    namespace: default
    resourceVersion: "293"
    selfLink: /api/v1/namespaces/default/serviceaccounts/default
    uid: 0fdad53b-ed81-11e8-8f47-00163e0227ca
  secrets:
  - name: default-token-26l9m
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

更新yaml 文件

[root@k8s-master-lzy k8s_test]# kubectl apply -f nginx-deployment2.yaml 
deployment "nginx-deployment" configured


#测试用的 沙盒pod
 kubectl run busybox   --rm=true    --image=busybox   --restart=Never   --tty   -i

6.容器编排微服务。

你可能感兴趣的:(【k8s-0】--命令与配置文件)