Kubernetes Questions

, , Comments Off on Kubernetes Questions

Expose a pod and create a node port service.

Create a pod with a label.
kubectl run my-app --image=nginx --port=80 --labels="app=my-app"
Create the node port service using a name to match the pod’s label. Otherwise, you’ll need to modify the service’s selector to match the node’s label.
kubectl create service nodeport my-app --tcp=80
Run kubectl get pods -o wide and kubectl describe service my-app to ensure the service endpoint(s) match the pod’s IP address.
kubectl get pods -o wide
NAME     READY   STATUS    RESTARTS   AGE   IP           NODE                 NOMINATED NODE   READINESS GATES
my-app   1/1     Running   0          60s   10.244.1.2   multinode-demo-m02   <none>           <none>
kubectl describe service my-app
Name:                     my-app
Namespace:                default
Labels:                   app=my-app
Annotations:              <none>
Selector:                 app=my-app
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.105.235.192
IPs:                      10.105.235.192
Port:                     80  80/TCP
TargetPort:               80/TCP
NodePort:                 80  31851/TCP
Endpoints:                10.244.1.2:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Create a new service account that is able to list persistent volumes. This will require creating a cluster role and a cluster binding to bind the role to the service account. Create a pod with the Redis image that uses this service account.  

kubectl create service myserviceaccount
Create the cluster role.
kubectl create clusterrole myclusterrole --verb=list --resource=persistentvolume
Bind the cluster role to the service account.
kubectl create clusterrolebinding myclusterrolebinding --clusterrole=myclusterrole --serviceaccount=myserviceaccount
Create the pod definition file.
kubectl run somepod --image=redis --dry-run=client -o yaml > redis.yaml
Edit the pod definition file to include the name of the service account.
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: somepod
    name: somepod
  spec:
    serviceAccountName: myservice account
    containers:
    - image: redis
      name: somepod
      resources: {}
    dnsPolicy: ClusterFirst
    restartPolicy: Always
    status: {}

Create a pod called multi with two containers. Name the first pod, “alpha”; it will use the Redis image, and name the second pod, “beta”; it will use the busybox image running the command sleep 4800. Add the environment variable “name=alpha” to the first container and the variable “name=beta” to the second container. 

Start by create the first pod configuration file.
kubectl run alph --image=redis --env="name=alpha" --dry-run=client -o yaml > mulit.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: alpha
  name: alpha
spec:
  containers:
  - env:
    - name: name
      value: alpha
      image: redis
    name: alpha
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  status: {}
Then create the second pod. We will add the relevant portions to the file created previously.
kubectl run beta --image=busybox --env="name=beta" --dry-run=client -o yaml -- sleep 4800
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: beta
  name: beta
spec:
  containers:
  - args:
    - sleep
    - "4800"
    env:
    - name: name
      value: beta
    image: busybox
    name: beta
  resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  status: {}
The result of combining the two files. Apply using kubectl apply -f multi.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: multi
  name: multi
spec:
  containers:
  - env:
    - name: name
      value: alpha
    image: redis
    name: redis
    resources: {}
  - args:
    - sleep
    - "4800"
    env:
    - name: name
      value: beta
    image: busybox
    name: beta
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}